API

class sprockets.mixins.http.HTTPClientMixin(*args, **kwargs)

Mixin for making http requests using the asynchronous http_fetch() method.

_http_req_apply_default_headers(request_headers, content_type, body)

Set default values for common HTTP request headers

Parameters:
  • request_headers (dict) – The HTTP request headers
  • content_type (ietfparse.datastructures.ContentType or str) – The mime-type used in the request/response
  • body (mixed) – The request body
Return type:

dict

_http_req_body_serialize(body, content_type)

Conditionally serialize the request body value if mime_type is set and it’s serializable.

Parameters:
  • body (mixed) – The request body
  • content_type (str) – The content type for the request body
Raises:

ValueError

http_fetch(url, method='GET', request_headers=None, body=None, content_type=<ietfparse.datastructures.ContentType application/msgpack, 0 parameters>, follow_redirects=False, max_redirects=None, connect_timeout=None, request_timeout=None, retry_timeout=None, max_http_attempts=None, auth_username=None, auth_password=None, user_agent=None, validate_cert=True, allow_nonstandard_methods=False, dont_retry=None, **kwargs)

Perform a HTTP request

Will retry up to max_http_attempts times with an exponentially increasing sleep time starting with retry_timeout seconds. If a Retry-Header is included in a response, then it will override the calculated sleep time.

Parameters:
  • url (str) – The URL for the request
  • method (str) – The HTTP request method, defaults to GET
  • request_headers (dict) – Headers to include in the HTTP request
  • body (mixed) – The HTTP request body to send with the request
  • content_type (ContentType or str) – The mime type to use for requests & responses. Defaults to application/msgpack
  • follow_redirects (bool) – Follow HTTP redirects when received
  • max_redirects (int) – Maximum number of redirects to follow, default is 5
  • connect_timeout (float) – Timeout for initial connection in seconds, default 20 seconds
  • request_timeout (float) – Timeout for entire request in seconds, default 20 seconds
  • retry_timeout (float) – Time to sleep between retries, default 3 seconds
  • max_http_attempts (int) – Maximum number of times to retry a request, default is 3 attempts
  • auth_username (str) – Username for HTTP authentication
  • auth_password (str) – Password for HTTP authentication
  • user_agent (str) – The str used for the User-Agent header, default used if unspecified.
  • validate_cert (bool) – For HTTPS requests, validate the server’s certificate? Default is True
  • allow_nonstandard_methods (bool) – Allow methods that don’t adhere to the HTTP spec.
  • dont_retry (set) – A list of status codes that will not be retried if an error is returned. Default: set({})
  • kwargs – additional keyword parameters are passed to tornado.httpclient.AsyncHTTPClient.fetch()
Return type:

HTTPResponse

Raises:

RuntimeError if the raise_error keyword argument is specified

_http_req_user_agent()

Return the User-Agent value to specify in HTTP requests, defaulting to service/version if configured in the application settings, or if used in a consumer, it will attempt to obtain a user-agent from the consumer’s process. If it can not auto-set the User-Agent, it defaults to sprockets.mixins.http/[VERSION].

Return type:str
_http_req_modify_for_retry(response: sprockets.mixins.http.HTTPResponse, attempt: int, url: str, headers: dict, body)

Implement this method to modify the request on each attempt.

Parameters:
  • response – the current HTTP response which includes both response and exception history
  • attempt – current attempt counter
  • url – current request URL
  • headers – current request headers
  • body – serialized request body

The default behavior is to add the X-Retry-Attempt header. You will need to implement this for protocols that include a one-time-use value such as the OAuth 1 request nonce.

Returns:a tuple containing the URL, headers, and body to use in the next request
class sprockets.mixins.http.HTTPResponse(simplify_error_response=True)

Encapsulate the response(s) for requests made using the http_fetch() method.

attempts

Return the number of HTTP attempts made by calculating the number of exceptions and responses the object contains.

Return type:int
body

Returns the HTTP response body, deserialized if possible.

Return type:mixed
code

Returns the HTTP status code of the response.

Return type:int
duration

Return the calculated duration for the total amount of time across all retries.

Return type:float
exceptions

Return the list of exceptions raised when making the request.

Return type:list(Exception)
headers

Return the HTTP Response headers as a dict.

Return type:dict
history

Return all of the HTTP responses for the request.

Return type:list(tornado.httpclient.HTTPResponse)

Return the parsed link header if it was set, returning a list of the links as a dict.

Return type:list(dict()) or None
ok

Returns True if the response status code was between 200 and 399. Returns False if no responses were received or the response status code was >= 400.

:rtype bool

raw

Return the raw tornado HTTP Response object

Return type:tornado.httpclient.HTTPResponse