In previous articles, Creating a Twitter Client in C# and Creating a Twitter Client in C#, Part 2, I showed you the basics of creating a Twitter client using C# and WCF. We also saw problems with the Xml data and so we switched to the Json data format.
Today, we’ll continue and add another method call. This call is one of the fundamental methods used in a Twitter Client: Friends Timeline.
The Friends Timeline returns back an array of data. This adds a bit of extra complication to the method. The return value is an array of status objects. For this, we’ll need to create two new data structures: Status and Statuses.
[DataContract(Name = "status", Namespace = "")]
public class Status
{
[DataMember(Name = "id")]
public int Id { get; set; }
[DataMember(Name = "text")]
public string Text { get; set; }
[DataMember(Name = "user")]
public User User { get; set; }
}
[CollectionDataContract(Name = "statuses",
ItemName = "status", Namespace = "")]
public class Statuses : List<Status>
{
}
The Status class is set up very similar to the User class that we created last time. We have Id and Text fields which are present in the resulting Json data. Also, we’re including our previous User class since some user data is returned as well. You could create another class (for example, BasicUser) since not all of the information is included here as was in the VerifyCredentials call.
The Statuses class is a bit different. Since it’s a list, we need to declare it as a CollectionDataContract instead of just a simple DataContract. Also, we’re defining the type of items in the collection with the ItemName parameter.
Next, we need to add our new function to the interface.
public interface ITwitterClient
{
[OperationContract(Name = "FriendsTimeline")]
[WebGet(UriTemplate = "statuses/friends_timeline.json",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
Statuses FriendsTimeline();
}
And to the client class.
public class TwitterClient
{
public Statuses FriendsTimeline()
{
return base.Channel.FriendsTimeline();
}
}
Now we can retrieve the timeline from the client.
User user = client.VerifyCredentials();
Statuses statuses = client.FriendsTimeline();