diff --git a/pyproject.toml b/pyproject.toml index 8ff0635..3f38a5e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,9 +10,18 @@ dependencies = ["httpx>=0.27.0"] requires-python = ">=3.9" readme = "README.md" keywords = [ - "string", "api", "client", "httpx", - "sib", "cpr", "embl", "biodata", "elixir", - "protein", "gene", "interaction", + "string", + "api", + "client", + "httpx", + "sib", + "cpr", + "embl", + "biodata", + "elixir", + "protein", + "gene", + "interaction", ] classifiers = [ "Development Status :: 4 - Beta", @@ -40,15 +49,8 @@ Repository = "https://github.com/cachitas/stringx" Issues = "https://github.com/cachitas/stringx/issues" [tool.pdm.dev-dependencies] -dev = [ - "pytest>=8.1.1", - "pytest-httpx>=0.30.0", - "ruff>=0.3.5", -] -examples = [ - "pandas>=2.2.1", - "ipykernel>=6.29.4", -] +dev = ["pytest>=8.1.1", "pytest-httpx>=0.30.0", "ruff>=0.3.5"] +examples = ["pandas>=2.2.1", "ipykernel>=6.29.4"] [tool.pdm.version] source = "file" @@ -56,13 +58,13 @@ path = "src/stringx/__init__.py" [tool.ruff] lint.select = [ - "B", # flake8-bugbear + "B", # flake8-bugbear "C4", # flake8-comprehensions - "E", # pycodestyle - Error - "F", # Pyflakes - "I", # isort - "W", # pycodestyle - Warning + "E", # pycodestyle - Error + "F", # Pyflakes + "I", # isort + "W", # pycodestyle - Warning "UP", # pyupgrade ] -include = ["**/*.py", "**/*.pyi", "**/pyproject.toml"] +extend-include = ["*.ipynb"] diff --git a/src/stringx/__init__.py b/src/stringx/__init__.py index 60d684d..72c508b 100644 --- a/src/stringx/__init__.py +++ b/src/stringx/__init__.py @@ -4,7 +4,6 @@ __version__ = "0.3.0" -from typing import List from .client import Client @@ -13,16 +12,21 @@ DEFAULT_CALLER_IDENTITY = f"{__name__} {__version__}" -def map(identifiers: List[str], species: int): +def map(identifiers: list[str], species: int): with Client() as client: return client.map(identifiers=identifiers, species=species) -def network(identifiers: List[str], species: int): +def network(identifiers: list[str], species: int): with Client() as client: return client.network(identifiers=identifiers, species=species) -def interaction_partners(identifiers: List[str], species: int): +def interaction_partners(identifiers: list[str], species: int): with Client() as client: return client.interaction_partners(identifiers=identifiers, species=species) + + +def version(): + with Client() as client: + return client.version() diff --git a/src/stringx/client.py b/src/stringx/client.py index baa4d40..620f445 100644 --- a/src/stringx/client.py +++ b/src/stringx/client.py @@ -1,41 +1,43 @@ import logging -from typing import List, Optional import httpx - logger = logging.getLogger(__name__) class Client(httpx.Client): def __init__( - self, base_url: str = "https://string-db.org", *, identity: Optional[str] = None + self, base_url: str = "https://string-db.org", *, identity: str | None = None ) -> None: from stringx import DEFAULT_CALLER_IDENTITY super().__init__( - params=dict(caller_identity=identity or DEFAULT_CALLER_IDENTITY), + params={"caller_identity": identity or DEFAULT_CALLER_IDENTITY}, base_url=base_url, ) def request( self, endpoint: str, - params: dict = {}, + params: dict | None = None, *, method: str = "POST", format: str = "json", ): + if params is None: + params = {} url = "/".join(["api", format, endpoint]) logger.info("POST", url, params) + print("params BEFORE request", self.params) response = super().request(method, url, params=params) + print("params AFTER request", self.params) logger.info(response.status_code) response.raise_for_status() return response.json() def map( self, - identifiers: List[str], + identifiers: list[str], species: int, limit: int = 1, echo_query: bool = True, @@ -50,19 +52,19 @@ def map( def network( self, - identifiers: List[str], + identifiers: list[str], species: int, - required_score: Optional[float] = None, + required_score: float | None = None, network_type: str = "functional", - add_nodes: Optional[int] = None, + add_nodes: int | None = None, show_query_node_labels: bool = False, ): - params = dict( - identifiers="\r".join(identifiers), - species=species, - network_type=network_type, - show_query_node_labels=int(show_query_node_labels), - ) + params = { + "identifiers": "\r".join(identifiers), + "species": species, + "network_type": network_type, + "show_query_node_labels": int(show_query_node_labels), + } if required_score: params |= {"required_score": required_score} @@ -74,16 +76,24 @@ def network( def interaction_partners( self, - identifiers: List[str], + identifiers: list[str], species: int, - limit: Optional[int] = None, + limit: int | None = None, ): - params = dict( - identifiers="\r".join(identifiers), - species=species, - ) + params = { + "identifiers": "\r".join(identifiers), + "species": species, + } if limit: params.update(limit=limit) return self.request("interaction_partners", params=params) + + def version(self): + print(self.params) + self.params = {} + request = self.build_request("GET", "https://api.example.com") + del request.headers["X-Api-Key"] + + return self.request("version", params={}) diff --git a/tests/test_client.py b/tests/test_client.py index d2d49ca..0e5efeb 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,5 +1,4 @@ import httpx - import stringx @@ -116,3 +115,16 @@ def test_interaction_partners(httpx_mock): with stringx.Client() as client: client.interaction_partners(["id1", "id2"], 7227) + + +def test_version(httpx_mock): + httpx_mock.add_response( + url=httpx.URL("https://string-db.org/api/json/version"), method="POST", json={} + ) + with stringx.Client() as client: + version = client.version() + version = client.map(["edin"], species=7227) + version = client.version() + + print(version) + assert version == ""