ServiceAPI, a base class for APIs which talk to a service, typically a web service via HTTP.
Latest release 20260531:
- HTTPServiceAPI: add truediv(suburl) which does a GET.
- HTTPServiceAPI: default .API_HOSTNAME comes from the subclass.
- HTTPServiceAPI.json: catch requests.exceptions.JSONDecodeError also.
- HTTPServiceAPI: new mode ("response", "json", "data", callable) parameter.
- HTTPServiceAPI: new check_json optional dict of expected fields and values in JSON responses.
An instance of a ServiceAPI embodies some basic features
that feel common to web based services:
- a notion of a login
- local state, an
SQLTagsfor data about entities of the service - downloads, if that is a thing, with
FSTagsfor file annotations
Short summary:
HTTPServiceAPI:HTTPServiceAPIbase class for other APIs talking to HTTP services.RequestsNoAuth: This is a special purpose subclass ofrequests.auth.AuthBaseto apply no authorisation at all. This is for services with their own special purpose authorisation and avoids things like automatic netrc based auth.ServiceAPI:SewrviceAPIbase class for other APIs talking to services.
Module contents:
-
class HTTPServiceAPI(ServiceAPI):HTTPServiceAPIbase class for other APIs talking to HTTP services.Subclasses must define:
API_BASE: the base URL of API calls. For example, thePlayOnAPIdefines this asf'https://{API_HOSTNAME}/v3/'.
HTTPServiceAPI.get(self, suburl, **kw) -> requests.models.Response:
Call slef.suburl with method="GET".
HTTPServiceAPI.post(self, suburl, **kw) -> requests.models.Response:
Call slef.suburl with method="POST".
HTTPServiceAPI.response_as_json(self, rsp: requests.models.Response) -> dict:
Return rsp.json().
HTTPServiceAPI.response_as_json_data(self, rsp: requests.models.Response) -> dict:
Return the "data" element from a JSON response.
HTTPServiceAPI.suburl(self, suburl, *, base_url=None, method='GET', mode=None, check=True, cookies=None, headers=None, runstate: Optional[cs.resources.RunState] = <function uses_runstate.<locals>.<lambda> at 0x111844fe0>, verbose: bool, **rqkw) -> Union[requests.models.Response, dict]:
Request suburl from the service, by default using a GET.
The suburl must be a URL subpath not commencing with '/'.
Return:
mode(Response)ifmodeis callable- the
Responseifmode=="response" - the
Response.json()ifmode=="json" - the
Response.json()["data"]ifmode=="data"
Keyword parameters:
base_url: the base request domain, default fromself.API_BASEmethod: optional request method, default'GET'check: if true, raise an HTTP error if the response status is not 200; defaultTruecookies: optional cookie jar, default fromself.cookiesmode: optional result mode, default fromself.modeOther keyword parameters are passed to the requests method.
class RequestsNoAuth(requests.auth.AuthBase): This is a special purpose subclass ofrequests.auth.AuthBaseto apply no authorisation at all. This is for services with their own special purpose authorisation and avoids things like automatic netrc based auth.class ServiceAPI(cs.resources.MultiOpenMixin, cs.sqltags.UsesSQLTags):SewrviceAPIbase class for other APIs talking to services.
ServiceAPI.available(self) -> Set[cs.sqltags.SQLTagSet]:
Return a set of the SQLTagSet instances representing available
items at the service, for example purchased books
available to your login.
ServiceAPI.get_login_state(self, do_refresh=False) -> cs.tagset.HasTags:
The login state, a HasTags, stored as login.state.login_userid.
This performs a login if necessary or if do_refresh is true
(default False).
ServiceAPI.login(self) -> Mapping:
Do a login: authenticate to the service, return a mapping of related information.
Not all services require this and we expect such subclasses to avoid use of login-based methods.
ServiceAPI.login_expiry:
Expiry UNIX time for the login state.
This implementation returns None.
ServiceAPI.startup_shutdown(self):
Open/close the FSTags and UsesTagSets.
Release Log
Release 20260531:
- HTTPServiceAPI: add truediv(suburl) which does a GET.
- HTTPServiceAPI: default .API_HOSTNAME comes from the subclass.
- HTTPServiceAPI.json: catch requests.exceptions.JSONDecodeError also.
- HTTPServiceAPI: new mode ("response", "json", "data", callable) parameter.
- HTTPServiceAPI: new check_json optional dict of expected fields and values in JSON responses.
Release 20241007: HTTPServiceAPI.suburl: support interruption by RunState.cancel.
Release 20240723: ServiceAPI: acquire the fstags automatically at init.
Release 20230703: Retry logic for requests.
Release 20230217: Initial release.
Release files for cs-service-api 20260531
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| cs_service_api-20260531.tar.gz | 5.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| cs_service_api-20260531-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 11.5 kB
Release files / cs_service_api-20260531.tar.gz
| Download URL | cs_service_api-20260531.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5db3f2cec0256b711ff756bc4ef9ad04386e52e0815d3aee0e3cd7ed9f2839dd
|
|
BLAKE2b-256 checksum How to use checksums |
564c1bf9424099d2515d3ac511b9f722eae3f96043452b4e7df0be36c3a83fcb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|
Release files / cs_service_api-20260531-py2.py3-none-any.whl
| Download URL | cs_service_api-20260531-py2.py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
fc2e31ddc744728a9b29218b8f0d0059a1fa3124846eafd483f4e49a29c81c31
|
|
BLAKE2b-256 checksum How to use checksums |
7a393beb29761e3d27fe8e955efaa20dfc2eb6c066e49ed6f016a3adb3bf4926
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|