Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/opencloning/ncbi_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

async def async_get(url, headers, params=None) -> Response:
async with get_http_client() as client:
return await client.get(url, headers=headers, params=params, timeout=20.0)
resp = await client.get(url, headers=headers, params=params, timeout=20.0)
if resp.status_code == 500:
raise HTTPException(503, 'NCBI is down, try again later')
return resp


# TODO: this does not return old assembly accessions, see https://github.com/ncbi/datasets/issues/380#issuecomment-2231142816
Expand Down Expand Up @@ -120,9 +123,9 @@ async def get_genbank_sequence(sequence_accession, start=None, end=None, strand=
elif resp.status_code == 400:
raise HTTPException(404, 'wrong sequence accession')
elif resp.status_code == 503:
raise HTTPException(503, 'NCBI returned an error')
raise HTTPException(503, 'NCBI returned an internal server error')
else:
raise HTTPException(500, 'NCBI returned an unexpected error')
raise HTTPException(503, 'NCBI returned an unexpected error')


def validate_coordinates_pre_request(start, end, strand):
Expand Down
12 changes: 12 additions & 0 deletions tests/test_endpoints_external_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,18 @@ def test_exceptions(self):
response = client.post('/genome_coordinates', json=viral_source.model_dump())
self.assertStatusCode(response.status_code, 400)

def test_ncbi_down(self):
correct_source = GenomeCoordinatesSource.model_validate(
request_examples.genome_region_examples['full']['value']
)
with respx.mock:
respx.get(
'https://api.ncbi.nlm.nih.gov/datasets/v2alpha/genome/accession/GCF_000002945.2/annotation_report'
).mock(return_value=httpx.Response(500, text='NCBI is down'))
response = client.post('/genome_coordinates', json=correct_source.model_dump())
self.assertEqual(response.status_code, 503)
self.assertIn('NCBI is down', response.json()['detail'])


class SEVASourceTest(unittest.TestCase):
def test_seva_url(self):
Expand Down
10 changes: 8 additions & 2 deletions tests/test_ncbi_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ async def test_get_genbank_sequence_subset(self):
with pytest.raises(HTTPException) as e:
await ncbi_requests.get_genbank_sequence('blah', 1, 10, 1)
assert e.value.status_code == 503
assert e.value.detail == 'NCBI returned an error'
assert e.value.detail == 'NCBI returned an internal server error'

respx.get('https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi').respond(500, text='')
with pytest.raises(HTTPException) as e:
await ncbi_requests.get_genbank_sequence('blah', 1, 10, 1)
assert e.value.status_code == 500
assert e.value.status_code == 503
assert e.value.detail == 'NCBI is down, try again later'

respx.get('https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi').respond(504, text='')
with pytest.raises(HTTPException) as e:
await ncbi_requests.get_genbank_sequence('blah', 1, 10, 1)
assert e.value.status_code == 503
assert e.value.detail == 'NCBI returned an unexpected error'

async def test_get_annotations_from_query(self):
Expand Down