Lately, I received a mail concerning my blogpost on Discogs API, saying the code didn’t work. Turned out it was due to new API limitations. Here’s how to get along with it.
I’ve recently received a mail saying the second part of the code wasn’t working, returning only NA. It turned out it was due to recent changes in Discogs API policy, limiting the number of calls you can make within a specific time window.
Also recently, I’ve been using Microsoft’s Computer Vision API — an API limiting calls to 20 per minute — for a blogpost on Data Bzh. So I said to myself: “time for a new blogpost!”.
How can you automate your API calls when the API is time-limited (for example, if you have 282 calls to make)?
Simplest way : a for loop and Sys.sleep()
Note : I won’t be making any API calls within this post, I’ll use Sys.time() to show how Sys.sleep() works.
If you’re calling Sys.time, and want to limit to 20 calls per minute, you’ll need to use Sys.sleep(). This function only takes one argument, time, which is the number of seconds you want R to stop before resuming.
When run, this function pause your session during the number of seconds you’ve entered. If you use this in a for loop, you can make a pause on every iteration of the loop. Here is an example with 10 seconds :
With a lapply
If you have access to the core of the function you want to use (i.e. the function calling on the API), you can use a lapply and insert a Sys.sleep() staight inside this function.
This is the method you’ll need to use if you’re trying to replicate the Discogs API calls.
Hope this can help!