snip.api.request

Make requests to the Snips API.

Functions

parse_response(response[, token])

Parse a response from a request.

request(method, url[, token])

Make a request to the given URL with the given method and authentication token.

request_image(method, url[, token])

Fetch an image from the given URL and returns it as a PIL Image object.

Module Functions

snip.api.request.parse_response(response: Response, token: Token | None = None) dict | list | Image | None

Parse a response from a request.

Parameters:
  • response (requests.Response) – The response object to parse.

  • token (Optional[Token]) – The authentication token used for the request. For better error messages.

Returns:

The parsed response.

Return type:

dict[str, Optional[str]]

Raises:
  • AuthenticationException – If the request was not successful due to an authentication problem.

  • BadRequestException – If the request was not successful due to a bad request.

  • HTTPError – If the request was not successful due to a server error or other problem.

snip.api.request.request(method: str, url: str, token: Token | None = None, **kwargs) dict | list | Image | None

Make a request to the given URL with the given method and authentication token.

Parameters:
  • method (str) – The HTTP method to use for the request.

  • url (str) – The URL to make the request to.

  • token (Token) – The authentication token to use for the request.

  • **kwargs (Any) – Additional keyword arguments to pass to the requests.request function.

Returns:

The parsed response from the request.

Return type:

ParseResponse

Raises:

and see parse_response

snip.api.request.request_image(method: str, url: str, token: Token | None = None, **kwargs) Image

Fetch an image from the given URL and returns it as a PIL Image object.

Parameters:
  • method (str) – The HTTP method to use for the request.

  • url (str) – The URL of the image to fetch.

  • token (Optional[Token]) – The authentication token to use for the request.

  • **kwargs (Any) – Additional keyword arguments to pass to the requests.requests function.

Returns:

The fetched image as a PIL Image object.

Return type:

Image.Image