Rejected Companion Library

This library contains a number of utility and helper classes that were embedded in consumers that I have written recently. They add useful functionality on top of rejected.

GitHub https://github.com/dave-shawley/vetoes
PyPI https://pypi.org/project/vetoes/
Docs https://pythonhosted.org/vetoes/
Coveralls https://coveralls.io/github/dave-shawley/vetoes
Travis https://travis-ci.org/dave-shawley/vetoes

HTTP API Helpers

class vetoes.service.HTTPServiceMixin(*args, **kwargs)

Mix this in to add the call_http_service() method.

Parameters:
  • service_map (dict) – mapping from logical “function” to HTTP service.
  • args – these are passed to rejected.consumer.Consumer() as-is
  • kwargs – these are passed to rejected.consumer.Consumer() as-is

The primary use of this mix-in is to make sending HTTP requests easier and safer. The call_http_service() method uses the get_service_url() to build the request URL for a named semantic function, send the request to a specific HTTP service, and perform some “opinionated” processing on the response. The separation of semantic function and HTTP service may seem a bit confusing at first. They are used to provide concise logging and well-named metrics. The semantic function describes the action being performed and the service is the actor performing the action.

The mapping from semantic function to HTTP service is handled by the service_map passed into the initializer. The mapping value is the HTTP service which is passed into get_service_url() to construct the request URL.

HTTP client behavior is controlled via consumer level configuration under the vetoes key. The following options are available:

Key Description
max_clients The max # of simultaneous requests that can be made
connect_timeout Timeout for initial connection in seconds
request_time Timeout for entire request in seconds

Example Configuration:

Application:
  Consumers:
    example:
      consumer: rejected.example.Consumer
      connections:
        - name: rabbitmq1
      qty: 2
      queue: generated_messages
      config:
        vetoes:
          max_clients: 10
          connect_timeout: 5.0
          request_timeout: 30.0
http_headers

tornado.httputil.HTTPHeaders instance of headers that are included in every request. This set is empty.

http

tornado.httpclient.AsyncHTTPClient used to make requests. The initializer sets the connect_timeout and request_timeout in self.http.defaults.

call_http_service(function, method, *path, **kwargs)

Send a HTTP request to a service.

Parameters:
  • function (str) – the function to invoke. The service is determined based on the service_map established during initialization.
  • method (str) – HTTP method to invoke.
  • path – path elements to the HTTP resource.
  • headers (dict) – optional set of headers to include in the message. These are in addition to http_headers.
  • json (dict or list) – optional body to send in the message. If this keyword is included, then the value is JSON encoded before being used as the body.
  • raise_error (bool) – if this keyword is included and set to False, then HTTP errors will be returned instead of raised as exceptions.
  • url (str) – if this keyword is included then it is used as-is instead of doing a service lookup.
  • kwargs – additional keyword arguments are passed to tornado.httpclient.AsyncHTTPClient.fetch().
Returns:

a tornado.httpclient.HTTPResponse instance

Return type:

tornado.httpclient.HTTPResponse

Raises:

tornado.httpclient.HTTPError if a HTTP error occurs

Raises:

rejected.consumer.ProcessingException if a low-level socket error occurs or a retry-able HTTP result is returned

get_service_url(service, *path, **kwargs)

Build a request URL for a specific service.

Parameters:
  • service (str) – name of the service to invoke
  • path – resource path elements
  • query_args (dict or None) – optional query parameters to include in the URL
Returns:

the request URL

Return type:

str

Note

You are required to override this method in your consumer. The base implementation simply raises an exception.

Release History

0.4.0 (13-Apr-2017)

0.3.0 (04-Apr-2017)

0.1.1 (06-Jan-2017)

  • Replaced readthedocs with pythonhosted.org.