Ingredients: Visual Studio 2008, .NET 3.5 sp1, WCF REST Starter Kit 2
The HttpClient type, included with the WCF REST Starter Kit 2, is intended to make it easier to consume RESTful services. One of the most widely used publically accessible REST API’s is the one provided by Twitter, making it an obvious target for trying out these new Microsoft bits.
Retrieving statuses from Twitter, as well as sending updates, is extremely easy using the WCF REST Starter Kit, as I posted earlier. This led me to want to see if I could build out a client library for the rest of the Twitter REST API.
This projected one night effort ended up taking about a week and a half over several evenings. For the most part, this was because actually building and testing the various contract objects is remarkably labor intensive.
The Windows Twitter Foundation (source code and binaries above) can be used as a reference application for anyone trying either to build a client on top of twitter or for those simply interested in seeing a relatively complicated implementation of the WCF REST Starter Kit.
If you want to just consume the binaries, you will need to download the WCF REST Starter Kit Preview 2 separately, if you haven’t already done so, and include the following assemblies in your project – Microsoft.Http.dll, Microsoft.Http.Extensions.dll, and Microsoft.ServiceModel.Web.dll .
The Windows Twitter Foundation library has several features I’m rather proud of:
1. It reads both XML and JSON formats. The client application included with the source code switches between these two API’s with either the /XML command or the /JSON command.
2. The library covers the complete Twitter REST API, including direct messaging and photo uploads. The photo uploads are particularly finicky and took a couple nights alone just to figure out.
3. The library also consumes the Twitter Search API. The data contracts for Trending was a bit too strange for me to figure out, however.
4. The CLR classes used for deserializing Twitter messages is included in a separate assembly. They are decorated for both XML and DataContract deserialization. If you are building your own Twitter client and are running into problems figuring out how to cast the Twitter messages, then these should help you out without obliging you to sniff out all the messages coming from Twitter in order to figure out their structure.
5. I built most of it in my installation of Windows 7. Visual Studio worked like a charm.
Things I feel bad about:
1. I couldn’t figure out how to deserialize the Trends service messages.
2. I used waaaay too many yellows and greens in the Console Application that comes with the source.
Please use this code freely and in any way you like. Be sure to consult the WCF REST Starter Kit 2 license on codeplex for any rights reserved by Microsoft.
I would like to expand the library with additional wrappers for tinyurl and tweetpics and all the other little services that make up the Twitter ecosystem.
I also plan to add helpers to support caching downloaded tweets and manage service calls to lighten the load on the Twitter servers (as the public API documents recommend).
My immediate goal, however, is to start building a nice WPF UI around the Windows Twitter Foundation library (if you haven’t gotten the joke about the name, yet, just give it a moment).
I’ve been enjoying the TweetDeck client, but so far haven’t seen anything in it that can’t be done using WPF. I should be able to make a UI that is at least as attractive (glossy black controls are pretty straightforward in PF, right?). Then I want to extend that design with carousels and some of the more interesting UI concepts that are part of the WPF development world. Using Prism would also be fun, but we’ll see.
The big gain in building a WPF client is that it can do things with the OS which are much more difficult in Adobe Air – for instance, hooking into the speech recognition features of Vista and Windows 7. Enabling users to start talking instead of typing their tweets may have interesting ramifications if it ever catches on. For myself, however, it will simply give me an excuse to start digging into speech recognition in Windows 7, which I hear works extremely well.
The next few posts will cover some of the more interesting things I found while digging through both the Twitter REST API as well as the WCF REST Starter Kit – for instance, that the HttpClient class isn’t actually based on WCF, but is instead a wrapper for the HttpWebRequest and HttpWebResponse objects – or that the Twitter API embodies some strange notions about what a “friend” is and what a “follower” is.
Again, enjoy the code.