Utils

Assorted helper utils

notifiers.utils.helpers.text_to_bool(value: str) bool[source]

Tries to convert a text value to a bool. If unsuccessful returns if value is None or not

Return type

bool

Parameters

value (str) – Value to check

notifiers.utils.helpers.merge_dicts(target_dict: dict, merge_dict: dict) dict[source]

Merges merge_dict into target_dict if the latter does not already contain a value for each of the key names in merge_dict. Used to cleanly merge default and environ data into notification payload.

Return type

dict

Parameters
  • target_dict (dict) – The target dict to merge into and return, the user provided data for example

  • merge_dict (dict) – The data that should be merged into the target data

Returns

A dict of merged data

notifiers.utils.helpers.dict_from_environs(prefix: str, name: str, args: list) dict[source]

Return a dict of environment variables correlating to the arguments list, main name and prefix like so: [prefix]_[name]_[arg]

Return type

dict

Parameters
  • prefix (str) – The environ prefix to use

  • name (str) – Main part

  • args (list) – List of args to iterate over

Returns

A dict of found environ values

JSON schema related utils

notifiers.utils.schema.helpers.one_or_more(schema: dict, unique_items: bool = True, min: int = 1, max: Optional[int] = None) dict[source]

Helper function to construct a schema that validates items matching schema or an array containing items matching schema.

Return type

dict

Parameters
  • schema (dict) – The schema to use

  • unique_items (bool) – Flag if array items should be unique

  • min (int) – Correlates to minLength attribute of JSON Schema array

  • max (int) – Correlates to maxLength attribute of JSON Schema array

notifiers.utils.schema.helpers.list_to_commas(list_of_args) str[source]

Converts a list of items to a comma separated list. If list_of_args is not a list, just return it back

Return type

str

Parameters

list_of_args – List of items

Returns

A string representing a comma separated list.

JSON schema custom formats

notifiers.utils.schema.formats.is_iso8601(instance: str)[source]

Validates ISO8601 format

notifiers.utils.schema.formats.is_rfc2822(instance: str)[source]

Validates RFC2822 format

notifiers.utils.schema.formats.is_ascii(instance: str)[source]

Validates data is ASCII encodable

notifiers.utils.schema.formats.is_valid_file(instance: str)[source]

Validates data is a valid file

notifiers.utils.schema.formats.is_valid_port(instance: int)[source]

Validates data is a valid port

notifiers.utils.schema.formats.is_timestamp(instance)[source]

Validates data is a timestamp

notifiers.utils.schema.formats.is_e164(instance)[source]

Validates data is E.164 format

class notifiers.utils.requests.RequestsHelper[source]

A wrapper around requests.Session which enables generically handling HTTP requests

classmethod request(url: str, method: str, raise_for_status: bool = True, path_to_errors: Optional[tuple] = None, *args, **kwargs) tuple[source]

A wrapper method for request`(), which adds some defaults and logging

Return type

tuple

Parameters
  • url (str) – The URL to send the reply to

  • method (str) – The method to use

  • raise_for_status (bool) – Should an exception be raised for a failed response. Default is True

  • args – Additional args to be sent to the request

  • kwargs – Additional args to be sent to the request

Returns

Dict of response body or original requests.Response