Skip to content

Commit

Permalink
ensure token is forwarded in request to n-API (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssadai authored Oct 31, 2024
1 parent d29cae8 commit 6f44b81
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
5 changes: 4 additions & 1 deletion app/api/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ async def get(
pipeline_name: str,
pipeline_version: str,
node_urls: list[str],
token: str | None = None,
) -> dict:
"""
Makes GET requests to one or more Neurobagel node APIs using send_get_request utility function where the parameters are Neurobagel query parameters.
Expand Down Expand Up @@ -75,6 +76,8 @@ async def get(
Version of pipeline run on subject scans.
node_urls : list[str]
List of Neurobagel nodes to send the query to.
token : str, optional
Google ID token for authentication, by default None
Returns
-------
Expand Down Expand Up @@ -113,7 +116,7 @@ async def get(
params["pipeline_version"] = pipeline_version

tasks = [
util.send_get_request(node_url + "query", params)
util.send_get_request(node_url + "query", params, token)
for node_url in node_urls
]
responses = await asyncio.gather(*tasks, return_exceptions=True)
Expand Down
1 change: 1 addition & 0 deletions app/api/routers/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ async def get_query(
query.pipeline_name,
query.pipeline_version,
query.node_url,
token,
)

if response_dict["errors"]:
Expand Down
12 changes: 11 additions & 1 deletion app/api/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ def validate_query_node_url_list(node_urls: list) -> list:


async def send_get_request(
url: str, params: list = None, timeout: float = None
url: str,
params: list | None = None,
token: str | None = None,
timeout: float | None = None,
) -> dict:
"""
Makes a GET request to one or more Neurobagel nodes.
Expand All @@ -239,6 +242,8 @@ async def send_get_request(
URL of Neurobagel node API.
params : list, optional
Neurobagel query parameters, by default None.
token : str, optional
Authorization token for the request, by default None.
timeout : float, optional
Timeout for the request, by default None.
Expand All @@ -254,10 +259,15 @@ async def send_get_request(
_description_
"""
async with httpx.AsyncClient() as client:
headers = {
"Content-Type": "application/json",
**({"Authorization": f"Bearer {token}"} if token else {}),
}
try:
response = await client.get(
url=url,
params=params,
headers=headers,
timeout=timeout,
# Enable redirect following (off by default) so
# APIs behind a proxy can be reached
Expand Down

0 comments on commit 6f44b81

Please sign in to comment.