"Working on the complexity that is always indirectly implied by simpilicity."

Amplify's WCF Twitter API Client Library v0.2

After some playing around with WCF and Flickr, I decided to move everything internally for the twitter library to use WCF with it's ServiceContract.  It was more of a pain that initially thought it would be. WCF is amazingly customizable but still has a couple of downfalls thanks to REST and people not writing their API for all technologies. 

The biggest issue is that Twitter throws an Http 403 unknown exception when you call a page and something is incorrect in the url.  Well with WCF, it throws a System.ServiceModel.Security.MessageSecurityException, due to the url returning a status code other than 200, and it wraps the inner exceptions inside of that.

I'm sure there is a better way to get around this, but each proxy call is now wrapped by a try/catch in the client class methods in order to check for the MessageSecurityException, then it checks the inner exception and determines if twitter sent back a response error message. If it does the library now throws a twitter exception with error information, otherwise you would only see the WCF exception that would mask the real issue. 

Also I renamed the "Service" classes to "Client" seeing that is the proper term for them.  The tests are now updated as well to reflect the changes.  The tests are a good way of seeing of how to use the library. Last but not least, methods now return arrays instead of lists in order to be more REST friendly.

using Amplify.Twitter;
using System.Security;

// ... stuff

public void Test() 
{
	try {
		string temp = "password";
	
		Twitter.SetCredentials("Username", temp);

		StatusClient client = Twitter.CreateStatusClient();
 		// or StatusClient client = new StatusClient("Username", temp);

        	Status[] tweets = service.GetUserTimeline();
	} catch (TwitterException ex) {
		Log.Write(ex.ToString()); // write the twitter rest error. 
	}

}

Labels: , , , , , , , ,

Amplify's TwitterN, Yet Another C# Twitter REST API Library

http://code.google.com/p/twittern

I put together a C# twitter library that uses WCF's DataContract & DataMembers and .Net 3.5's DataContractJsonSerializer.  Its currently in alpha stage, as I need to finish writing the tests, documentation and make a simple WPF client for twitter.  I needed a twitter library for a personal "secret" project of mine and found that most twitter libraries do not keep up with the twitter API and break when using them. Plus they tend to go to great lengths when parsing the xml.  Unfortunately I do not know if this will work on mono yet. It seems that they have "Olive", which is mono's version of WCF and I've seen where they have a path for the "DataContractJsonSerializer".  If there are any one familiar enough with mono, please by all means use the code, join the project. 

One design decision that I made was to use Json rather than xml as its more compact and less data to transfer.  I used WCF because with DataContract & DataMembers you can parse Json and you can name your properties the property way with Pascal Case properties and then tell wcf what attributes of json they represent. This way you don't have properties that look like ruby like "screen_name" or  "profile_sidebar_border_color" in your Data Transfer Object in order to deserialize the Json response from twitter.

 

A basic sample of how to use the library is below.


using Amplify.Twitter;
using System.Security;

// ... stuff

public void Test() 
{
	SecureString password = new SecureString();
	string temp = "password";
	for (var i = 0; i < temp.Length; i++)
		password.AppendChar(temp[i]);
	
	password.MakeReadOnly();
	Twitter.SetCredentials("Username", password);

	StatusService service = Twitter.CreateStatusService();
 	// or StatusService service = new StatusService("Username", password);

        List<Status> tweets = service.GetUserTimeline("MichaelHerndon");
}

Labels: , , , , , , ,