From 1fd9735793a2e3c607d7b80793d66482c07657f2 Mon Sep 17 00:00:00 2001 From: Hugo Cachitas Date: Tue, 9 Apr 2024 01:06:09 +0100 Subject: [PATCH] Implement version endpoint --- src/stringx/__init__.py | 4 +-- src/stringx/client.py | 27 +++++++++++++++-- tests/test_client.py | 67 +++++++++++++++++++++++++++++++++++++---- 3 files changed, 87 insertions(+), 11 deletions(-) diff --git a/src/stringx/__init__.py b/src/stringx/__init__.py index 70dff7c..5b73aba 100644 --- a/src/stringx/__init__.py +++ b/src/stringx/__init__.py @@ -99,6 +99,6 @@ def enrichment( ) -def version(): +def get_version() -> typing.Any: with Client(identity=identity) as client: - return client.version() + return client.get_version().raise_for_status().json() diff --git a/src/stringx/client.py b/src/stringx/client.py index 0cc5d1d..8ca010f 100644 --- a/src/stringx/client.py +++ b/src/stringx/client.py @@ -23,6 +23,27 @@ def __init__(self, identity: str, base_url: str = "https://string-db.org") -> No base_url=base_url, ) + def _update_metadata(self) -> None: + data = self.get_version().json() + self._version: str = data[0]["string_version"] + self._stable_address: str = data[0]["stable_address"] + + @property + def version(self) -> str: + try: + return self._version + except AttributeError: + self._update_metadata() + return self._version + + @property + def stable_address(self) -> str: + try: + return self._stable_address + except AttributeError: + self._update_metadata() + return self._stable_address + def map( self, identifiers: list[str], @@ -124,7 +145,7 @@ def enrichment( return self.post(url, params=params) - def version(self) -> str: - request = self.build_request("GET", "api/json/version") + def get_version(self, *, format: str | None = None) -> httpx.Response: + request = self.build_request("GET", f"api/{format or self.format}/version") request.url = request.url.copy_remove_param("caller_identity") - return self.send(request).json() + return self.send(request) diff --git a/tests/test_client.py b/tests/test_client.py index 4cf8e28..ad01a45 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -186,16 +186,71 @@ def test_enrichment(httpx_mock, test_client, format): ) -def test_version(httpx_mock, test_client): +@pytest.mark.parametrize("format", ["tsv", "tsv-no-header", "json", "xml"]) +def test_get_version(httpx_mock, test_client, format): httpx_mock.add_response( - url=httpx.URL("https://string-db.org/api/json/version"), + url=httpx.URL(f"https://string-db.org/api/{format}/version"), method="GET", + ) + assert test_client.get_version(format=format) + + +def test_metadata(httpx_mock, test_client): + version = "12.0" + stable_address = "https://version-12-0.string-db.org" + + httpx_mock.add_response( json=[ { - "string_version": "12.0", - "stable_address": "https://version-12-0.string-db.org", + "string_version": version, + "stable_address": stable_address, } - ], + ] ) + assert test_client.version == version + assert test_client.stable_address == stable_address + + +def test_client_metadata_is_independent(httpx_mock): + version = "12.0" + stable_address = "https://version-12-0.string-db.org" + + httpx_mock.add_response( + json=[ + { + "string_version": version, + "stable_address": stable_address, + } + ] + ) + + with stringx.Client("test") as client: + assert client.version == version + + with stringx.Client("test") as client: + assert client.stable_address == stable_address + + assert len(httpx_mock.get_requests()) == 2 + + +def test_metadata_is_saved_for_subsequent_requests(httpx_mock): + version = "12.0" + stable_address = "https://version-12-0.string-db.org" + + httpx_mock.add_response( + json=[ + { + "string_version": version, + "stable_address": stable_address, + } + ] + ) + + with stringx.Client("test") as client: + assert client.version == version + assert client.version == version + assert client.stable_address == stable_address + assert client.stable_address == stable_address - test_client.version() + # multiple property calls should only result in a single call to the API + assert len(httpx_mock.get_requests()) == 1