rankade.api.Api

Module Contents

Classes

Api

Wrapper around aiohttp.

Data

logger

JSON

TypeAlias = Dict[str, β€œJSON”] | List[β€œJSON”] | str | int | float | bool | None

HEADERS

TypeAlias = Dict[str, str] | None

PARAMS

TypeAlias = Dict[str, str | int] | None

API

logger = 'getLogger(...)'
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/

_key: Optional[str] = None

Rankade key.

_secret: Optional[str] = None

Rankade secret.

_session: Optional[ClientSession] = None

aiohttp ClientSession to use, will create one if not passed in…

_token: Optional[Token] = 'field(...)'

Rankade issued token

property _credentials_params

A dictionary containing the key and secret for Rankade API for use as parameters in the http request.

property token: Optional[Token]

Representation of a JWT.

_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.