Skip to content

Commit

Permalink
Added retries to textpresso api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
valearna committed Jun 18, 2024
1 parent 2a7768b commit b4d463c
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions genedescriptions/api_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import ssl
import urllib.request
from http.client import HTTPException

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -31,9 +32,16 @@ def get_textpresso_popularity(self, keyword: str):
data = json.dumps({"token": self.textpresso_api_token, "query": {
"keywords": keyword, "type": "document", "corpora": ["C. elegans and Suppl"]}})
data = data.encode('utf-8')
req = urllib.request.Request(self.tpc_api_endpoint, data, headers={'Content-type': 'application/json',
'Accept': 'application/json'})
res = urllib.request.urlopen(req)
res = None
num_tries = 0
try:
num_tries += 1
req = urllib.request.Request(self.tpc_api_endpoint, data, headers={'Content-type': 'application/json',
'Accept': 'application/json'})
res = urllib.request.urlopen(req)
except HTTPException:
if num_tries > 5:
raise HTTPException
logger.debug("Sending request to Textpresso Central API")
popularity = int(json.loads(res.read().decode('utf-8')))
self.tpc_cache[keyword] = popularity
Expand Down

0 comments on commit b4d463c

Please sign in to comment.