Pywrapid Webclient

Documentation coming soon

Configuration

Client settings: client_options: request options such as ssl settings, timeout etc.

Token settings: refresh_token_timeout: Time the auth provider specifies for refresh token expiry in seconds <default: 86400> access_token_timeout: The time for access token expiry in seconds, omit or set to 0 to use response body values from auth provider <default: 0> token_expiry_offset: Seconds before expiration time we will treat tokens as already expired to trigger early renewal <default: 10> access_token_header: Custom response header name to look for access token after authentication <default: ‘’>

Any additional configuration parameter passed in will be included in the credential objects config. Same goes for any extra key value pair passed as parameter at instantiation.

Webclient

class WebClient(authorization_type: AuthorizationType = AuthorizationType.NONE, credentials: Type[WebCredentials] | None = None, dict_config: dict | None = None, wrapid_config: Type[WrapidConfig] | None = None)[source]

Bases: object

Web Client base

Generic web client class as base for creating application specific clients or to be used directly as a general use web client. Wraps the request library and adds generic exceptions.

Passes web calls transparently to requests, meaning you can use any requests option you see fit, such as proxy settings etc by passing them as key word arguments. If a configuration section named client_options is passed to the client, these options will be set for the web communication. Passed arguments will have precedence over configuration items.

The client allows you to mix and match authetication types with authorization types to fit strange combinations used in some APIs.

Can be used with a wrapid config or straight up dict config for use in clients extending this class.

Allows raise of exception on non-2xx responses (optional).

Init function for web client class

Parameters:
  • authorization_type (AuthorizationType (ENUM), optional) – wrapid authorization type to use for clients communication.

  • credentials (Type[WebCredentials], optional) – wrapid credentials object to use for clients communication.

  • dict_config (dict, optional, mutually exlusive with wrapid_conf) – dict object to store in the clients config parameter.

  • wrapid_config (Type[WrapidConfig], optional, mutually exlusive with dict_config) – wrapid configuration object to store configuration in the clients config parameter from.

Raises:

ClientException

__init__(authorization_type: AuthorizationType = AuthorizationType.NONE, credentials: Type[WebCredentials] | None = None, dict_config: dict | None = None, wrapid_config: Type[WrapidConfig] | None = None)[source]

Init function for web client class

Parameters:
  • authorization_type (AuthorizationType (ENUM), optional) – wrapid authorization type to use for clients communication.

  • credentials (Type[WebCredentials], optional) – wrapid credentials object to use for clients communication.

  • dict_config (dict, optional, mutually exlusive with wrapid_conf) – dict object to store in the clients config parameter.

  • wrapid_config (Type[WrapidConfig], optional, mutually exlusive with dict_config) – wrapid configuration object to store configuration in the clients config parameter from.

Raises:

ClientException

_unpack_jwt(token: str) dict[source]

Decodes and unpacks JWT tokens content

Does not include signature verification or encrypted parts

Parameters:

token (str) – Token to unpack

Returns:

Unpacked JWT token

Return type:

dict

call(method: str, url: str, raise_for_status: bool = False, skip_authentication: bool = False, **options: Any) Response[source]

Send web request to the target url

Parameters:
  • method (str) – Method of the HTTP request

  • url (str) – URL of the request

  • raise_for_status (bool) – Raise for non 2xx repsonses

  • skip_authentication (bool) – Skip authentication and skip token refresh controls

  • **options (dict) – request options

Raises:
Returns:

requests.Response object

Return type:

Response

generate_session(method: str = 'POST', **options: Any) None[source]

Authenticate and generate new token

Parameters:

method (str, optional) – HTTP Method to use. Defaults to “POST”.

Raises:

ClientAuthenticationError

property get_config: dict

Get current configuration

Returns:

configuration {dict} – Dict representation of configuration

session_expired() bool[source]

Check if our session has expired

Returns:

True if token is expired, False if still valid

Return type:

bool

Credentials

class WebCredentials[source]

Bases: object

Credential base class

Init class for web credentials

property config: dict

Getter for options

property options: dict

Getter for options

validate_url(url: str = '', raise_on_fail: bool = False) bool[source]

Validate URL strings

Parameters:
  • url (str, optional) – The URL to validate. Defaults to “”.

  • raise_on_fail (bool, optional) – Raise error if validation fails. Defaults to False.

Raises:

CredentialURLError – _description_

Returns:

_description_

Return type:

bool

class BasicAuthCredentials(username: str, password: str, login_url: str = '', config: Type[WrapidConfig] | dict | None = None, **kwargs: dict[str, Any])[source]

Bases: WebCredentials

Credential class for basic auth

Init class for web credentials

class X509Credentials(cert_file: str = '', key_file: str = '', login_url: str = '', jwt_key: str = '', access_token_timeout: int = 0, token_expiry_offset: int = 0, config: Type[WrapidConfig] | dict | None = None, **kwargs: dict[str, Any])[source]

Bases: WebCredentials

Credential class for x509 auth

Init class for web credentials

Client Exceptions

exception ClientException[source]

Bases: PywrapidException

Base Client Exception

exception ClientError[source]

Bases: PywrapidError

Client Error Exception

exception ClientAuthorizationError[source]

Bases: ClientError

Client Authorization Error Exception

exception ClientAuthenticationError[source]

Bases: ClientError

Client Authorization Error Exception

exception ClientTokenRefreshError[source]

Bases: ClientError

Token Refresh Error Exception

exception ClientTimeout[source]

Bases: ClientError

Client Timeout Exception

exception ClientConnectionError[source]

Bases: ClientError

Client Connection Error Exception

exception ClientHTTPError[source]

Bases: ClientError

Client HTTP Error Exception

Credential Exceptions

exception CredentialError[source]

Bases: PywrapidError

Credential Certificate Error Exception

exception CredentialCertificateFileError[source]

Bases: CredentialError

Credential Certificate Error Exception