RESTful Live Search Service

Live Search finally gets a REST head and JSON-based API ... and testing the C# 4.0 dynamic RestClient against it (and Google Search) to see if it holds up to these services...

Earlier today, Live Search released a simple REST and JSON-based API for performing search with full flexibility for developers in terms of how to use the results (code named "Silk Road"). The previous API happened to be SOAP-based. Cool (and finally)! Check out the general documentation and the JSON example.

In brief, you issue a request to http://api.search.live.net/json.aspx?AppId={appID}&Market=en-US&Query={query_words}&Sources=web, and you get back a JSON structure as the response that looks like the following:

{
  "SearchResponse": {
    ...
    "Web": {
      "Total": 100,
      "Offset": 0,
      "Results": [
        {
          "Title": "...",
          "Description": "...",
          "Url": "...",
          "DisplayUrl": "...",
          "DateTime": "...",
          "Rank": ...
        }
      ]
    }
  }
}

My immediate thought was would my recent experimentation with REST via dynamic C# programming and turning dynamic method invokation into HTTP requests just work? In fact, can I use this new Live Search API without building a strongly typed wrapper? It turns out the answers are yes - the dynamic RestClient just worked. Here is my snippet of C# code:

dynamic liveSearch =
    new RestClient("http://api.search.live.net/json.aspx?AppId={appID}&Market=en-US&Sources=web",
                   RestClientMode.Json);
liveSearch.appID = AppSettings.Default.LiveSearchApiKey;

dynamic searchOptions = new JsonObject();
searchOptions.Query = "seattle";
dynamic response = liveSearch(searchOptions);

dynamic results = response.SearchResponse.Web.Results;
foreach (dynamic item in results) {
    Console.WriteLine(item.Title);
    Console.WriteLine(item.DisplayUrl);
}

Of course the dynamic REST applies just as well to the equivalent Google's Ajax Search API as well... for completeness:

dynamic googleSearch =
    new RestClient("http://ajax.googleapis.com/ajax/services/search/web?v=1.0",
                   RestClientMode.Json);
dynamic searchOptions = new JsonObject();
searchOptions.q = "seattle";
dynamic response = googleSearch(searchOptions);

dynamic results = response.responseData.results;
foreach (dynamic item in results) {
    Console.WriteLine(item.titleNoFormatting);
    Console.WriteLine(item.url);
}

I updated the DynamicREST github repository, and also updated the sample code. The snippets above illustrate quite natural looking c# for both making HTTP calls and handling the JSON responses.


[ Tags: | | | | | | | ]
Posted on Friday, 11/14/2008 @ 10:02 PM


Comments

3 comments have been posted.

Thanigainathan.S

Posted on 11/15/2008 @ 11:19 PM
Hi Nikhil,

This post seems to be fine . The greater thing is that the same can be used for Google service. So JSON plays the role here . Thanks for giving th sample code.

Thanks

jayesh

Posted on 12/2/2008 @ 9:28 PM
Hi,
solution is nice one,but it is in c# 4.0 ,can I implement same with c# 3.0?
actually I wanted WADL to represent RESTful web services.
how to use it?
can I have code sample or links which can help me out...
Thanks in advance..

Nikhil Kothari

Posted on 12/10/2008 @ 12:57 PM
Jayesh - you can certainly build a similar rest client that works in c# 3.0, but the point of this implementation was to explicitly highlight the programming model you can achieve when using "dynamic", which is a c# 4.0 feature that is in the works and available in CTP form as of now.
Post your comment and continue the discussion.