Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Remove trailing slash from forwarded requests #159

Merged
merged 4 commits into from
Feb 13, 2025
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
36 changes: 24 additions & 12 deletions app/api/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
from . import utility as util


def build_node_request_urls(node_urls: list, path: str) -> list:
"""
Return a list of URLs for the current request for the specified set of Neurobagel nodes.
"""
node_request_urls = []
for node_url in node_urls:
node_request_urls.append(node_url + path)
return node_request_urls


def build_combined_response(
total_nodes: int, cross_node_results: list | dict, node_errors: list
) -> dict:
Expand Down Expand Up @@ -116,8 +126,8 @@ async def get(
params["pipeline_version"] = pipeline_version

tasks = [
util.send_get_request(node_url + "query", params, token)
for node_url in node_urls
util.send_get_request(node_request_url, params, token)
for node_request_url in build_node_request_urls(node_urls, "query")
]
responses = await asyncio.gather(*tasks, return_exceptions=True)

Expand All @@ -142,14 +152,14 @@ async def get(
)


async def get_instances(attribute_base_path: str):
async def get_instances(attribute_path: str):
"""
Makes a GET request to the root subpath of the specified attribute router of all available Neurobagel n-APIs.

Parameters
----------
attribute_base_path : str
Base path corresponding to a specific Neurobagel class for which all the available instances should be retrieved, e.g., "assessments"
attribute_path : str
Path corresponding to a specific Neurobagel class for which all the available instances should be retrieved, e.g., "assessments"

Returns
-------
Expand All @@ -160,14 +170,14 @@ async def get_instances(attribute_base_path: str):
unique_terms_dict = {}
# We want to always provide the URI of the requested attribute in a successful federated response,
# but cannot rely on it always being available in the node responses (e.g., if all nodes fail),
# so we define it locally based on the requested attribute base path.
attribute_uri = util.RESOURCE_URI_MAP[attribute_base_path]
# so we define it locally based on the requested attribute path.
attribute_uri = util.RESOURCE_URI_MAP[attribute_path]

tasks = [
util.send_get_request(
url=node_url + attribute_base_path + "/",
util.send_get_request(url=node_request_url)
for node_request_url in build_node_request_urls(
util.FEDERATION_NODES, attribute_path
)
for node_url in util.FEDERATION_NODES
]
responses = await asyncio.gather(*tasks, return_exceptions=True)

Expand Down Expand Up @@ -216,8 +226,10 @@ async def get_pipeline_versions(pipeline_term: str):
all_pipe_versions = []

tasks = [
util.send_get_request(f"{node_url}pipelines/{pipeline_term}/versions")
for node_url in util.FEDERATION_NODES
util.send_get_request(node_request_url)
for node_request_url in build_node_request_urls(
util.FEDERATION_NODES, f"pipelines/{pipeline_term}/versions"
)
]
responses = await asyncio.gather(*tasks, return_exceptions=True)

Expand Down
2 changes: 1 addition & 1 deletion app/api/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
],
}

# API resource names (base paths) and corresponding controlled terms for
# API resource names (paths) and corresponding controlled terms for
# Neurobagel attributes queryable using the API
RESOURCE_URI_MAP = {
"assessments": "nb:Assessment",
Expand Down
32 changes: 32 additions & 0 deletions tests/test_attribute_factory_routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import httpx
import pytest
from fastapi import status

from app.api import crud


def test_get_instances_with_duplicate_terms_handled(
test_app, monkeypatch, set_valid_test_federation_nodes
Expand Down Expand Up @@ -138,3 +141,32 @@ def test_fully_failed_get_instances_handled_gracefully(
assert response["nodes_response_status"] == "fail"
assert len(response["errors"]) == 2
assert response["responses"] == {"nb:Assessment": []}


@pytest.mark.parametrize(
"path",
[
"assessments",
"diagnoses",
"pipelines",
"query",
],
)
def test_node_request_urls_do_not_have_trailing_slash(path):
"""
Ensure that URLs used to forward requests to node APIs do not include a trailing slash.
"""
# TODO: Revisit once root_path is tested in production -
# the example node URLs below assume that validate_query_node_url_list has already been called,
# which will have appended trailing slashes to the node URLs if they were missing
node_request_urls = crud.build_node_request_urls(
node_urls=[
"https://api.node1.institute.org/",
"https://node2.institute.org/api/",
],
path=path,
)
assert node_request_urls == [
f"https://api.node1.institute.org/{path}",
f"https://node2.institute.org/api/{path}",
]
Loading