Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.
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
19 changes: 10 additions & 9 deletions cads_api_client/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import cads_api_client

from . import catalogue, config, processing, profile
from . import __version__, catalogue, config, processing, profile


@attrs.define(slots=False)
Expand Down Expand Up @@ -69,11 +69,12 @@ def __attrs_post_init__(self) -> None:
warnings.warn(str(exc), UserWarning)

def _get_headers(self, key_is_mandatory: bool = True) -> dict[str, str]:
if self.key is None:
if key_is_mandatory:
raise ValueError("The API key is needed to access this resource")
return {}
return {"PRIVATE-TOKEN": self.key}
headers = {"User-Agent": f"cads-api-client/{__version__}"}
if self.key is not None:
headers["PRIVATE-TOKEN"] = self.key
elif key_is_mandatory:
raise ValueError("The API key is needed to access this resource")
return headers

@property
def _retry_options(self) -> dict[str, Any]:
Expand All @@ -99,10 +100,10 @@ def _request_options(self) -> dict[str, Any]:
}

def _get_request_kwargs(
self, mandatory_key: bool = True
self, key_is_mandatory: bool = True
) -> processing.RequestKwargs:
return processing.RequestKwargs(
headers=self._get_headers(key_is_mandatory=mandatory_key),
headers=self._get_headers(key_is_mandatory=key_is_mandatory),
session=self.session,
retry_options=self._retry_options,
request_options=self._request_options,
Expand All @@ -116,7 +117,7 @@ def _get_request_kwargs(
def _catalogue_api(self) -> catalogue.Catalogue:
return catalogue.Catalogue(
f"{self.url}/catalogue",
**self._get_request_kwargs(mandatory_key=False),
**self._get_request_kwargs(key_is_mandatory=False),
)

@functools.cached_property
Expand Down
2 changes: 2 additions & 0 deletions tests/integration_test_60_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ def test_api_client_get_process(api_anon_client: ApiClient) -> None:
process = api_anon_client.get_process("test-adaptor-dummy")
assert isinstance(process, processing.Process)
assert process.id == "test-adaptor-dummy"
assert set(process.headers) == {"User-Agent", "PRIVATE-TOKEN"}


def test_api_client_get_remote(api_anon_client: ApiClient) -> None:
request_uid = api_anon_client.submit("test-adaptor-dummy").request_uid
remote = api_anon_client.get_remote(request_uid)
assert remote.request_uid == request_uid
assert set(remote.headers) == {"User-Agent", "PRIVATE-TOKEN"}


def test_api_client_get_results(api_anon_client: ApiClient) -> None:
Expand Down