{ "cells": [ { "metadata": { "collapsed": true }, "cell_type": "markdown", "source": "# Rate limiting\n\nRequests are rate limited to prevent a single user from monopolising the resources.\n\nResponse headers show we are allowed 55000 requests over an hour (3600 seconds): an average 15 requests per second.\n\n## Exercises 7\n\nThis script queries the ping endpoint 25 times, printing the count, the HTTP Status Code, and the X-RateLimit-Remaining header each time.\n\n1\\. Increase the number of loops, do you start to get 429 errors?\n\n2\\. Can you add in a step to make it wait a few seconds every iteration? Or every 100 iterations?" }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "# Exercise 7.1\nlibrary(httr)\n\nserver <- \"http://rest.ensembl.org/\"\ncon <- \"application/json\"\next_ping <- \"/info/ping?\"\n\nx <- 0\n\nwhile (x < 25) {\n ping <- GET(paste(server, ext_ping, sep = \"\"), accept(con))\n x = x + 1\n print( paste (\"count: \", x, \"status:\", status_code(ping), \"remaining:\", headers(ping)['X-RateLimit-Remaining']))\n}", "execution_count": null, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "[Next page: Exercises 7 – answers](7_Rate_limiting_answers.ipynb)" } ], "metadata": { "kernelspec": { "name": "r", "display_name": "R", "language": "R" }, "language_info": { "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "3.5.3", "file_extension": ".r", "codemirror_mode": "r" } }, "nbformat": 4, "nbformat_minor": 2 }