Skip to content

Commit 3d7c58a

Browse files
author
Chiara Rasi
committed
Revert validate and add another endpoint to retrieve errors from testapi
1 parent d59cb0d commit 3d7c58a

File tree

3 files changed

+81
-65
lines changed

3 files changed

+81
-65
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## [unreleased]
2-
### Changed
3-
- Expand the output of validate endpoint to return the link for downloading json file containing evantual submission errors
2+
### Added
3+
- An endpoint `/apitest-status` that returns a response containing a link to the eventual json file with the submissions data errors
44

55
## [2.5.2]
66
### Fixed

preClinVar/main.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,32 @@ async def root():
3333
return {"message": f"preClinVar v{VERSION} is up and running!"}
3434

3535

36+
@app.post("/apitest-status")
37+
async def apitest_status(api_key: str = Form(), submission_id: str = Form()) -> JSONResponse:
38+
"""Returns the status (validation) of a test submission to the apitest endpoint."""
39+
40+
# Create a submission header
41+
header = build_header(api_key)
42+
43+
apitest_actions_url = f"{VALIDATE_SUBMISSION_URL}/{submission_id}/actions/"
44+
apitest_actions_resp = requests.get(apitest_actions_url, headers=header)
45+
46+
return JSONResponse(
47+
status_code=apitest_actions_resp.status_code,
48+
content=apitest_actions_resp.json(),
49+
)
50+
51+
3652
@app.post("/validate")
37-
async def validate(api_key: str = Form(), json_file: UploadFile = File(...)) -> JSONResponse:
38-
"""A proxy to the apitest submission ClinVar API endpoint. Returns the ID of the submission and the data summary report with eventual errors."""
53+
async def validate(api_key: str = Form(), json_file: UploadFile = File(...)):
54+
"""A proxy to the apitest ClinVar API endpoint"""
3955
# Create a submission header
4056
header = build_header(api_key)
4157

4258
# Get json file content as dict:
4359
submission_obj = json.load(json_file.file)
4460

45-
# And use it as data in a POST request to API
61+
# And use it in POST request to API
4662
data = {
4763
"actions": [
4864
{
@@ -52,23 +68,10 @@ async def validate(api_key: str = Form(), json_file: UploadFile = File(...)) ->
5268
}
5369
]
5470
}
55-
apitest_resp = requests.post(VALIDATE_SUBMISSION_URL, data=json.dumps(data), headers=header)
56-
apitest_json: dict = apitest_resp.json()
57-
58-
# If submission was created, return eventual link for downloading json file with data-specific errors
59-
if apitest_resp.status_code == 201:
60-
61-
apitest_actions_url = f"{VALIDATE_SUBMISSION_URL}/{apitest_json.get('id')}/actions/"
62-
apitest_actions_resp = requests.get(apitest_actions_url, headers=header)
63-
64-
return JSONResponse(
65-
status_code=apitest_actions_resp.status_code,
66-
content=apitest_actions_resp.json(),
67-
)
68-
71+
resp = requests.post(VALIDATE_SUBMISSION_URL, data=json.dumps(data), headers=header)
6972
return JSONResponse(
70-
status_code=apitest_resp.status_code,
71-
content=apitest_json,
73+
status_code=resp.status_code,
74+
content=resp.json(),
7275
)
7376

7477

tests/test_main.py

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -312,56 +312,69 @@ def test_validate_wrong_api_key():
312312

313313

314314
@responses.activate
315-
def test_validate_error():
316-
"""Test the validated API proxy endpoint (with a mocked ClinVar API response)"""
315+
def test_validate():
316+
"""Test the endpoint validate, a proxy to ClinVar apitest, with a mocked ClinVar API response."""
317317

318-
# GIVEN a mocked POST response from CLinVar apitest endpoint
318+
# GIVEN a json submission file
319+
json_file = {"json_file": open(subm_json_path, "rb")}
320+
321+
# AND a mocked ClinVar API
319322
responses.add(
320323
responses.POST,
321324
VALIDATE_SUBMISSION_URL,
322325
json={"id": DEMO_SUBMISSION_ID},
323-
status=201, # The ClinVar API returns code 201 when request is successful (created)
324-
)
325-
326-
# GIVEN a mocked error response from apitest actions endpoint
327-
actions: list[dict] = [
328-
{
329-
"id": "SUB14404390-1",
330-
"targetDb": "clinvar-test",
331-
"status": "error",
332-
"updated": "2024-04-26T06:41:04.533900Z",
333-
"responses": [
334-
{
335-
"status": "error",
336-
"message": {
337-
"severity": "error",
338-
"errorCode": "2",
339-
"text": 'Your ClinVar submission processing status is "Error". Please find the details in the file referenced by actions[0].responses[0].files[0].url.',
340-
},
341-
"files": [
342-
{
343-
"url": "https://submit.ncbi.nlm.nih.gov/api/2.0/files/vxgc6vtt/sub14404390-summary-report.json/?format=attachment"
344-
}
345-
],
346-
"objects": [],
347-
}
348-
],
349-
}
350-
]
351-
352-
responses.add(
353-
responses.GET,
354-
f"{VALIDATE_SUBMISSION_URL}/{DEMO_SUBMISSION_ID}/actions/",
355-
json={"actions": actions},
356-
status=200, # The ClinVar API returns code 201 when request is successful (created)
326+
status=201, # The ClinVar API returs code 201 when request is successful (created)
357327
)
358328

359-
# GIVEN a json submission file
360-
json_file = {"json_file": open(subm_json_path, "rb")}
361-
362329
response = client.post("/validate", data={"api_key": DEMO_API_KEY}, files=json_file)
363330

364-
# THEN the ClinVar API proxy should return the expected data
365-
assert response.status_code == 200
366-
assert response.json()["actions"][0]["id"]
367-
assert response.json()["actions"][0]["responses"][0]["files"]
331+
# THEN the ClinVar API proxy should return "success"
332+
assert response.status_code == 201 # Created
333+
assert response.json()["id"] == DEMO_SUBMISSION_ID
334+
335+
@responses.activate
336+
def test_apitest_status():
337+
"""Test the endpoint that sends GET requests to the apitest actions ClinVar endpoint."""
338+
339+
# GIVEN a mocked error response from apitest actions endpoint
340+
actions: list[dict] = [
341+
{
342+
"id": "SUB14404390-1",
343+
"targetDb": "clinvar-test",
344+
"status": "error",
345+
"updated": "2024-04-26T06:41:04.533900Z",
346+
"responses": [
347+
{
348+
"status": "error",
349+
"message": {
350+
"severity": "error",
351+
"errorCode": "2",
352+
"text": 'Your ClinVar submission processing status is "Error". Please find the details in the file referenced by actions[0].responses[0].files[0].url.',
353+
},
354+
"files": [
355+
{
356+
"url": "https://submit.ncbi.nlm.nih.gov/api/2.0/files/vxgc6vtt/sub14404390-summary-report.json/?format=attachment"
357+
}
358+
],
359+
"objects": [],
360+
}
361+
],
362+
}
363+
]
364+
365+
responses.add(
366+
responses.GET,
367+
f"{VALIDATE_SUBMISSION_URL}/{DEMO_SUBMISSION_ID}/actions/",
368+
json={"actions": actions},
369+
status=200, # The ClinVar API returns code 201 when request is successful (created)
370+
)
371+
372+
# GIVEN a call to the apitest_status endpoint
373+
response = client.post(
374+
"/validate", data={"api_key": DEMO_API_KEY, "submission_id": DEMO_SUBMISSION_ID}
375+
)
376+
377+
# THEN the response should contain the provided actions
378+
assert response.status_code == 200
379+
assert response.json()["actions"][0]["id"]
380+
assert response.json()["actions"][0]["responses"][0]["files"]

0 commit comments

Comments
 (0)