Skip to content

Commit

Permalink
Rename API client
Browse files Browse the repository at this point in the history
  • Loading branch information
cachitas committed Mar 30, 2024
1 parent 4dc0896 commit 1393a88
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 163 deletions.
68 changes: 0 additions & 68 deletions src/stringdb/api.py

This file was deleted.

65 changes: 65 additions & 0 deletions src/stringdb/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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
) -> None:
from stringdb import DEFAULT_CALLER_IDENTITY

super().__init__(
params=dict(caller_identity=identity or DEFAULT_CALLER_IDENTITY),
base_url=base_url,
)

def request(
self,
endpoint: str,
params: dict = {},
*,
method: str = "POST",
format: str = "json",
):
url = "/".join(["api", format, endpoint])
logger.info("POST", url, params)
response = super().request(method, url, params=params)
logger.info(response.status_code)
response.raise_for_status()
return response.json()

def get_string_ids(
self,
identifiers: List[str],
species: int,
limit: int = 1,
echo_query: bool = True,
):
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)

def get_interaction_partners(
self,
identifiers: List[str],
species: int,
limit: Optional[int] = None,
):
params = dict(
identifiers="\r".join(identifiers),
species=species,
)

if limit:
params.update(limit=limit)

return self.request("interaction_partners", params=params)
46 changes: 0 additions & 46 deletions tests/test_api.py

This file was deleted.

73 changes: 73 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import httpx

import stringdb


def test_default_base_url():
with stringdb.Client() as client:
assert client.base_url.scheme == "https"
assert client.base_url.host == "string-db.org"
assert client.base_url.path == "/"


def test_custom_address():
with stringdb.Client("https://example.com/api/v2") as client:
assert client.base_url.host == "example.com"
assert client.base_url.path == "/api/v2/"


def test_timeout():
with stringdb.Client() as client:
assert client.timeout.connect == 5.0
assert client.timeout.pool == 5.0
assert client.timeout.read == 5.0
assert client.timeout.write == 5.0


def test_caller_identity():
with stringdb.Client() as client:
assert client.params["caller_identity"] == stringdb.DEFAULT_CALLER_IDENTITY


def test_custom_caller_identity():
custom_identity = "random tool"
with stringdb.Client(identity=custom_identity) as client:
assert client.params["caller_identity"] == custom_identity


def test_map_identifiers(httpx_mock):
httpx_mock.add_response(
url=httpx.URL(
"https://string-db.org/api/json/get_string_ids",
params={
"identifiers": "some_identifier",
"species": "7227",
"limit": 1,
"echo_query": True,
"caller_identity": stringdb.DEFAULT_CALLER_IDENTITY,
},
),
method="POST",
json=True,
)

with stringdb.Client() as client:
client.get_string_ids(["some_identifier"], 7227)


def test_interaction_partners(httpx_mock):
httpx_mock.add_response(
url=httpx.URL(
"https://string-db.org/api/json/interaction_partners",
params={
"identifiers": "id1\rid2",
"species": "7227",
"caller_identity": stringdb.DEFAULT_CALLER_IDENTITY,
},
),
method="POST",
json=True,
)

with stringdb.Client() as client:
client.get_interaction_partners(["id1", "id2"], 7227)
49 changes: 0 additions & 49 deletions tests/test_using_httpx.py

This file was deleted.

0 comments on commit 1393a88

Please sign in to comment.