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 7 – Collections

10.05.2010
| 7029 views |
  • submit to reddit

Flickr has a feature called collections that allows you to group photo sets and other collections in larger groups. One important thing to mention here is that collections are only available for pro (paid) accounts on Flickr, so if you don’t have one, then you won’t be able to directly test this code. However, I’d still recommend having it in your application, since the end user might be using a pro account.

So to start, there are two methods that allow working with collections:
•    getInfo – gets the list of photos/collections/sets inside a single collection
•    getTree – returns a list of collections that are created by the end user

I went ahead and created a Collections class using the same class template I’ve used in my previous example, so the skeleton looks like this:

using System;
using System.Net;
using System.Windows;
 
namespace FlickrWP7.Core
{
    public class Collections
    {
        private HelperDelegate helperDelegateInstance;
 
        public string LastCollectionResult { get; set; }
    }
}

The getInfo method implementation follows the standard pattern – it requires authentication, so there will be the authentication token and the signature. Together with the tied event handler (triggered when JSON data is downloaded) it looks like this:

public void GetCollectionInfo(string apiKey, string signature, string collectionID, HelperDelegate helperDelegate)
{
    helperDelegateInstance = helperDelegate;

    WebClient client = new WebClient();
    client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
    string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.collections.getInfo&api_key={0}&api_sig={1}&collection_id={2}&format=json",
            apiKey,signature,collectionID);

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

void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    LastCollectionResult = e.Result;
    helperDelegateInstance();
}

The collectionID parameter can be found through the web browser address bar when you navigate to a specific collection. It is a required parameter for this method and you cannot skip it. Now all I have to add is the getTree implementation.

This method does not require authentication, so there is no need to generate the signature and you don’t need to pass the authentication token. The API key is, however, required.

public void GetCollectionTree(string apiKey, HelperDelegate helperDelegate, string collectionID = "", string userID="")
{
    helperDelegateInstance = helperDelegate;

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

    if (collectionID != "")
        URL += "&collection_id=" + collectionID;
    if (userID != "")
        URL += "&user_id=" + userID;

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

The collectionID parameter here is optional. If you specify it, a list of collections will be returned that belong to the specified collection. The userID is also optional and will return the collection set for a specific user (defined by a unique ID).

The Collections.cs file is attached here, so you can easily insert it into the existing project.