At the Forge - eBay Web Services

Access eBay development and test support in addition to its comprehensive Web services API.
Making Queries

Once you have gotten through the registration nightmare, you can make queries. The REST API is well documented and is quite straightforward to use. First, let's look at a simple program that sees how many matches we can find to a particular text string. The program, shown in Listing 1, is written in Ruby and is similar to some of the Amazon- and Google-searching programs presented during the past few months.

The program begins by retrieving our search parameters, automatically placed in the ARGV variable. We iterate over each element of ARGV, calling each individual argument query_string. We then use a hash to create an easily understood set of name-value pairs, in which the hash keys are the parameter names and the hash values are the parameter values. We then use a bit of Ruby magic to combine them, first turning them into pairs with map, and then using join to connect the pairs together with &. In the end, we have a string we can pass to eBay's server.

In this particular example, we're using the Query method in the REST API. Query allows us to enter a text string, for which eBay will then search. The way that eBay has grown somewhat organically over the years becomes apparent when you use its Web services. You must explicitly indicate if you want to search in stores as well as auctions. We also must indicate whether we want auction items, fixed-price items or both. Thus, our example searches through all stores (because StoreSearch = 3), auctions and fixed-price items (ItemTypeFilter = 3), in descriptions as well as item titles (SearchInDescription = 1), and with a fair amount of detail returned (DetailLevel = 3).

We also indicate we want Schema = 1. This tells eBay we want to receive a response using eBay's new XML schema, rather than the older one that is now being deprecated.

We then take ebay_params, a string created from our name-value pairs, and pass it to Net::HTTP.get_response. This sends an HTTP request to eBay's server (rest.api.ebay.com), using the appropriate path (/restapi), followed by our name-value pairs.

When we get a response—and our sample code here assumes that we do receive a response—we expect that it is formatted in XML and parse it using Ruby's built-in REXML library. We grab the total number of entries in eBay's database containing this search string and use the text method to extract the text from between the <TotalNumberOfEntries> tags. Finally, the program displays its output, showing us how many items on eBay contain this text string.

The API is relatively fast, allowing us to perform lookups for a particular string in relatively short time. That said, popular search strings can take far longer than rare words. A search for an ISBN took 1–2 seconds on my computer and indicated how many sellers were offering that ISBN for sale. A search for the term auction, not surprisingly, took more than 30 seconds to return a result and indicated that 29,458,603 sellers mentioned that term in the title or description. Obviously, the choice of search term, as well as the number of sellers and the quantity of text searched for that term, will have a significant effect on the performance of your application.

eBay's API makes it possible to perform Boolean searches of various types. Putting two words together within quotation marks (URL-encoded, of course) allows you to search for a phrase. You can search for two words in the same auction by linking them with commas.

You also can include and exclude particular sellers. If you are a seller on eBay, you might want to look at all of your items—or all of your competitors' items, ignoring yours. These functions make it easier to navigate through the complex world of eBay, which sells a staggering variety of goods from all over the world.

______________________

Webcast
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers

Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.

Learn More

Sponsored by AMD

White Paper
Red Hat White Paper: Using an Open Source Framework to Catch the Bad Guy

Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6

Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.

Learn more about catching the bad guy in this free white paper.

Learn More

Sponsored by DLT Solutions