Skip to content

Commit

Permalink
Implement version endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
cachitas committed Apr 9, 2024
1 parent b37ef16 commit 1fd9735
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/stringx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
27 changes: 24 additions & 3 deletions src/stringx/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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)
67 changes: 61 additions & 6 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1fd9735

Please sign in to comment.