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
intotarget_dict
if the latter does not already contain a value for each of the key names inmerge_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 examplemerge_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 usename (
str
) – Main partargs (
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: 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 useunique_items (
bool
) – Flag if array items should be uniquemin (
int
) – Correlates tominLength
attribute of JSON Schema arraymax (
int
) – Correlates tomaxLength
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
-
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: 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 tomethod (
str
) – The method to useraise_for_status (
bool
) – Should an exception be raised for a failed response. Default is Trueargs – 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
-
classmethod