thorn.request

Webhook HTTP requests.

class thorn.request.Request(event, data, sender, subscriber, id=None, on_success=None, on_error=None, timeout=None, on_timeout=None, retry=None, retry_max=None, retry_delay=None, headers=None, user_agent=None, app=None, recipient_validators=None, allow_keepalive=True)[source]

Webhook HTTP request.

Parameters:
  • event (str) – Name of event.
  • data (Any) – Event payload.
  • sender (Any) – Sender of event (or None).
  • subscriber (Subscriber) – Subscriber to dispatch the request for.
Keyword Arguments:
 
  • on_success (Callable) – Optional callback called if the HTTP request succeeds. Must take single argument: request.
  • on_timeout (Callable) – Optional callback called if the HTTP request times out. Must have signature: (request, exc).
  • on_error (Callable) – Optional callback called if the HTTP request fails. Must have signature: (request, exc).
  • headers (Mapping) – Additional HTTP headers to send with the request.
  • user_agent (str) – Set custom HTTP user agent.
  • recipient_validators (Sequence) – List of serialized recipient validators.
  • allow_keepalive (bool) – Allow reusing session for this HTTP request. Enabled by default.
  • retry (bool) – Retry in the event of timeout/failure? Disabled by default.
  • retry_max (int) – Maximum number of times to retry before giving up. Default is 3.
  • retry_delay (float) – Delay between retries in seconds int/float. Default is 60 seconds.
class Session

A Requests session.

Provides cookie persistence, connection-pooling, and configuration.

Basic Usage:

>>> import requests
>>> s = requests.Session()
>>> s.get('http://httpbin.org/get')
<Response [200]>

Or as a context manager:

>>> with requests.Session() as s:
>>>     s.get('http://httpbin.org/get')
<Response [200]>
close()

Closes all adapters and as such the session

delete(url, **kwargs)

Sends a DELETE request. Returns Response object.

Parameters:
  • url – URL for the new Request object.
  • **kwargs – Optional arguments that request takes.
Return type:

requests.Response

get(url, **kwargs)

Sends a GET request. Returns Response object.

Parameters:
  • url – URL for the new Request object.
  • **kwargs – Optional arguments that request takes.
Return type:

requests.Response

get_adapter(url)

Returns the appropriate connection adapter for the given URL.

Return type:requests.adapters.BaseAdapter
head(url, **kwargs)

Sends a HEAD request. Returns Response object.

Parameters:
  • url – URL for the new Request object.
  • **kwargs – Optional arguments that request takes.
Return type:

requests.Response

merge_environment_settings(url, proxies, stream, verify, cert)

Check the environment and merge it with some settings.

Return type:dict
mount(prefix, adapter)

Registers a connection adapter to a prefix.

Adapters are sorted in descending order by key length.

options(url, **kwargs)

Sends a OPTIONS request. Returns Response object.

Parameters:
  • url – URL for the new Request object.
  • **kwargs – Optional arguments that request takes.
Return type:

requests.Response

patch(url, data=None, **kwargs)

Sends a PATCH request. Returns Response object.

Parameters:
  • url – URL for the new Request object.
  • data – (optional) Dictionary, bytes, or file-like object to send in the body of the Request.
  • **kwargs – Optional arguments that request takes.
Return type:

requests.Response

post(url, data=None, json=None, **kwargs)

Sends a POST request. Returns Response object.

Parameters:
  • url – URL for the new Request object.
  • data – (optional) Dictionary, bytes, or file-like object to send in the body of the Request.
  • json – (optional) json to send in the body of the Request.
  • **kwargs – Optional arguments that request takes.
Return type:

requests.Response

prepare_request(request)

Constructs a PreparedRequest for transmission and returns it. The PreparedRequest has settings merged from the Request instance and those of the Session.

Parameters:requestRequest instance to prepare with this session’s settings.
Return type:requests.PreparedRequest
put(url, data=None, **kwargs)

Sends a PUT request. Returns Response object.

Parameters:
  • url – URL for the new Request object.
  • data – (optional) Dictionary, bytes, or file-like object to send in the body of the Request.
  • **kwargs – Optional arguments that request takes.
Return type:

requests.Response

request(method, url, params=None, data=None, headers=None, cookies=None, files=None, auth=None, timeout=None, allow_redirects=True, proxies=None, hooks=None, stream=None, verify=None, cert=None, json=None)

Constructs a Request, prepares it and sends it. Returns Response object.

Parameters:
  • method – method for the new Request object.
  • url – URL for the new Request object.
  • params – (optional) Dictionary or bytes to be sent in the query string for the Request.
  • data – (optional) Dictionary, bytes, or file-like object to send in the body of the Request.
  • json – (optional) json to send in the body of the Request.
  • headers – (optional) Dictionary of HTTP Headers to send with the Request.
  • cookies – (optional) Dict or CookieJar object to send with the Request.
  • files – (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.
  • auth – (optional) Auth tuple or callable to enable Basic/Digest/Custom HTTP Auth.
  • timeout (float or tuple) – (optional) How long to wait for the server to send data before giving up, as a float, or a (connect timeout, read timeout) tuple.
  • allow_redirects (bool) – (optional) Set to True by default.
  • proxies – (optional) Dictionary mapping protocol or protocol and hostname to the URL of the proxy.
  • stream – (optional) whether to immediately download the response content. Defaults to False.
  • verify – (optional) whether the SSL cert will be verified. A CA_BUNDLE path can also be provided. Defaults to True.
  • cert – (optional) if String, path to ssl client cert file (.pem). If Tuple, (‘cert’, ‘key’) pair.
Return type:

requests.Response

send(request, **kwargs)

Send a given PreparedRequest.

Return type:requests.Response
Request.annotate_headers(extra_headers)[source]
Request.app = None
Request.as_dict()[source]

Return dictionary representation of this request.

Note

All values must be json serializable.

Request.connection_errors = (<class 'requests.exceptions.ConnectionError'>,)

Tuple of exceptions considered a connection error.

Request.default_headers
Request.dispatch(session=None, propagate=False)[source]
Request.handle_connection_error(exc, propagate=False)[source]
Request.handle_timeout_error(exc, propagate=False)[source]
Request.headers[source]
Request.post(session=None)[source]
Request.recipient_validators[source]
Request.response = None

Holds the response after the HTTP request is performed.

Request.session_or_acquire(*args, **kwds)[source]
Request.sign_request(subscriber, data)[source]
Request.timeout_errors = (<class 'requests.exceptions.Timeout'>,)

Tuple of exceptions considered a timeout error.

Request.urlident[source]

Used to order HTTP requests by URL.

Request.user_agent = u'Mozilla/5.0 (compatible; thorn/1.5.0; python-requests/2.11.1)'

HTTP User-Agent header.

Request.validate_recipient(url)[source]
Request.value