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.
Posted on Friday, 11/14/2008 @ 10:02 PM