rankade.api.Api
Module Contents
Classes
Wrapper around aiohttp. |
Data
TypeAlias = Dict[str, βJSONβ] | List[βJSONβ] | str | int | float | bool | None |
|
TypeAlias = Dict[str, str] | None |
|
TypeAlias = Dict[str, str | int] | None |
API
- JSON: TypeAlias = None
TypeAlias = Dict[str, βJSONβ] | List[βJSONβ] | str | int | float | bool | None
- HEADERS: TypeAlias = None
TypeAlias = Dict[str, str] | None
- PARAMS: TypeAlias = None
TypeAlias = Dict[str, str | int] | None
- class Api(key_or_token: str, secret: Optional[str] = None, base_url: Optional[str] = None, session: Optional[ClientSession] = None)[source]
Bases:
object
Wrapper around aiohttp.
It manages the aiohttp ClientSession, Authorisation, and requests to the Rankade API. It is generally best to use this as a context manager as it will manage the creation and cleanup of the ClientSession.
1 async with Api(...) as api: 2 result = api.request(...)
It can also be used without the context manager, if you are managing the ClientSession & error handling yourself.
1 api = Api(...) 2 async api.start() 3 async api.request(...) 4 ... 5 async api.stop()
Initialization
- Parameters:
key_or_token (str) β Rankade key or token.
secret (Optional[str]) β Rankade secret.
base_url (Optional[str]) β Scheme and Resource to send requests to. Defaults to βhttps://api.rankade.com/public/api/1/β
session (Optional[ClientSession]) β aiohttp ClientSession to use, will create one if not passed in.
- _base_url: str = None
Scheme and Resource to send requests to. Defaults to βhttps://api.rankade.com/public/api/1/
- _session: Optional[ClientSession] = None
aiohttp ClientSession to use, will create one if not passed inβ¦
- property _credentials_params
A dictionary containing the key and secret for Rankade API for use as parameters in the http request.
- _make_url_for(endpoint_path: str = '') str [source]
Returns combined base_url + path for endpoint
- Parameters:
endpoint_path (str) β endpoint path to be turned into full URL.
- async _request_jwt() Optional[Token] [source]
Checks whether the token is valid or set and the will grab a new one using the key & secret provided during initialisation.
- Raises:
NoValidCredentials β If only a token is passed as a parameter to the constructor.
- async _paginated_request(endpoint: Endpoint_Request) MutableMapping[str, Any] [source]
Loops through the pages of response from the endpoint and combines them all into one Page to return.
- Parameters:
endpoint (Endpoint_Request) β Full information for the request contained in endpoint.Endpoint
- async _request(endpoint: Endpoint_Request) MutableMapping[str, Any] [source]
Main wrapper around aiohttp.ClientSession.request, takes in the Endpoint request transforms it into the appropriate parameters for the ClientSession request.
- Params Endpoint_Request endpoint:
full endpoint request.
- async _check_request(raw_response: ClientResponse)[source]
Overrides aiohttp.ClientRequest.raise_for_status to allow better error handling.
- Parameters:
raw_response (ClientResponse) β Client response object.
- Raises:
ApiErrorResponse β If something unexpected with the response has happened.
MatchValidation β Match rejected by the server, due to some kind of validation error.
AuthCredentials β Invalid credentials or client disabled.
Quotas β One of the quote limits has been hit.
- async _exception_of_last_resort(response: ClientResponse, message: str = 'An unknown error occured while dealing with the request.') ApiErrorResponse [source]
If something has gone badly wrong with parsing the response, this will create and return an exception to be raised.
- Parameters:
response (ClientResponse) β Response which we are having an issue with.
message (str) β Message to be added to the exception, will also include the raw text of the response.
- async request(endpoint: Endpoint, headers: rankade.api.Api.HEADERS = None, params: rankade.api.Api.PARAMS = None, json: rankade.api.Api.JSON = None, **kwargs: Any) MutableMapping[str, Any] [source]
Public facing interface for API class, will call 1 of 2 methods depending if the request is expecting a paginated response or not.
- Parameters:
endpoint (Endpoint) β Contains basic config for the request, including if authorisation is required, will the response be paginated, and what http method & path to use. See
Endpoint
for a full list.headers (rankade.api.Api.HEADERS) β A dict representing any headers to be passed, normally this is internal use only for Bearer Auth header.
params (rankade.api.Api.PARAMS) β Parameters to use with the request.
json (rankade.api.Api.JSON) β JSON to send to the server.
**kwargs β Used to keep the call site as clean as possible, we can make almost all the requests with some mix of the above parameters. There are a few parameters that require passing through to the request.
Endpoint_Request
for a full list.
- start()[source]
Creates a Client session if none was passed through.
Note
Should only be called manually if not using API as a context manager.
- async stop()[source]
Closes the Api session.
Note
Should only be called manually if not using API as a context manager.
- async __aenter__()[source]
Entry handler for context manager.
- async __aexit__(*_)[source]
Cleanup handler for context manager.