diff --git a/src/stringx/__init__.py b/src/stringx/__init__.py index aa0925f..1eeeb2c 100644 --- a/src/stringx/__init__.py +++ b/src/stringx/__init__.py @@ -11,17 +11,29 @@ def map(identifiers: list[str], species: int): with Client(identity=identity) as client: - return client.map(identifiers=identifiers, species=species) + return ( + client.map(identifiers=identifiers, species=species) + .raise_for_status() + .json() + ) def network(identifiers: list[str], species: int): with Client(identity=identity) as client: - return client.network(identifiers=identifiers, species=species) + return ( + client.network(identifiers=identifiers, species=species) + .raise_for_status() + .json() + ) def interaction_partners(identifiers: list[str], species: int): with Client(identity=identity) as client: - return client.interaction_partners(identifiers=identifiers, species=species) + return ( + client.interaction_partners(identifiers=identifiers, species=species) + .raise_for_status() + .json() + ) def version(): diff --git a/src/stringx/client.py b/src/stringx/client.py index d0a1026..2dc68e9 100644 --- a/src/stringx/client.py +++ b/src/stringx/client.py @@ -21,45 +21,39 @@ def __init__(self, identity: str, base_url: str = "https://string-db.org") -> No base_url=base_url, ) - def request( - self, - endpoint: str, - params: dict | None = None, - *, - method: str = "POST", - format: str = "json", - ): - if params is None: - params = {} - url = "/".join(["api", format, endpoint]) - response = super().request(method, url, params=params) - response.raise_for_status() - return response.json() - def map( self, identifiers: list[str], species: int, + *, limit: int = 1, echo_query: bool = True, + format: str | None = None, ): + url = f"api/{format or self.format}/get_string_ids" + params = { "identifiers": "\r".join(identifiers), # your protein list "species": species, # species NCBI identifier "limit": limit, # only one (best) identifier per input protein "echo_query": echo_query, # see your input identifiers in the output } - return self.request("get_string_ids", params=params) + + return self.post(url, params=params) def network( self, identifiers: list[str], species: int, + *, required_score: float | None = None, network_type: str = "functional", add_nodes: int | None = None, show_query_node_labels: bool = False, + format: str | None = None, ): + url = f"api/{format or self.format}/network" + params = { "identifiers": "\r".join(identifiers), "species": species, @@ -73,14 +67,18 @@ def network( if add_nodes: params |= {"add_nodes": add_nodes} - return self.request("network", params=params) + return self.post(url, params=params) def interaction_partners( self, identifiers: list[str], species: int, + *, limit: int | None = None, + format: str | None = None, ): + url = f"api/{format or self.format}/interaction_partners" + params = { "identifiers": "\r".join(identifiers), "species": species, @@ -89,7 +87,7 @@ def interaction_partners( if limit: params.update(limit=limit) - return self.request("interaction_partners", params=params) + return self.post(url, params=params) def homology( self, identifiers: list[str], species: int, *, format: str | None = None diff --git a/tests/test_client.py b/tests/test_client.py index 1d1e6fd..b750fe0 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -49,47 +49,52 @@ def test_default_format(test_client): assert test_client.format == "json" -def test_map(httpx_mock, test_client): +@pytest.mark.parametrize("format", ["tsv", "tsv-no-header", "json", "xml"]) +def test_map(httpx_mock, test_client, format): httpx_mock.add_response( url=httpx.URL( - "https://string-db.org/api/json/get_string_ids", + f"https://string-db.org/api/{format}/get_string_ids", params={ "identifiers": "some_identifier", - "species": "7227", + "species": 1234, "limit": 1, "echo_query": True, "caller_identity": test_client.identity, }, ), method="POST", - json=True, ) - test_client.map(["some_identifier"], 7227) + test_client.map(["some_identifier"], 1234, format=format) -def test_network(httpx_mock, test_client): +@pytest.mark.parametrize( + "format", ["tsv", "tsv-no-header", "json", "xml", "psi-mi", "psi-mi-tab"] +) +def test_network(httpx_mock, test_client, format): + identifiers = ["id1"] + species = 1234 + httpx_mock.add_response( url=httpx.URL( - "https://string-db.org/api/json/network", + f"https://string-db.org/api/{format}/network", params={ - "identifiers": "id1", - "species": "7227", + "identifiers": identifiers, + "species": species, "network_type": "functional", "show_query_node_labels": 0, "caller_identity": test_client.identity, }, ), method="POST", - json=True, ) httpx_mock.add_response( url=httpx.URL( - "https://string-db.org/api/json/network", + f"https://string-db.org/api/{format}/network", params={ - "identifiers": "id1\rid2", - "species": "7227", + "identifiers": identifiers, + "species": species, "required_score": 1, "network_type": "physical", "add_nodes": 2, @@ -101,24 +106,31 @@ def test_network(httpx_mock, test_client): json=True, ) - test_client.network(["id1"], 7227) + test_client.network(identifiers, species, format=format) test_client.network( - identifiers=["id1", "id2"], - species=7227, + identifiers=identifiers, + species=species, required_score=1, network_type="physical", add_nodes=2, show_query_node_labels=True, + format=format, ) -def test_interaction_partners(httpx_mock, test_client): +@pytest.mark.parametrize( + "format", ["tsv", "tsv-no-header", "json", "xml", "psi-mi", "psi-mi-tab"] +) +def test_interaction_partners(httpx_mock, test_client, format): + identifiers = ["id1"] + species = 1234 + httpx_mock.add_response( url=httpx.URL( - "https://string-db.org/api/json/interaction_partners", + f"https://string-db.org/api/{format}/interaction_partners", params={ - "identifiers": "id1\rid2", - "species": "7227", + "identifiers": identifiers, + "species": species, "caller_identity": test_client.identity, }, ), @@ -126,25 +138,27 @@ def test_interaction_partners(httpx_mock, test_client): json=True, ) - test_client.interaction_partners(["id1", "id2"], 7227) + test_client.interaction_partners(identifiers, species, format=format) @pytest.mark.parametrize("format", ["tsv", "tsv-no-header", "json", "xml"]) def test_homology(httpx_mock, test_client, format): - identifiers = ["id1", "id2"] + identifiers = ["id1"] species = 1234 - httpx_mock.add_response() + httpx_mock.add_response( + url=httpx.URL( + f"https://string-db.org/api/{format}/homology", + params={ + "identifiers": identifiers, + "species": species, + "caller_identity": test_client.identity, + }, + ), + ) test_client.homology(identifiers, species, format=format) - requested_url = httpx_mock.get_request().url - - assert requested_url.path == f"/api/{format}/homology" - assert requested_url.params["identifiers"] == "\r".join(identifiers) - assert requested_url.params["species"] == str(species) - assert requested_url.params["caller_identity"] == test_client.identity - def test_version(httpx_mock, test_client): httpx_mock.add_response( diff --git a/tests/test_stringx.py b/tests/test_stringx.py index 44b5002..6f6cb1f 100644 --- a/tests/test_stringx.py +++ b/tests/test_stringx.py @@ -24,62 +24,16 @@ def test_valid_identity(httpx_mock): ) -def test_map(): - identifiers = stringx.map(["edin"], 7227) +def test_map(httpx_mock): + httpx_mock.add_response(json=True) + stringx.map(["id1"], 1234) - assert len(identifiers) == 1 - for id_ in identifiers: - keys = id_.keys() - assert "queryIndex" in keys - assert "queryItem" in keys - assert "stringId" in keys - assert "ncbiTaxonId" in keys - assert "taxonName" in keys - assert "preferredName" in keys - assert "annotation" in keys - assert "annotation" in keys +def test_network(httpx_mock): + httpx_mock.add_response(json=True) + stringx.network(["id1"], 1234) -def test_network(): - network = stringx.network(["edin", "atta", "attc"], 7227) - - assert len(network) == 3 - - for id_ in network: - keys = id_.keys() - assert "stringId_A" in keys - assert "stringId_B" in keys - assert "preferredName_A" in keys - assert "preferredName_B" in keys - assert "ncbiTaxonId" in keys - assert "score" in keys - assert "nscore" in keys - assert "fscore" in keys - assert "pscore" in keys - assert "ascore" in keys - assert "escore" in keys - assert "dscore" in keys - assert "tscore" in keys - - -def test_interaction_partners(): - interaction_partners = stringx.interaction_partners(["edin"], 7227) - - assert len(interaction_partners) > 1 - - for id_ in interaction_partners: - keys = id_.keys() - assert "stringId_A" in keys - assert "stringId_B" in keys - assert "preferredName_A" in keys - assert "preferredName_B" in keys - assert "ncbiTaxonId" in keys - assert "score" in keys - assert "nscore" in keys - assert "fscore" in keys - assert "pscore" in keys - assert "ascore" in keys - assert "escore" in keys - assert "dscore" in keys - assert "tscore" in keys +def test_interaction_partners(httpx_mock): + httpx_mock.add_response(json=True) + stringx.interaction_partners(["id1"], 1234)