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 12 – Group Pools

10.17.2010
| 5476 views |
  • submit to reddit

So we’re finally at the part where I am talking about group pools – now that you know how to get information about groups, you probably want to know how to manage photos that are available in the pool and contribute to the existing set. Well look no further.

Today I will be discussing 5 methods in the groups.pools category.

flickr.groups.pools.add

This is the method you would want to use to add photos to an existing group pool. Note, however, that this method doesn’t let you upload photos – you should already have a photo in your own Flickr collection that will be later on added to an existing group pool.

This method requires three parameters that you’ve probably thought about – the API key, which is tied to your application, the photo ID – this will be the photo that will be added to the pool, and of course the group ID – defining the group that will contain the photo passed.

Assuming that you already have the class skeleton (can be downloaded here), there is the Pools nested class – that’s where we’ll put the implementation for the methods that belong in the Pools category. Inside that class, create a method called Add:

static void Add(string apiKey, string authToken, string signature, string groupID, string photoID, HelperDelegate helperDelegate)
{

}

Let’s look at the parameters passed to this method. apiKey has an obvious purpose - it is used to store the API key. The authToken keeps the authentication token that is generated for your application based on the user account that authorized the application to be used with his/her Flickr account. The signature is generated based on the parameter array passed to the method and the unique user identifiers. groupID will be the alphanumeric identifier of the group that will accept the photo and photoID is the photoID itself (note, that you are not sending the photo in binary format but rather as a reference to an existing photo in a Flickr set linked to your account).

The core of this method is fairly simple:

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.pools.add&api_key={0}&format=json&auth_token={1}&api_sig={2}&photo_id={3}&group_id={4}",
        apiKey, authToken, signature, photoID, groupID);

client.DownloadStringAsync(new Uri(URL));

There is the HelperDelegate instance set in the parent class that is set to the instance passed to this specific method – it is really helpful when I need to execute an action right after the result is received – saves some time for me to directly work with threading to wait for the asynchronous operation to complete. Then, the WebClient instance will actually be the link between my request and the server response. The URL is built accordingly, considering the passed parameters.

NOTE: The signature for this method is generated outside the method itself. It is tied to a MD5 hashing service that should be launched separately or hosted. In case you were wondering if there is a way to use the service without having to run the service on your own, the answer is – yes, you can. I am hosting the MD5 hashing service at:

http://services.dennisdel.com/md5/MD5HashService.svc

Add a reference to this service and use it just the way you used it in previous samples – it runs on the same core engine.

That is pretty much it for the method – you can test it right away in your main application.

flickr.groups.pools.getContext

So now you have a picture added to the group pool. But you would also like to know what’s the next picture that goes after yours in the pool and what’s the picture that goes before it. getContext is exactly what you need in this case – it returns the IDs of photos that “surround” your own photo in the pool.

The parameter set is the same as for the method above (as there is no additional information needed to find the nearby pictures),  so the implementation is exactly like the flickr.groups.pools.add - what you want to do here is just copy and paste the same code block, the only difference being the passed URL – you need to change the method name. So what you need is this:

static void GetContext(string apiKey, string authToken, string signature, string groupID, string photoID, HelperDelegate helperDelegate)
{
    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.pools.getContext&api_key={0}&format=json&auth_token={1}&api_sig={2}&photo_id={3}&group_id={4}",
            apiKey, authToken, signature, photoID, groupID);

    client.DownloadStringAsync(new Uri(URL));
}

Simple, isn’t it?

flickr.groups.pools.getGroups

So now you know how to add photos to a group. But how exactly do you know where you can add photos?

getGroups returns a list of groups (where the user is a member of) that are ready for new pictures to be added to the pool. The implementation of this method shouldn’t be much of a problem since you are already familiar with the basic request structure:

static void GetGroups(string apiKey, string authToken, string signature, HelperDelegate helperDelegate, string page="",string perPage="")
{
    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.pools.getGroups&api_key={0}&format=json&auth_token={1}&api_sig={2}",
            apiKey, authToken, signature);

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

    client.DownloadStringAsync(new Uri(URL));
}

The only difference here is the fact that the number of results can be so high, that it should be separated on various pages, thus the presence of the page and perPage parameters.

flickr.groups.pools.getPhotos

This method might be a bit tricky – mainly because it can be either authenticated or not. If it is not authenticated, then it will get the list of photos in a group if those are accessible for public viewing.

Otherwise, none will be returned and you might want to authorize the call. So let’s look at the parameters used by this method:

•    api_key (Required)
•    group_id (Required)
•    tags (Optional)
•    user_id (Optional)
•    extras (Optional)
         description
         license
         date_upload
         date_taken
         owner_name
         icon_server
         original_format
         last_update
         geo
         tags
         machine_tags
         o_dims
         views
         media
         path_alias
         url_sq
         url_t
         url_s
         url_m
         url_o

•    per_page (Optional)
•    page (Optional)

Notice that although the method can be unauthenticated, it still requires the API key to identify the calling application. If it is authenticated, then you have to provide the authentication token and sign the call.

The plain method call returns only the basic information and chances are that eventually you will want to get more info about photos to perform specific manipulations with them. In this case, the extras parameter is what you’re looking for – it allows you to specify what additional photo information needs to be attached to the returned result.

The extras parameter can be passed as a string array (the same way I did when I was working on Favorites.GetList) and then each item from the array will be added to the URL.

So the actual implementation based on the requirements I stated above is like this:

public static void GetPhotos(string apiKey, string groupID, HelperDelegate helperDelegate, string[] extras,
string[] tags, string userID = "", string page = "",
string perPage = "", string authToken="", string signature ="")
{
    helperDelegateInstance = helperDelegate;

    WebClient client = new WebClient();
    client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
    string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.groups.pools.getPhotos&api_key={0}&group_id={1}&format=json",
            apiKey,groupID);

    if (tags != null)
    {
        string toAdd = "&tags=";
        for (int i = 0; i < tags.Length; i++)
        {
            if (i != tags.Length - 1)
                toAdd += tags[i] + ",";
            else
                toAdd += tags[i];
        }
        URL += toAdd;
    }

    if (extras != null)
    {
        string toAdd = "&extras=";
        for (int i = 0; i < extras.Length; i++)
        {
            if (i != extras.Length - 1)
                toAdd += extras[i] + ",";
            else
                toAdd += extras[i];
        }
        URL += toAdd;
    }

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

    client.DownloadStringAsync(new Uri(URL));
}

Although it might seems like this method is overly complicated – it is not. I do the same thing I did in previous methods, but there are two arrays – tags and extras. Since those aren’t required, I am checking against null to make sure that one is actually passed. In order for those to be ignored, all you have to do is pass null as the value when executing the call.

flickr.groups.pools.remove

Last and the easiest method from this category – it allows you to remove a picture from a group pool. It is absolutely (and by that I mean – 100%) the same as Add, the only difference being the method name passed to the URL. So here it is:

static void Remove(string apiKey, string authToken, string signature, string groupID, string photoID, HelperDelegate helperDelegate)
{
    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.pools.remove&api_key={0}&format=json&auth_token={1}&api_sig={2}&photo_id={3}&group_id={4}",
            apiKey, authToken, signature, photoID, groupID);

    client.DownloadStringAsync(new Uri(URL));
}

Congratulations! All group operations are now tied to this class. You can download the complete Groups.cs file here. Don't forget that if you plan on using this file, you need to add it to the Core folder inside the solution.