Update: Now storing Flickr.Net source code over here on WDevs.com where a new version is now available for download.
I found the Flickr.Net workspace over at GotDotNet (I’m not linking to the stupid thing as it doesn’t work! what a load of crap it is) which attempts to create a .Net library for accessing the Flickr API.
But basically it only did the upload picture bit, and none of the rest of the API, so I decided to do the lot. So far I’ve written all the code except for the upload bit, as well as a test set and a sample application. I plan on submitting the work either to the Flickr.Net team or something later on.
So download the files here. It contains a complete Visual Studio 2003 solution, the library (coded in C#) plus a NUnit test project to test everything (not complete yet), and a sample application which uses the FlickrNet dll to get a users sets, and then download thumbnails of the images and creates some Html containing the images and links to their pages.
Update: I’ve updated the above to NOT include my Api key, sorry for the inconvenience. I’ve also fixed a bug or two and added more tests.
The code uses XML Serialization to move between the response sent back and the class objects. Quite a few of the classes have multiple uses and some properties are only available if returned from a particular function.
Below is the function that does the bulk of the work:
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Proxy = Proxy;
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
XmlSerializer serializer = new XmlSerializer(typeof(FlickrNet.Response));
StreamReader sr = new StreamReader(res.GetResponseStream());
string s= sr.ReadToEnd();
StringReader str = new StringReader(s);
FlickrNet.Response response = (FlickrNet.Response)serializer.Deserialize(str);
From there the each method calls the GetResponse function with the url and gets back a Response object. This object has many properties, each of which is filled in depending on the flickr method being called.
To use the code you need to create a copy of the Flickr class, passing in your email address and password (if you want to use methods which require authentication).
Flickr f = new Flickr("email@example.com", "password");
The you call the relevant methods on the object, and get the response. The object also exposes a Proxy object so that the proxy can be configured if desired (I need one for work, so this is always high on my list of things to do).
FlickrNet.User u = f.PeopleFindByUsername("Sam Judson");
The above will return a User object for me, giving you access to my userId and username.
The sample application asks for a screen name, then lists that users photosets, and then finally displays a collection of thumbnails followed by the HTML code required to display the images with a link back to their homepage (I got the idea for this from Mamamusings post). The links are of the same format as the ones Flickr gives you.
The unit test, which are not quite complete yet use NUnit 2.2 to test each of the functions. The library itself has a load of test parameters that need to be configured before you can run them – it needs a valid email address and password, and various test photo ids and group ids etc so that it can test each method properly.
So far I’ve got tests for about 75% of the functions, which isn’t bad going I reckon.
The way forward
Firstly, lets point out that Flickr is still in beta, and the api is changing all the time (new functions, fixes etc).
If you have any comments you can either email me or post to the API developers mailing list.