Mining Google Web Services: Building Applications with the Google API
Today, many people develop applications for individual Web services. We're past the stage where Web services are completely new, but now that businesses are beginning to accept Web services (see the eWeek story at http://www.eweek.com/article2/0,4149,1455562,00.asp for details), they're looking at them as single applications. This stage in the development of Web services will eventually pass too. At some point, developers will realize that the true power of Web services is in combining the functionality that each Web service provides into a new form ”a cohesive whole. At least one intrepid developer is already making strides in this area. Make sure you review the Authorama site at http://www.authorama.com/ for details on how this application is put together.
Note | This chapter doesn't contain very much information about Amazon Web Services. For a complete discussion of this valuable Web service, get my book, Mining Amazon Web Services: Building Applications with the Amazon API (Sybex, 2004). You can also learn more on the Amazon site at http://www.amazon.com/gp/browse.html/103-5753584-4315028?node=3435361. |
This section of the chapter creates a simple application that uses resources provided by both Google and Amazon to research a Universal Product Code (UPC). The two Web services work very well together, in this case, because they both provide a piece of information a user is likely to need. Amazon Web Services tells the user the product name , which vendor produces the product, and the Amazon price. In addition, the user could obtain product reviews from Amazon. Google Web Services takes the user further by providing a list of links about the product. You can't currently look for a product using UPC on Google Web Services, so this Web service can't do the job alone. Likewise, Amazon Web Services can't provide related links. By combining the two Web services, you get a much better view of what this product is all about. Listing 11.3 shows a typical example of how these two Web services can work together. You'll find the complete source code for this example in the \Chapter 11\CombinedServices folder of the source code located on the Sybex Web site.
Listing 11.3: Combining Amazon Web Services with Google Web Services
|
private void btnTest_Click(object sender, System.EventArgs e) { UpcRequest Request; // The UPC of the CD. AmazonSearchService AServe; // Amazon Search Service ProductInfo PI; // Returned information. GoogleSearchService GServe; // Search service routine access. GoogleSearchResult Result; // All of the results. ResultElement[] Items; // All of the search items. DataRow DR; // Output data. // Create the service. AServe = new AmazonSearchService(); // Create and define the request. Request = new UpcRequest(); Request.devtag = txtTag.Text; Request.mode = txtMode.Text; Request.tag = "webservices-20"; Request.type = "lite"; Request.upc = txtUPC.Text; // Get the data. PI = AServe.UpcSearchRequest(Request); // Add data to the appropriate places on screen. txtASIN.Text = PI.Details[0].Asin; txtAvailability.Text = PI.Details[0].Availability; txtPrice.Text = PI.Details[0].ListPrice; txtManufacturer.Text = PI.Details[0].Manufacturer; txtName.Text = PI.Details[0].ProductName; txtReleaseDate.Text = PI.Details[0].ReleaseDate; lblURL.Text = PI.Details[0].Url; // Now that we have Google search criteria, create the // search service. GServe = new GoogleSearchService(); // Make the call. Result = GServe.doGoogleSearch( txtLicense.Text, txtName.Text + " " + txtManufacturer.Text, 0, 10, false, "", false, "", "", ""); // Clear the dataset of previous results. dsGoogle.Tables["SearchResults"].Clear(); // Process the result elements. Items = Result.resultElements; foreach (ResultElement Item in Items) { // Add a row. DR = dsGoogle.Tables["SearchResults"].NewRow(); // Add the data to the row. DR["Title"] = StringToText(Item.title); DR["URL"] = Item.URL; if (Item.snippet.Length > 0) DR["SnippetOrSummary"] = StringToText(Item.snippet); else DR["SnippetOrSummary"] = StringToText(Item.summary); DR["CachedSize"] = Item.cachedSize; // Display the row on screen. dsGoogle.Tables["SearchResults"].Rows.Add(DR); } }
|
Before you do anything else, you need to add two Web references to your application. The first, http://api.google.com/GoogleSearch.wsdl, is for Google Web Services, while the second, http://soap.amazon.com/schemas3/AmazonWebServices.wsdl, points to Amazon Web Services. You'll also need to reference the two Web services at the beginning of the code module like this:
-
using CombinedServices.com.amazon.soap;
-
using CombinedServices.com.google.api;
Now that you have the proper references in place, it's time to look at the code. Remember, all the code has to go on when it starts is a UPC. You would normally hard code the license numbers into the code. The user also has to supply a product category, music, in this case, but you could theoretically hard code that information too. The inputs you require depend entirely on how the user will interact with the application and how the two Web services interact.
The code begins by creating an Amazon Web Services connection. Creating the service is about the same as working with Google. However, the next step is different. Amazon supports a number of complex request types, each of which requires a special request object. In this case, the code creates a UpcRequest object to hold the Amazon data. This data includes the developer tag ( essentially the same as the Google license), a product mode (such as music or books), an associate tag (in case you want to sell products through Amazon), the kind of search you want to perform (lite or heavy), and the UPC. The code sends all this information to Amazon Web Services using the AServe.UpcSearchRequest(Request) method call.
At this point, the ProductInfo object, PI , holds all of the data that Amazon provides for this particular request. The example doesn't use all of the information, but does provide a good representation of the information, as shown in Figure 11.4. In this case, the important information includes the product name, manufacturer, release date, availability, and price. In addition, the application provides an Amazon-specific identification number and an URL that the user can click to buy the product online.
Now that the application knows more about the product, it can perform a search for it using Google. The code instantiates the Google Web Service object, GServe , and uses it as many of the examples have in this book. However, you'll quickly notice that except for the required developer license, Google Web Services doesn't receive any input from the user. All of this input comes from Amazon, yet the user is the beneficiary of the output, as shown in Figure 11.4.
The code makes the search request as normal and fills the dataset with the resulting Web site titles, URLs, snippets, and cached sizes. The output appears in a grid, as shown in Figure 11.4. Now the user can search online to learn more about the product before buying it. Imagine a retail kiosk that uses this setup to help buyers make good decisions without sales staff support. In addition, a user could easily load such an application on a Pocket PC or access it as a Web application using a Palm. Now a store experience need not include the dread that follows some purchases. A user is always informed about the product.