Mobile Zone is brought to you in partnership with:

Den is a DZone Zone Leader and has posted 460 posts at DZone. You can read more from them at their website. View Full User Profile

Flickr API for Windows Phone 7 – Part 11- Group Membership

10.14.2010
| 4824 views |
  • submit to reddit

In my previous article I’ve shown how to query Flickr to obtain lists of groups, that might be useful when looking for content and/or looking for groups where you can publish your content. Here, I am going to show how to retrieve the list of members that relate to a specific group.

A list of people who are members of a specific group is retrieved via flickr.groups.members.getList. This method requires authentication, but provides quite a few options when it comes to the types of members you want to retrieve.

First of all, there are three types of members: regular members, moderators and admins. These user categories are identified by a numeric value – 2 for regular members, 3 for moderators and 4 for admins. The ID equal to 1 is assigned to a narwhal user group is not returned by this API call. What this member group represents is unknown.

You can query the member list without specifying the user group and you will get everyone who is a member of that group. Remember that a group ID will be required here, since membership is connected to a group anyway.

You should already have the class structure outlined in my previous article. Inside the Members class (which is nested), create a method called GetList.

public static void GetList(string apiKey,string authToken, string signature, string groupID, HelperDelegate helperDelegate,
                string page="", string perPage ="")
{

}

This is the very basic method implementation and it is in no way the complete GetList. So what’s missing here? First of all, there should be the possibility to set what user categories should be selected. Now the interesting thing is the fact that this won’t necessarily be a single integer. For example, someone would want to query both admins and moderators, but not regular users. This is passed to the membertypes parameter via a comma, in case there is more than one parameter required.

So for the example outlined above, my parameter with its values will be structured like this:

membertypes=3,4

So how exactly can this be passed to the method? There are two basic ways – you can either request an array of numeric values with the IDs to be included as parameter values or use Boolean values that will represent each user type, defining whether those should be selected or not.

The main drawback of the first method is that the user can provide incorrect values and that will result in a failed request. The second one involves too many checking layers, so it will create some code sections that won’t look pretty at all.

Another solution is implementing an enum that will ensure that there is no way the user will pass incorrect values. But by default, enums don’t support string enumerations, so I am using a static class instead:

public static class SelectedTypes
{
    public const string All = "2,3,4";
    public const string Regular = "2";
    public const string Mod = "3";
    public const string Admin = "4";
    public const string RegularAndMod = "2,3";
    public const string RegularAndAdmin = "2,4";
    public const string ModAndAdmin = "3,4";
}

Basically, it is the same first method under a different cover, but it minimzes the chance of an incorrect value. There is a workaround that will let you use string enumerations, but that will involve reflection and attribute usage, so for now a static class will do.

I am now required to add another mandatory parameter to the method: string types. The method will automatically receive the correct string values depending on the selected type, if you use the static class reference. Of course, you could enter the values by yourself, but you will have to check them for correctness by yourself.

Let’s look at the core of this method:

helperDelegateInstance = helperDelegate;

WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Core.Groups.client_DownloadStringCompleted);
string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.groups.members.getList&api_key={0}&format=json&auth_token={1}&api_sig={2}&membertypes={3}&group_id={4}",
                        apiKey, authToken, signature,types,groupID);

if (page != "")
    URL += "&page=" + page;
if (perPage != "")
    URL += "&per_page=" + perPage;

client.DownloadStringAsync(new Uri(URL));

Notice that I am referencing the event handler that is placed outside the nested class, therefore I have to use the fully qualified name.

Execute this method like this:

Core.Authentication.GetSignature(param, "SECRET", () =>
{
    Core.Groups.Members.GetList(apiKey, "TOKEN", Core.Authentication.Signature,"GROUPID",Core.Groups.Members.SelectedTypes.All,
    () =>
    {
        Debug.WriteLine(Core.Groups.LastGroupResult);
    });
});

You can download the updated Groups.cs here.