Skip to content

Commit

Permalink
Use httpx for making requests
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosribas committed May 30, 2024
1 parent c593d44 commit 212b5f1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
8 changes: 3 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ async def submit_job(
status_code=400, detail="Please upload a file in FASTA format"
)

# Validate the FASTA file
try:
content = await sequence_file.read()
parsed = api.SubmittedRequest.parse(content.decode())
Expand All @@ -190,11 +191,8 @@ async def submit_job(
query.sequences = "\n".join(parsed.sequences)
query.email_address = email_address if email_address else "[email protected]"

try:
job_id = await jd.JobDispatcher().submit_cmscan_job(query)
except HTTPException as e:
logger.error(f"Error submitting job. Error: {e}")
raise e
# Submit to Job Dispatcher
job_id = await jd.JobDispatcher().submit_cmscan_job(query)

if email_address:
# Background task to check status
Expand Down
46 changes: 37 additions & 9 deletions rfam_batch/job_dispatcher.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from __future__ import annotations

import aiohttp
import httpx
import re
import typing as ty

import aiohttp
from fastapi import HTTPException
from logger import logger

INFERNAL_CMSCAN_BASE_URL = "https://www.ebi.ac.uk/Tools/services/rest/infernal_cmscan"

Expand Down Expand Up @@ -46,16 +49,41 @@ async def shutdown(cls) -> None:
async def submit_cmscan_job(self, data: Query) -> str:
try:
query = data.payload()
async with self.client.post(
f"{INFERNAL_CMSCAN_BASE_URL}/run", data=query
) as r:
response_text = await r.text()
return response_text
# return JobSubmitResult(job_id=response_text)
async with httpx.AsyncClient() as client:
response = await client.post(
f"{INFERNAL_CMSCAN_BASE_URL}/run", data=query
)
response.raise_for_status()
return response.text
except httpx.RequestError as e:
raise HTTPException(
status_code=500,
detail=f"An error occurred while requesting {e.request.url!r}: {str(e)}",
)
except httpx.HTTPStatusError as e:
if e.response.status_code == 400 and "<description>" in response.text:
# Extract and display the Job Dispatcher error message, e.g.:
# <error>
# <description>Please enter a valid email address</description>
# </error>
text = re.search(
r"<description>(.*?)</description>", response.text, re.DOTALL
)
logger.error(f"JD error message: {text.group(1)}")
raise HTTPException(status_code=400, detail=text.group(1))
else:
raise HTTPException(
status_code=e.response.status_code,
detail=f"Error {e.response.status_code} while requesting "
f"{e.request.url!r}: {e.response.text}",
)
except httpx.TimeoutException as e:
raise HTTPException(
status_code=504, detail=f"Request to {e.request.url!r} timed out."
)
except Exception as e:
print(f"An Exception occurred: {e}")
raise HTTPException(
status_code=500, detail=f"An unexpected error occurred: {e}"
status_code=500, detail=f"An unexpected error occurred: {str(e)}"
)

async def cmscan_result(self, job_id: str) -> str:
Expand Down

0 comments on commit 212b5f1

Please sign in to comment.