Hack 98 Queue API Calls

figs/expert.giffigs/hack98.gif

How to work around eBay's API call limit.

Developers working under the Individual license, explained at the beginning of this chapter, have a strict limit as to the number of eBay calls they are allowed to make each day.

Provided that your application uses the API efficiently, as described in [Hack #99], this shouldn't pose a problem. And as the theory goes, anyone needing more than 50 API calls per day is probably running a business on eBay and can justify one of the higher developer tiers.

Consider the following scenario. As a seller, you might have 55 separate auctions ending on a single day. If you use the script in [Hack #87], you can retrieve sufficient data for all your recently closed auctions with only a single GetSellerList call. But if you use the script in [Hack #90], you'll need a minimum of 56 calls: one to retrieve the list of auctions, and 55 more to retrieve the individual auction descriptions. All you need is to run this single script just once to exceed your API quota for the day.

If you feel that exceeding your quota is a possibility, you can take some extra steps to ensure both that your software understands this limit, and that if your software reaches the limit, it can queue additional API calls and complete its work the next day.

The first step is to begin recording your API call usage. Probably the best way to do this is by adding a counter to the call_api subroutine in the ebay.pl script listed at the beginning of this chapter. The counter code might look something like this:

open (INFILE,"$localdir/apiquota.txt");
  my %count = map { split(/,/, $_) } <INFILE>;     [1]
close (INFILE);
chomp %count;

my ($today, $dummy) = split(/ /, &formatdate(time));
$count{$today}++;     [2]
open (OUTFILE,">$localdir/apiquota.txt");
  while (($date,$count) = each %count) {
    print OUTFILE "$date,$count\n";     [3]
  }
close (OUTFILE);

if ( $count{$today} > 50 ) {     [4]
   . . . 
  # queue this API call for later processing
   . . . 
  return undef;
  exit;
}

Here's how it works. The script retrieves the counts (line [1]) for all recorded days from the apiquota.txt file (located in the directory specified by $localdir in your config.pl script). It then adds 1 to today's count (line [2]) and writes the updated data back to the file (line [3]).

Finally, if today's count is above 50 (line [4]), it runs some code you provide (sorry, I'm not going to do all the work for you!) to queue the next API call. You could, for example, set up a temporary cron job, as described in [Hack #17], to run another script that resubmits queued API calls.

If nothing else, this nifty snippet of code records accounting data for your API usage. After a few days of using the API, your apiquota.txt file will look something like this:

2005-07-11,3
2005-07-12,11
2005-07-13,0
2005-07-14,60
2005-07-15,4

This shows that on July 14, 2005, 60 calls were made, while only 4 calls were made the next day. (Note that since the data is manipulated as a hash by this script, the records won't necessarily be recorded in order in the file.)