Skip to content

Commit

Permalink
Rework Client endpoints implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cachitas committed Apr 4, 2024
1 parent 15010e8 commit 4f21bd3
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 106 deletions.
18 changes: 15 additions & 3 deletions src/stringx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
34 changes: 16 additions & 18 deletions src/stringx/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
Expand Down
74 changes: 44 additions & 30 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -101,50 +106,59 @@ 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,
},
),
method="POST",
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(
Expand Down
64 changes: 9 additions & 55 deletions tests/test_stringx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 4f21bd3

Please sign in to comment.