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]¶
Equivalent to
APIClient.request()
with DELETE passed as the method.
- async get(endpoint, *, raise_for_status=True, **kwargs)[source]¶
Equivalent to
APIClient.request()
with GET passed as the method.
- 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.
- async post(endpoint, *, raise_for_status=True, **kwargs)[source]¶
Equivalent to
APIClient.request()
with POST passed as the method.
- async put(endpoint, *, raise_for_status=True, **kwargs)[source]¶
Equivalent to
APIClient.request()
with PUT passed as the method.
- 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, or
None
if the response code is 204.- 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 (
dict
|None
) – The JSON response returned from the request, if any.response_text (
str
|None
) – The text of the request, if any.