Skip to content

Commit

Permalink
Add delay to respect ncbi entrez api rate limit
Browse files Browse the repository at this point in the history
Add 1 second delay within the Refactor.xml() function when the ncbi/entrez api returns a header ['X-RateLimit-Remaining'] <=1  #68
  • Loading branch information
dmboyd authored and sckott committed Sep 11, 2020
1 parent 1dc2a24 commit dc4a2ed
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pytaxize/refactor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import requests
from lxml import etree

import time

class Refactor:
def __init__(self, url, payload={}, request="get"):
Expand All @@ -20,6 +20,12 @@ def xml(self, **kwargs):
out.raise_for_status()
xmlparser = etree.XMLParser()
tt = etree.fromstring(out.content, xmlparser)
try:
# If entrez api 'X-RateLimit-Remaining' header is 1 or below, pause for a second to allow rate limit to reset
if (int(out.headers['X-RateLimit-Remaining'])<=1):
time.sleep(1)
except:
pass
return tt
else:
out = requests.post(self.url, params=self.payload, **kwargs)
Expand Down

0 comments on commit dc4a2ed

Please sign in to comment.