thorn.events

thorn.webhook.events

User-defined webhook events.

class thorn.events.Event(name, timeout=None, dispatcher=None, retry=None, retry_max=None, retry_delay=None, app=None, recipient_validators=None, subscribers=None, request_data=None, allow_keepalive=None, **kwargs)[source]

Webhook Event.

Parameters:
  • name – Name of this event. Namespaces can be dot-separated, and if so subscribers can glob-match based on the parts in the name (e.g. "order.created").
  • timeout – Default request timeout for this event.
  • retry – Enable/disable retries when dispatching this event fails (disabled by default).
  • retry_max – Max number of retries (3 by default).
  • retry_delay – Delay between retries (60 seconds by default).
  • recipient_validators

    List of functions validating the recipient URL string. Functions must return False if the URL is blocked. Default is to only allow HTTP and HTTPS, with respective reserved ports 80 and 443, and to block internal IP networks, and can be changed using the THORN_RECIPIENT_VALIDATORS setting:

    recipient_validators=[
        thorn.validators.block_internal_ips(),
        thorn.validators.ensure_protocol('http', 'https'),
        thorn.validators.ensure_port(80, 443),
    ]
    

    WARNING: block_internal_ips() will only test for reserved internal networks, and not private networks with a public IP address. You can block those using block_cidr_network.

  • subscribers – Additional subscribers, as a list of URLs, subscriber model objects, or callback functions returning these
  • request_data – Optional mapping of extra data to inject into event payloads,
  • allow_keepalive – Flag to disable HTTP connection keepalive for this event only. Keepalive is enabled by default.
allow_keepalive = True
app = None
dispatcher
prepare_payload(data)[source]
prepare_recipient_validators(validators)[source]
prepared_recipient_validators[source]
recipient_validators = None
send(data, sender=None, on_success=None, on_error=None, timeout=None, on_timeout=None)[source]

Send event to all subscribers.

Parameters:
  • data – Event payload (must be json serializable).
  • sender – Optional event sender, as a User instance.
  • timeout – Specify custom HTTP request timeout overriding the THORN_EVENT_TIMEOUT setting.
  • on_success – Callback called for each HTTP request if the request succeeds. Must take single Request argument.
  • on_timeout – Callback called for each HTTP request if the request times out. Takes two arguments: a Request, and the time out exception instance.
  • on_error – Callback called for each HTTP request if the request fails. Takes two arguments: a Request argument, and the error exception instance.
  • context – Extra context to pass to subscriber callbacks
subscribers
class thorn.events.ModelEvent(name, *args, **kwargs)[source]

Event related to model changes.

This event type follows a specific payload format:

{"event": "(str)event_name",
 "ref": "(URL)model_location",
 "sender": "(User pk)optional_sender",
 "data": {"event specific data": "value"}}
Parameters:
  • name – Name of event.
  • reverse – A function that takes a model instance and returns the canonical URL for that resource.
  • sender_field – Field used as a sender for events, e.g. "account.user", will use instance.account.user.
  • $field__$op

    Optional filter arguments to filter the model instances to dispatch for. These keyword arguments can be defined just like the arguments to a Django query set, the only difference being that you have to specify an operator for every field: this means last_name="jerry" does not work, and you have to use last_name__eq="jerry" instead.

    See Q for more information.

  • signal_dispatcher – Custom signal_dispatcher used to connect this event to a model signal.
connect_model(model)[source]
dispatches_on_change()[source]
dispatches_on_create()[source]
dispatches_on_delete()[source]
dispatches_on_m2m_add(related_field)[source]
dispatches_on_m2m_clear(related_field)[source]
dispatches_on_m2m_remove(related_field)[source]
instance_data(instance)[source]

Get event data from instance.webhook_payload().

instance_sender(instance)[source]

Get event sender from model instance.

on_signal(instance, **kwargs)[source]
send(instance, data=None, sender=None, **kwargs)[source]

Send event for model instance.

Parameters:data – Event specific data.

See Event.send() for more arguments supported.

send_from_instance(instance, context={}, **kwargs)[source]
should_dispatch(instance, **kwargs)[source]
signal_dispatcher
to_message(data, instance=None, sender=None, ref=None)[source]