Skip to content

Commit

Permalink
fix: Update NBIA toolkit dependencies and fix base URL references.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjjermiah committed Feb 25, 2024
1 parent 1c75711 commit f0f8824
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 21 deletions.
10 changes: 5 additions & 5 deletions src/nbiatoolkit/auth.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import requests
import time
from typing import Union, Tuple
from .utils import NBIA_ENDPOINTS
from .utils import NBIA_ENDPOINTS, NBIA_BASE_URLS
from cryptography.fernet import Fernet


Expand Down Expand Up @@ -113,7 +113,7 @@ def __init__(
username: str = "nbia_guest",
password: str = "",
client_id: str = "NBIA",
base_url: Union[str, NBIA_ENDPOINTS] = NBIA_ENDPOINTS.NBIA,
base_url: str | NBIA_BASE_URLS = NBIA_BASE_URLS.NBIA,
) -> None:
"""
Initialize the OAuth2 class.
Expand All @@ -127,7 +127,7 @@ def __init__(
The password for authentication. Default is an empty string.
client_id : str, optional
The client ID for authentication. Default is "NBIA".
base_url : str or NBIA_ENDPOINTS, optional. Default is NBIA_ENDPOINTS.NBIA
base_url : str or NBIA_BASE_URLS, optional. Default is NBIA_BASE_URLS.NBIA
"""

Expand All @@ -143,7 +143,7 @@ def __init__(
key=self._fernet_key, username=username, password=password
)

if isinstance(base_url, NBIA_ENDPOINTS):
if isinstance(base_url, NBIA_BASE_URLS):
self.base_url = base_url.value
else:
self.base_url = base_url
Expand Down Expand Up @@ -292,7 +292,7 @@ def logout(self) -> None:
if not self.access_token:
return None

query_url = NBIA_ENDPOINTS.LOGOUT_URL.value
query_url = NBIA_BASE_URLS.LOGOUT_URL.value
response = requests.get(query_url, headers=self.api_headers)
response.raise_for_status()

Expand Down
23 changes: 14 additions & 9 deletions src/nbiatoolkit/nbia.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from logging import Logger
from .utils import (
NBIA_ENDPOINTS,
NBIA_BASE_URLS,
validateMD5,
clean_html,
convertMillis,
Expand Down Expand Up @@ -64,7 +65,7 @@ def downloadSingleSeries(
filePattern: str,
overwrite: bool,
api_headers: dict[str, str],
base_url: NBIA_ENDPOINTS,
base_url: NBIA_BASE_URLS,
log: Logger,
):
"""
Expand Down Expand Up @@ -123,12 +124,16 @@ def downloadSingleSeries(


class NBIAClient:
"""
The NBIAClient class is a wrapper around the NBIA REST API. It provides
methods to query the API and download series.
"""A client for interacting with the NBIA API.
The NBIAClient class provides a high-level interface for querying the NBIA API and downloading DICOM series.
The default authentication uses the guest account. If you have a username
and password, you can pass them to the constructor.
Attributes:
OAuth_client (OAuth2): The OAuth2 client used for authentication.
headers (dict[str, str]): The API headers.
base_url (NBIA_ENDPOINTS): The base URL for API requests.
logger (Logger): The logger for logging client events.
return_type (str): The current return type for API responses.
"""

def __init__(
Expand All @@ -152,7 +157,7 @@ def __init__(
"Content-Type": "application/json",
}

self._base_url: NBIA_ENDPOINTS = NBIA_ENDPOINTS.NBIA
self._base_url: NBIA_BASE_URLS = NBIA_BASE_URLS.NBIA
self._return_type: ReturnType = (
return_type
if isinstance(return_type, ReturnType)
Expand All @@ -175,11 +180,11 @@ def headers(self):

# create a setter for the base_url in case user want to use NLST
@property
def base_url(self) -> NBIA_ENDPOINTS:
def base_url(self) -> NBIA_BASE_URLS:
return self._base_url

@base_url.setter
def base_url(self, nbia_url: NBIA_ENDPOINTS) -> None:
def base_url(self, nbia_url: NBIA_BASE_URLS) -> None:
self._base_url = nbia_url

@property
Expand Down
3 changes: 2 additions & 1 deletion src/nbiatoolkit/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .nbia_endpoints import NBIA_ENDPOINTS
from .nbia_endpoints import NBIA_ENDPOINTS, NBIA_BASE_URLS
from .md5 import validateMD5
from .parsers import (
convertMillis,
Expand All @@ -10,6 +10,7 @@

__all__ = [
"NBIA_ENDPOINTS",
"NBIA_BASE_URLS",
"validateMD5",
"convertMillis",
"clean_html",
Expand Down
17 changes: 15 additions & 2 deletions src/nbiatoolkit/utils/nbia_endpoints.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
from enum import Enum


class NBIA_ENDPOINTS(Enum):
class NBIA_BASE_URLS(Enum):
"""
This enum class defines the NBIA endpoints used in the NBIA toolkit.
This enum class defines the NBIA base URLs used in the NBIA toolkit.
"""

NBIA = "https://services.cancerimagingarchive.net/nbia-api/services/"
NLST = "https://nlst.cancerimagingarchive.net/nbia-api/services/"
LOGOUT_URL = "https://services.cancerimagingarchive.net/nbia-api/logout"

# Helper functions
def __str__(self):
return self.value

Check warning on line 15 in src/nbiatoolkit/utils/nbia_endpoints.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/utils/nbia_endpoints.py#L15

Added line #L15 was not covered by tests

def _format(self):
return self.value.split("/")[-1]

Check warning on line 18 in src/nbiatoolkit/utils/nbia_endpoints.py

View check run for this annotation

Codecov / codecov/patch

src/nbiatoolkit/utils/nbia_endpoints.py#L18

Added line #L18 was not covered by tests


class NBIA_ENDPOINTS(Enum):
"""
This enum class defines the NBIA endpoints used in the NBIA toolkit.
"""

GET_COLLECTIONS = "v2/getCollectionValues"
GET_COLLECTION_PATIENT_COUNT = "getCollectionValuesAndCounts"
GET_COLLECTION_DESCRIPTIONS = "getCollectionDescriptions"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_new_query_functionality.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_nbia_properties(nbia_client2):
assert "Content-Type" in nbia_client.headers.keys()
assert nbia_client.headers["Content-Type"] == "application/json"

assert nbia_client.base_url == NBIA_ENDPOINTS.NBIA
assert nbia_client.base_url == NBIA_BASE_URLS.NBIA

assert nbia_client.logger is not None

Expand Down
6 changes: 3 additions & 3 deletions tests/test_tcga_collections_separate.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def test_nbia_properties(nbia_context_manager):


def test_break(nbia_client_tobreak):
assert nbia_client_tobreak.base_url == NBIA_ENDPOINTS.NBIA
nbia_client_tobreak.base_url = NBIA_ENDPOINTS.NLST
assert nbia_client_tobreak.base_url == NBIA_ENDPOINTS.NLST
assert nbia_client_tobreak.base_url == NBIA_BASE_URLS.NBIA
nbia_client_tobreak.base_url = NBIA_BASE_URLS.NLST
assert nbia_client_tobreak.base_url == NBIA_BASE_URLS.NLST


def test_getModalityValues(nbia_client, tcga_collections):
Expand Down

0 comments on commit f0f8824

Please sign in to comment.