thorn.events — Thorn 1.3.0 documentation

# thorn.events

- thorn.webhook.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)

  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)
  `prepare_recipient_validators`(validators)
  `prepared_recipient_validators`
  `recipient_validators` = None
  `send`(data, sender=None, on_success=None, on_error=None, timeout=None, on_timeout=None)

    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)

  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)
  `dispatches_on_change`()
  `dispatches_on_create`()
  `dispatches_on_delete`()
  `dispatches_on_m2m_add`(related_field)
  `dispatches_on_m2m_clear`(related_field)
  `dispatches_on_m2m_remove`(related_field)
  `instance_data`(instance)

    Get event data from `instance.webhook_payload()`.

  `instance_sender`(instance)

    Get event `sender` from model instance.

  `on_signal`(instance, **kwargs)
  `send`(instance, data=None, sender=None, **kwargs)

    Send event for model `instance`.

    Parameters: | data – Event specific data.

    See `Event.send()` for more arguments supported.

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