Geeks With Blogs

Joe Mayo

Today, I added a new extensibility feature to LINQ to Twitter, called Raw Queries. The Twitter API has historically changed suddenly and without warning, leaving 3rd party libraries, such as LINQ to Twitter deficient in full support.  On occasion, LINQ to Twitter encounters bugs that leave users without a means for accomplishing specific tasks.  Therefore, we need work arounds to allow users to quickly adapt to changes and new features.

Raw queries include two points of extensibility: the ability to perform any query and the ability to execute side-effects.  The extensibility will allow users to control the entire URL, including parameters.  Furthermore, the results will be raw XML returned from Twitter.

To perform queries, use the RawQuery entity. One of the new APIs that, as I write, LINQ to Twitter will soon support, is account totals.  Here’s an example:

var rawResult =
    (from raw in twitterCtx.RawQuery
     where raw.QueryString == "account/settings.xml"
     select raw)

Console.WriteLine("Response from Twitter: \n\n" + rawResult.Result);

Notice that QueryString is set to the URL segment for querying Twitter’s account totals. RawQuery will append your query string to the BaseUrl property to create a full URL. Therefore, you shouldn’t add the full URL to QueryString. The query will only ever return one Raw object. The Raw.Result property returns the XML that Twitter returns.

Raw Queries supports parameters.  However, this particular extensibility is raw, giving you a greater amount of control over the query.  This also means you’re responsible for encoding parameters, as shown in the following example:

twitterCtx.BaseUrl = "";
string unencodedStatus = "LINQ to Twitter";
string encodedStatus = Uri.EscapeDataString(unencodedStatus);
string queryString = "search.atom?q=" + encodedStatus;

var rawResult =
    (from raw in twitterCtx.RawQuery
     where raw.QueryString == queryString
     select raw)

Console.WriteLine("Response from Twitter: \n\n" + rawResult.Result);

The example above uses Uri.EscapeDataString to encode the search query.  If you don’t encode results and and your parameters contain invalid characters, you’ll receive an exception with results from the Twitter API.

In addition to queries, Raw Queries allows you to perform side-effects, those actions that change data in Twitter. The following example shows how to perform a status update, posting a Tweet on Twitter, with the ExecuteRaw method:

string status = "Testing LINQ to Twitter Raw Interface: " + DateTime.Now.ToString();
var parameters = new Dictionary
    { "status", status }

string queryString = "/statuses/update.xml";

string result = twitterCtx.ExecuteRaw(queryString, parameters);

Console.WriteLine("Result from update status: \n\n" + result);

The ExecuteRaw method takes two parameters: queryString and parameters. The queryString parameter is the same as QueryString in the RawQuery entity query above. Again, don’t provide the full URL because ExecuteRaw will append your queryString to the BaseUrl.  The parameters is a Dictionary of name/value pairs. Unlike RawQuery queries, where you are required to encode parameters, you shouldn’t encode parameters for ExecuteRaw.  Behind the scenes, ExecuteRaw uses the same execution logic as all other side-effect methods.  The return value from ExecuteRaw is the XML response returned from Twitter.

Moving forward, the recommended approach to using LINQ to Twitter will be via specific queries and methods.  The raw extensibility interface is intended for those situations where specific API support isn't available.


Posted on Saturday, October 2, 2010 8:50 PM C# , Twitter , LINQ , LINQ to Twitter | Back to top

Copyright © Joe Mayo | Powered by: