From 212b5f1139c5b230285b27a3dc945bd2254e2b52 Mon Sep 17 00:00:00 2001 From: carlosribas Date: Thu, 30 May 2024 11:25:20 +0100 Subject: [PATCH] Use httpx for making requests --- main.py | 8 +++---- rfam_batch/job_dispatcher.py | 46 +++++++++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index eba7eb5..6c7016d 100755 --- a/main.py +++ b/main.py @@ -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()) @@ -190,11 +191,8 @@ async def submit_job( query.sequences = "\n".join(parsed.sequences) query.email_address = email_address if email_address else "dummy@email.com" - 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 diff --git a/rfam_batch/job_dispatcher.py b/rfam_batch/job_dispatcher.py index a6cd045..fc7e72c 100644 --- a/rfam_batch/job_dispatcher.py +++ b/rfam_batch/job_dispatcher.py @@ -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" @@ -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 "" in response.text: + # Extract and display the Job Dispatcher error message, e.g.: + # + # Please enter a valid email address + # + text = re.search( + r"(.*?)", 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: