site_api¶
An API wrapper around the Site API.
- class APIClient(site_api_url, site_api_token, **session_kwargs)[source]¶
Bases:
object
A wrapper for the Django Site API.
- __init__(site_api_url, site_api_token, **session_kwargs)[source]¶
Initialize a new
APIClient
instance.- Parameters:
site_api_url (
str
) – The URL of the site API.site_api_token (
str
) – The token to use for authentication.session_kwargs – Keyword arguments to pass to the
aiohttp.ClientSession
constructor.
- async delete(endpoint, *, raise_for_status=True, **kwargs)[source]¶
Send a DELETE request to the site API and return the JSON response.
- Parameters:
endpoint (
str
) – The endpoint to send the request to.raise_for_status (
bool
) – Whether or not to raise an exception if the response is not OK.**kwargs – Any extra keyword arguments to pass to
aiohttp.request()
.
- Return type:
- Returns:
The JSON response the API returns, or None if the response is 204 No Content.
- async get(endpoint, *, raise_for_status=True, **kwargs)[source]¶
Equivalent to
APIClient.request()
with GET passed as the method.- Return type:
- async static maybe_raise_for_status(response, should_raise)[source]¶
Raise
ResponseCodeError
for non-OK response if an exception should be raised.- Parameters:
response (
aiohttp.ClientResponse
) – The response to check.should_raise (
bool
) – Whether or not to raise an exception.
- Raises:
ResponseCodeError – If the response is not OK and
should_raise
is True.- Return type:
- async patch(endpoint, *, raise_for_status=True, **kwargs)[source]¶
Equivalent to
APIClient.request()
with PATCH passed as the method.- Return type:
- async post(endpoint, *, raise_for_status=True, **kwargs)[source]¶
Equivalent to
APIClient.request()
with POST passed as the method.- Return type:
- async put(endpoint, *, raise_for_status=True, **kwargs)[source]¶
Equivalent to
APIClient.request()
with PUT passed as the method.- Return type:
- async request(method, endpoint, *, raise_for_status=True, **kwargs)[source]¶
Send an HTTP request to the site API and return the JSON response.
- Parameters:
method (
str
) – The HTTP method to use.endpoint (
str
) – The endpoint to send the request to.raise_for_status (
bool
) – Whether or not to raise an exception if the response is not OK.**kwargs – Any extra keyword arguments to pass to
aiohttp.request()
.
- Return type:
- Returns:
The JSON response the API returns.
- Raises:
ResponseCodeError – If the response is not OK and
raise_for_status
is True.
- exception ResponseCodeError(response, response_json=None, response_text=None)[source]¶
Bases:
ValueError
Raised in
APIClient.request()
when a non-OK HTTP response is received.- __init__(response, response_json=None, response_text=None)[source]¶
Initialize a new
ResponseCodeError
instance.- Parameters:
response (
aiohttp.ClientResponse
) – The response object from the request.response_json (
Optional
[dict
]) – The JSON response returned from the request, if any.response_text (
Optional
[str
]) – The text of the request, if any.