Session Ready
Exercise

Rate-limiting

The next stage of respectful API usage is rate-limiting: making sure you only make a certain number of requests to the server in a given time period. Your limit will vary from server to server, but the implementation is always pretty much the same and involves a call to Sys.sleep(). This function takes one argument, a number, which represents the number of seconds to "sleep" (pause) the R session for. So if you call Sys.sleep(15), it'll pause for 15 seconds before allowing further code to run.

As you can imagine, this is really useful for rate-limiting. If you are only allowed 4 requests a minute? No problem! Just pause for 15 seconds between each request and you're guaranteed to never exceed it. Let's demonstrate now by putting together a little loop that sends multiple requests on a 5-second time delay. We'll use httpbin.org 's APIs, which allow you to test different HTTP libraries.

Instructions
100 XP
  • Construct a vector of 2 URLs, http://httpbin.org/status/404 and http://httpbin.org/status/301.
  • Write a for-loop that sends a GET() request to each one.
  • Ensure that the for-loop uses Sys.sleep() to delay for 5 seconds between request.