Core¶
Core API reference
-
class
notifiers.core.SchemaResource[source]¶ Base class that represent an object schema and its utility methods
-
_get_environs(prefix: str = None) → dict[source]¶ Fetches set environment variables if such exist, via the
dict_from_environs()Searches for [PREFIX_NAME]_[PROVIDER_NAME]_[ARGUMENT] for each of the arguments defined in the schema- Return type
dict- Parameters
prefix (
str) – The environ prefix to use. If not supplied, uses the default- Returns
A dict of arguments and value retrieved from environs
-
_merge_defaults(data: dict) → dict[source]¶ Convenience method that calls
merge_dicts()in order to merge default values- Return type
dict- Parameters
data (
dict) – Notification data- Returns
A merged dict of provided data with added defaults
-
_prepare_data(data: dict) → dict[source]¶ - Use this method to manipulate data that’ll fit the respected provider API.
For example, all provider must use the
messageargument but sometimes provider expects a different variable name for this, liketext.
- Return type
dict- Parameters
data (
dict) – Notification data- Returns
Returns manipulated data, if there’s a need for such manipulations.
-
_process_data(**data) → dict[source]¶ - The main method that process all resources data. Validates schema, gets environs, validates data, prepares
it via provider requirements, merges defaults and check for data dependencies
- Return type
dict- Parameters
data – The raw data passed by the notifiers client
- Returns
Processed data
-
abstract property
_required¶ Will hold the schema’s required part
-
abstract property
_schema¶ Resource JSON schema without the required part
-
_validate_data(data: dict)[source]¶ Validates data against provider schema. Raises
BadArgumentsif relevant- Parameters
data (
dict) – Data to validate- Raises
-
_validate_data_dependencies(data: dict) → dict[source]¶ Validates specific dependencies based on the content of the data, as opposed to its structure which can be verified on the schema level
- Return type
dict- Parameters
data (
dict) – Data to validate- Returns
Return data if its valid
- Raises
-
_validate_schema()[source]¶ Validates provider schema for syntax issues. Raises
SchemaErrorif relevant- Raises
-
property
arguments¶ Returns all of the provider argument as declared in the JSON schema
-
create_response(data: dict = None, response: requests.models.Response = None, errors: list = None) → notifiers.core.Response[source]¶ Helper function to generate a
Responseobject- Return type
- Parameters
data (
dict) – The data that was used to send the notificationresponse (
Response) –requests.Responseif existerrors (
list) – List of errors if relevant
-
property
defaults¶ A dict of default provider values if such is needed
-
abstract property
name¶ Resource provider name
-
property
required¶ Returns a dict of the relevant required parts of the schema
-
property
schema¶ A property method that’ll return the constructed provider schema. Schema MUST be an object and this method must be overridden
- Returns
JSON schema of the provider
-
-
class
notifiers.core.Provider[source]¶ The Base class all notification providers inherit from.
-
abstract
_send_notification(data: dict) → notifiers.core.Response[source]¶ The core method to trigger the provider notification. Must be overridden.
- Return type
- Parameters
data (
dict) – Notification data
-
property
metadata¶ Returns a dict of the provider metadata as declared. Override if needed.
-
notify(raise_on_errors: bool = False, **kwargs) → notifiers.core.Response[source]¶ The main method to send notifications. Prepares the data via the
_prepare_data()method and then sends the notification via the_send_notification()method- Return type
- Parameters
kwargs – Notification data
raise_on_errors (
bool) – Should theraise_on_errors()be invoked immediately
- Returns
A
Responseobject- Raises
NotificationErrorifraise_on_errorsis set to True and response contained errors
-
property
resources¶ Return a list of names of relevant
ProviderResourceobjects
-
abstract
-
class
notifiers.core.ProviderResource[source]¶ The base class that is used to fetch provider related resources like rooms, channels, users etc.
-
class
notifiers.core.Response(status: str, provider: str, data: dict, response: requests.models.Response = None, errors: list = None)[source]¶ A wrapper for the Notification response.
- Parameters
status – Response status string.
SUCCESSorFAILEDprovider – Provider name that returned that response. Correlates to
namedata – The notification data that was used for the notification
response – The response object that was returned. Usually
requests.Responseerrors – Holds a list of errors if relevant
-
raise_on_errors()[source]¶ Raises a
NotificationErrorif response hold errors- Raises
NotificationError: If response has errors
-
notifiers.core.get_notifier(provider_name: str, strict: bool = False) → notifiers.core.Provider[source]¶ Convenience method to return an instantiated
Providerobject according to itname- Return type
- Parameters
provider_name (
str) – Thenameof the requestedProviderstrict (
bool) – Raises aValueErrorif the given provider string was not found
- Returns
Provideror None- Raises
ValueError – In case
strictis True and provider not found
-
notifiers.core.notify(provider_name: str, **kwargs) → notifiers.core.Response[source]¶ Quickly sends a notification without needing to get a notifier via the
get_notifier()method.- Return type
- Parameters
provider_name (
str) – Name of the notifier to use. Note that if this notifier name does not exist it will raise akwargs – Notification data, dependant on provider
- Returns
- Raises
NoSuchNotifierErrorIfprovider_nameis unknown, will raise notification error
Logging¶
-
class
notifiers.logging.NotificationHandler(provider: str, defaults: dict = None, **kwargs)[source]¶ A
logging.Handlerthat enables directly sending log messages to notifiers-
emit(record)[source]¶ Override the
emit()method that takes themsgattribute from the log record passed- Parameters
record –
logging.LogRecord
-
handleError(record)[source]¶ Handles any errors raised during the
emit()method. Will only try to pass exceptions to fallback notifier (if defined) in case the exception is a sub-class ofNotifierException- Parameters
record –
logging.LogRecord
-
init_providers(provider, kwargs)[source]¶ Inits main and fallback provider if relevant
- Parameters
provider – Provider name to use
kwargs – Additional kwargs
- Raises
ValueError – If provider name or fallback names are not valid providers, a
ValueErrorwill be raised
-