Sessions

Session module for Peak API.

class session.Session(auth_token=None, stage=None)

A session stores credentials which are used to authenticate the requests.

By default, a DEFAULT_SESSION is created which reads the credentials from the env variables. Custom Session objects can be created and used if you want to work with multiple tenants.

Parameters:
  • auth_token (str) –

  • stage (Stage) –

create_download_request(endpoint, method, content_type, download_path, params=None, body=None)

Prepares a request to be sent over the network.

Adds auth_token to the headers and creates URL using STAGE. To be used for file download requests.

Parameters:
  • endpoint (str) – The endpoint to send the request to.

  • method (HttpMethods) – The HTTP method to use.

  • content_type (ContentType) – The content type of the request.

  • download_path (str) – Path where the downloaded file will be stored.

  • params (Dict[str, Any], optional) – params to send to the request, defaults to None

  • body (Dict[str, Any], optional) – body to send to the request, only used in multipart requests, defaults to None

Raises:

InvalidPathException – The download_path is invalid.

Return type:

None

create_generator_request(endpoint, method, content_type, response_key, *, params=None, body=None, path=None, subdomain='service')

Prepares a request to be sent over the network.

Adds auth_token to the headers and creates URL using STAGE. Returns an iterator which automatically handles pagination and returns a new page at each iteration. To be used with list endpoints only, which returns pageNumber, pageCount keys in response.

# noqa: DAR201

Parameters:
  • endpoint (str) – The endpoint to send the request to.

  • method (HttpMethods) – The HTTP method to use.

  • content_type (ContentType) – The content type of the request.

  • response_key (str) – key in the response dict which contains actual list data.

  • params (Optional[Dict[str, Any]]) – params to send to the request.

  • body (Optional[Dict[str, Any]]) – body to send to the request.

  • path (Optional[str]) – path to the file or folder that will be compressed and used as artifact.

  • subdomain (Optional[str]) – Subdomain for the endpoint. Defaults to service.

Yields:

Iterator[Dict[str, Any]] – paginated response json, element wise.

Raises:

StopIteration – There are no more pages to list

Return type:

Iterator[Dict[str, Any]]

create_request(endpoint, method, content_type, *, params=None, body=None, path=None, ignore_files=None, subdomain='service', file_key='artifact')

Prepares a request to be sent over the network.

Adds auth_token to the headers and creates URL using STAGE. To be used with endpoints which returns JSON parsable response.

Parameters:
  • endpoint (str) – The endpoint to send the request to.

  • method (HttpMethods) – The HTTP method to use.

  • content_type (ContentType) – The content type of the request.

  • params (Dict[str, Any], optional) – params to send to the request, defaults to None

  • body (Dict[str, Any], optional) – body to send to the request, defaults to None

  • path (Optional[str] optional) – path to the file or folder that will be compressed and used as artifact, required for multipart requests.

  • ignore_files (Optional[list[str]]) – Ignore files to be used when creating artifact, used only for multipart requests.

  • subdomain (Optional[str]) – Subdomain for the endpoint. Defaults to service.

  • file_key (Optional[str]) – the field in which the files must be uploaded

Returns:

response dict object.

Return type:

Any