Wednesday, January 28, 2009

Release 0.9.1.28

I've added the following elements to complete the Status class: in_reply_to_status_id, in_reply_to_user_id, favorited, and in_reply_to_screen_name


The folks at Twitter were also kind enough to add TwitterXML to their apps listing, so I now have "source" parameter being passed along with new updates. Twitter status updates will now be accompanied by the words "from TwitterXML" instead of "from web".


Thursday, January 15, 2009

Using serialized objects

Here we use the 'update' method from my previous example to create a Status object and get the screen name of the user that posted the status. We can use the screen name to call the 'show' method.

// Make your API calls with this.
private Twitter twitter;

// The responses from the Twitter REST API go here.
private XmlDocument xmlresponse;

// NEVER hard code your password. 
// I'm just trying to keep it simple for the example.
twitter = new Twitter("onil", "{my password}");

// Make a call to the 'update' method to post your status.
xmlresponse = twitter.StatusUpdate("Pooping.");

// Create a Status object using the XML response.
Status myStatus = new Status();
myStatus = (Status)XmlSerializerHelper.Deserialize(xmlresponse, typeof(Status));

// We can use the Status object to pass a parameter to the 'show' method.
// Every status object contains a User object representing the user that
// posted the status. We'll use the User object to get the screen name.
xmlresponse = twitter.UserShow(myStatus.User.Screen_Name);
            
// We could also have passed myStatus.User.Id in the above example.
// The Twitter REST API will accept the screen name or user ID. It's up to you.


// Create a User object using the XML response.
User myUser = new User();
myUser = (User)XmlSerializerHelper.Deserialize(xmlresponse, typeof(User));


I think that calling the Deserialize method is a bit annoying. TwitterXML will be changed in a future release so that all methods return a the deserialized XML  object instead of an XML document.

How to update your status

Here's how to use the 'update' status method.

// Make your API calls with this.
private Twitter twitter;

// The responses from the Twitter REST API go here.
private XmlDocument xmlresponse;

// NEVER hard code your password. 
// I'm just trying to keep it simple for the example.
twitter = new Twitter("onil", "{my password}");

// Make a call to the 'update' method to post your status.
xmlresponse = twitter.StatusUpdate("Pooping.");

// Create a Status object using the XML response.
Status myStatus = new Status();
myStatus = (Status)XmlSerializerHelper.Deserialize(xmlresponse, typeof(Status));
            

// Convert the object back into XML.
// I find this useful for testing by printing the results to the screen.
xmlresponse = XmlSerializerHelper.Serialize(myStatus);


Here is a comparison of the response from the Twitter REST API against serializing the Status object:

Response from Twitter REST API

???<?xml version="1.0" encoding="UTF-8"?>
<status>
  <created_at>Fri Jan 16 04:17:39 +0000 2009</created_at>
  <id>1122908409</id>
  <text>Pooping.</text>
  <source>web</source>
  <truncated>false</truncated>
  <in_reply_to_status_id>
  </in_reply_to_status_id>
  <in_reply_to_user_id>
  </in_reply_to_user_id>
  <favorited>false</favorited>
  <in_reply_to_screen_name>
  </in_reply_to_screen_name>
  <user>
    <id>13871782</id>
    <name>Onil Patel</name>
    <screen_name>onil</screen_name>
    <location>Toronto, ON</location>
    <description>
    </description>
    <profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/50747572/me_normal.jpg</profile_image_url>
    <url>http://onil.homeip.net</url>
    <protected>false</protected>
    <followers_count>3</followers_count>
  </user>
</status>

Serialization of myStatus

???<?xml version="1.0" encoding="utf-8"?>
<status xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <created_at>Fri Jan 16 04:17:39 +0000 2009</created_at>
  <id>1122908409</id>
  <text>Pooping.</text>
  <source>web</source>
  <truncated>false</truncated>
  <user>
    <id>13871782</id>
    <name>Onil Patel</name>
    <screen_name>onil</screen_name>
    <location>Toronto, ON</location>
    <description />
    <profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/50747572/me_normal.jpg</profile_image_url>
    <url>http://onil.homeip.net</url>
    <protected>false</protected>
  </user>
</status>

As you can see, I'm missing some elements (highlighted in red) in my Status class. Twitter probably added these after I created it. They'll be added in a future release of TwitterXML.

Sunday, January 11, 2009

Release 0.9.1.11

This is the first release. All included methods were last tested on January 4th, 2009, but modifications still need to be made to better handle error responses from the Twitter REST API.

It is also highly likely that instead of returning an XMLDocument, the methods in a future release will return object representations of the the XML response: Statuses, Status, Users, User, DirectMessages, DirectMessage.

Get it from CodePlex.

Twitter API library with XML serialization for C# .NET

A .NET library containing methods for calling the Twitter REST API. Currently, all of the methods return an XMLDocument object. Also included are classes for Users, Statuses, and Direct Messages that work with XML serialization. This means you can convert the XML responses to an object with a single Deserialize() call.

I’m aiming to make this as simple and easy to use as possible.

I hope you like it.