Using the Google SOAP Search API - Asynchronous Calls
(Page 4 of 4 )
So far, all of our calls to the SOAP Search API service have been synchronous. However, the API also provides asynchronous functionality. Instead of waiting for a result to be returned before performing any more work, you can send a message to the service and do something else while you wait for a reply.
The API actually provides two ways to do this. The first method involves subscribing to the appropriate event and then calling a special version of whichever “do-” method you wish to use. Here, we perform an asynchronous Google search using this approach:
usingSystem;
usingSystem.Text.RegularExpressions;
classGoogleAsyncTest
{
private static Regex stripHtml = new Regex("<(.+?)>");
static void Main()
{
GoogleSearchService google = new GoogleSearchService();
google.doGoogleSearchCompleted += new
doGoogleSearchCompletedEventHandler(OnSearchCompleted);
google.doGoogleSearchAsync("x0x0", "Cookie Recipe", 0,
10, true, "", true, "", "", "");
Console.ReadKey();
}
static void OnSearchCompleted(object sender,
doGoogleSearchCompletedEventArgs e)
{
Console.WriteLine("Total results: " +
e.Result.estimatedTotalResultsCount);
foreach (ResultElement result in e.Result.resultElements)
{
Console.WriteLine("n" + stripHtml.Replace
(result.title, ""));
Console.WriteLine(stripHtml.Replace(result.snippet,
""));
}
}
}
Above, we subscribe the OnSearchCompleted static method to the onGoogleSearchCompleted event. We then call doGoogleSearchAsync and pass all the usual arguments. When the SOAP Search API service completes the search, the OnSearchCompleted method is called. Here, we display the estimated total number of results and then iterate through the ten results we receive. This information is contained within a doGoogleSearchCompletedEventArgs object.
There is also an alternate version of the doGoogleSearchAsync method that accepts an additional argument, an object. For example, here we pass a string:
static void Main()
{
...
string state = "This represents the current state.";
google.doGoogleSearchAsync("x0x0", "Cookie Recipe", 0, 10, true, "", true, "", "", "",
state);
...
}
The object is intended to represent the current state—anything we want to make use of later on in the OnSearchCompleted method. There, we can cast it to whatever type (here, a string):
static void OnSearchCompleted(object sender,
doGoogleSearchCompletedEventArgs e)
{
...
string state = (string)e.UserState;
...
}
In fact, the state object argument is mandatory when more than one asynchronous operation is in progress.
Cached pages and spelling suggestions are handled in much the same way:
static void Main()
{
google.doGetCachedPageCompleted +=
new doGetCachedPageCompletedEventHandler
(OnGetCachedPageCompleted);
google.doSpellingSuggestionCompleted +=
new doSpellingSuggestionCompletedEventHandler
(OnSpellingSuggestionCompleted);
...
google.doGetCachedPageAsync("x0x0", "http://google.com",
"State 1");
google.doSpellingSuggestionAsync("x0x0", "gogles", "State
2");
...
}
static void OnGetCachedPageCompleted(object sender,
doGetCachedPageCompletedEventArgs e)
{
byte[] page = e.Result;
// Do whatever here
}
static void OnSpellingSuggestionCompleted(object sender,
doSpellingSuggestionCompletedEventArgs e)
{
string spelling = e.Result;
// Do whatever here
}
The second way to use the API asynchronously utilizes callbacks rather than events. For example, to conduct a search, the BegindoGoogleSearch method is called. It takes the usual arguments, in addition to an AsyncCallback object (which points to the method that will be called when the search is complete) and a state object. It returns an object in the form of theIAsyncResult interface:
usingSystem;
usingSystem.Text.RegularExpressions;
classGoogleAsyncTest2
{
private static Regex stripHtml = new Regex("<(.+?)>");
private static GoogleSearchService google = new
GoogleSearchService();
static void Main()
{
IAsyncResult ar = google.BegindoGoogleSearch("x0x0",
"Cookie Recipe", 0, 10, true, "",
true, "", "", "", new AsyncCallback
(OnSearchCompleted), "State");
"State");
Console.ReadKey();
}
(continued)
...
In the OnSearchCompleted method, we can retrieve the state object, but before we can see the results of our search, we must call the EnddoGoogleSearch method, which returns a GoogleSearchResult object:
...
static void OnSearchCompleted(IAsyncResult ar)
{
string state = (string) ar.AsyncState;
GoogleSearchResult search = google.EnddoGoogleSearch(ar);
Console.WriteLine("Total results: " +
search.estimatedTotalResultsCount);
foreach (ResultElement result in search.resultElements)
{
Console.WriteLine("n" + stripHtml.Replace
(result.title, ""));
Console.WriteLine(stripHtml.Replace(result.snippet,
""));
}
}
Again, the process for getting cached pages or spelling suggestions is nearly identical:
static void Main()
{
...
IAsyncResult ar2 = google.BegindoGetCachedPage
("hW0StcxQFHJl8dGmsUxL/J3zS+fGcZs6",
"http://msn.com", new AsyncCallback
(OnGetCachedPageCompleted), "State 2");
IAsyncResult ar3 = google.BegindoSpellingSuggestion
("hW0StcxQFHJl8dGmsUxL/J3zS+fGcZs6",
"telephoen", new AsyncCallback
(OnSpellingSuggestionCompleted), "State 3");
...
}
static void OnGetCachedPageCompleted(IAsyncResult ar)
{
byte[] page = google.EnddoGetCachedPage(ar);
Console.WriteLine(page);
}
static void OnSpellingSuggestionCompleted(IAsyncResult ar)
{
string spelling = google.EnddoSpellingSuggestion(ar);
Console.WriteLine(spelling);
}
Conclusion
Though still in beta, the Google SOAP Search API appears to be very promising for developers who wish to implement a few of Google's features into their own applications. Currently, it offers support for Web searches, retrieval of cached pages and spelling suggestions. These three popular services can be easily accessed through the API, either synchronously or asynchronously, to create more complex and informed applications with .NET.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |