-
Notifications
You must be signed in to change notification settings - Fork 5
/
crud.py
68 lines (58 loc) · 1.56 KB
/
crud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""CRUD functions called by path operations."""
import os
import httpx
from fastapi import HTTPException
from . import utility as util
async def get(
age_min: float,
age_max: float,
sex: str,
diagnosis: str,
is_control: bool,
image_modal: str,
):
"""
Makes a POST request to Stardog API using httpx where the payload is a SPARQL query generated by the create_query function.
Parameters
----------
age_min : float
Minimum age of subject.
age_max : float
Maximum age of subject.
sex : str
Sex of subject.
diagnosis : str
Subject diagnosis.
is_control : bool
Whether or not subject is a control.
image_modal : str
Imaging modality of subject scans.
Returns
-------
httpx.response
Response of the POST request.
"""
response = httpx.post(
url=util.QUERY_URL,
content=util.create_query(
age=(age_min, age_max),
sex=sex,
diagnosis=diagnosis,
is_control=is_control,
image_modal=image_modal,
),
headers=util.QUERY_HEADER,
auth=httpx.BasicAuth(
os.environ.get("USER"), os.environ.get("PASSWORD")
),
)
if not response.is_success:
raise HTTPException(
status_code=response.status_code,
detail=f"{response.reason_phrase}: {response.text}",
)
results = response.json()
return [
{k: v["value"] for k, v in res.items()}
for res in results["results"]["bindings"]
]