Skip to content

Commit

Permalink
fixed async notebook issue and switched to httpx
Browse files Browse the repository at this point in the history
  • Loading branch information
haeussma committed Apr 12, 2024
1 parent a2423de commit 5d46adc
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions pyeed/fetchers/interprorequester.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
from typing import List
import aiohttp
import httpx


class InterProRequester:
Expand All @@ -10,17 +10,40 @@ def __init__(self, ids: List[str], rate_limit_per_second: int = 30):
self.rate_limit_per_second = rate_limit_per_second

async def make_request(self):
async with aiohttp.ClientSession() as session:
"""
Make a request to the InterPro API for each ID in the list of IDs.
Returns:
A list of JSON responses from the InterPro API for each ID.
Raises:
aiohttp.ClientError: If there is an error making the request.
asyncio.TimeoutError: If the request times out.
"""

async with httpx.AsyncClient() as session:
tasks = [self.fetch(session, id) for id in self.ids]
return await asyncio.gather(*tasks)

async def fetch(self, session, id):
url = (
"https://www.ebi.ac.uk/interpro/api/entry/all/protein/UniProt/"
+ id
+ "?format=json"
)
async with session.get(url) as response:
data = await response.json()
async def fetch(self, session: httpx.AsyncClient, id):
"""
Fetches data from the InterPro API for a given ID.
"""
url = f"https://www.ebi.ac.uk/interpro/api/entry/all/protein/UniProt/{id}?format=json"
try:
response = await session.get(url)
if response.status_code == 204:
print(f"No InterPro entry found for ID: {id}")
return None

data = response.json()
await asyncio.sleep(1 / self.rate_limit_per_second)

return data

except httpx.RequestError as e:
print(f"Request error occurred: {e}")
except httpx.HTTPStatusError as e:
print(f"HTTP status error occurred: {e}")
finally:
await response.aclose()

0 comments on commit 5d46adc

Please sign in to comment.