-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
138 additions
and
163 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file was deleted.
Oops, something went wrong.