pystclient.utils.helpers

Utility functions for pystclient.

Functions

handle_exception()

Context manager to handle exceptions and convert unknown exceptions to PyStclientError.

merge_dicts(dict1, dict2)

Recursively merge dict2 into dict1.

pprint_map(m)

Pretty print a dictionary as JSON.

raise_if_not_json_ok(resp)

Raise APIResponseError if response is not JSON and OK.

raise_if_not_ok(resp)

Raise APIResponseError if response is not OK.

random_string([length])

Generate a random string of ASCII letters.

stringify_keys(m)

Traverse a dictionary or Mapping tree recursively and convert Keyword keys to strings.

to_uuid(uuid_)

Convert string to UUID or return UUID as-is.

without_none_values(m)

Traverse a dictionary tree recursively and remove all keys whose values are None.

pystclient.utils.helpers.handle_exception() Generator[None, Any, Any]

Context manager to handle exceptions and convert unknown exceptions to PyStclientError.

pystclient.utils.helpers.merge_dicts(dict1: dict[Any, Any], dict2: dict[Any, Any]) dict[Any, Any]

Recursively merge dict2 into dict1.

Parameters:
  • dict1 – Base dictionary to merge into.

  • dict2 – Dictionary to merge from.

Returns:

Merged dictionary.

pystclient.utils.helpers.pprint_map(m: dict[Any, Any]) None

Pretty print a dictionary as JSON.

Parameters:

m – Dictionary to print.

pystclient.utils.helpers.raise_if_not_json_ok(resp: Response) Response

Raise APIResponseError if response is not JSON and OK.

Parameters:

resp – HTTP response to check.

Returns:

The original response if OK and JSON.

Raises:

APIResponseError – If response is not JSON and OK.

pystclient.utils.helpers.raise_if_not_ok(resp: Response) Response

Raise APIResponseError if response is not OK.

Parameters:

resp – HTTP response to check.

Returns:

The original response if OK.

Raises:

APIResponseError – If response is not OK.

pystclient.utils.helpers.random_string(length: int = 8) str

Generate a random string of ASCII letters.

Parameters:

length – Length of the random string. Defaults to 8.

Returns:

Random string of ASCII letters.

pystclient.utils.helpers.stringify_keys(m: dict[Any, Any] | Mapping[Any, Any]) dict[str, Any]

Traverse a dictionary or Mapping tree recursively and convert Keyword keys to strings.

Returns a new dict that contains the converted keys.

Parameters:

m – An input Mapping or dict to traverse.

Returns:

A new dictionary with all Keywords converted to strings.

pystclient.utils.helpers.to_uuid(uuid_: str | UUID) UUID

Convert string to UUID or return UUID as-is.

Parameters:

uuid – UUID as string or UUID object.

Returns:

UUID object.

pystclient.utils.helpers.without_none_values(m: dict[Any, Any]) dict[Any, Any]

Traverse a dictionary tree recursively and remove all keys whose values are None.

Parameters:

m – A dictionary to traverse.

Returns:

A new dictionary without None values.