Go to file
Brian Korty 34ecbbfad5 Merge pull request #1 from sprockets/initial-implementation
Initial implementation
2016-02-29 17:02:12 -05:00
docs Polish the documentation a little. 2016-02-28 09:00:15 -05:00
examples Split "server" parameter into "scheme", "host", and "port". 2016-02-24 08:00:39 -05:00
requires Initial implementation of ClientMixin. 2016-01-07 19:59:55 -05:00
sprockets Export HTTPClient and HTTPError in the top-level module. 2016-02-28 08:59:57 -05:00
tests HTTPClient: Add headers property. 2016-02-28 08:44:16 -05:00
.gitignore SYN 2016-01-07 19:58:39 -05:00
.travis.yml Add Travis CI control file. 2016-02-28 09:00:31 -05:00
LICENSE SYN 2016-01-07 19:58:39 -05:00
MANIFEST.in SYN 2016-01-07 19:58:39 -05:00
README.rst tests: Enable local httpbin server for testing. 2016-01-31 09:26:07 -05:00
setup.cfg SYN 2016-01-07 19:58:39 -05:00
setup.py Initial implementation of ClientMixin. 2016-01-07 19:59:55 -05:00
tox.ini SYN 2016-01-07 19:58:39 -05:00

README.rst

sprockets.clients.http
======================

Simplifies calling HTTP APIs from Tornado-based applications.

This library implements a mix-in class that adds a single method which
makes a HTTP request and calls a callback when an error occurs.  The
request is made using Tornado's asynchronous HTTP client so it does
not block the active IO loop.

.. code-block:: python

   from sprockets.clients import http
   from tornado import gen, web

   class MyHandler(http.ClientMixin, web.RequestHandler):

       @gen.coroutine
       def get(self):
           response = yield self.make_http_request(
               'GET', some_server_url, on_error=self.handle_api_error)
           if self._finished:
               yield gen.Return()

           # handle response as you wish
           # ...
           self.finish()

       def handle_api_error(self, request, error):
           self.send_error(error.code)

That's it.  What you do not see is asynchronous client usage and logging
that happens inside of the library.  There is some setup code in
``ClientMixin.initialize`` so make sure to call the super implementation
if you implement ``initialize`` in your request handler.

Running Tests
-------------
The test cases use the most excellent httpbin.org site to poke an HTTP API
that responds to errors in a reliable manner. However this can cause failures
if you happen to get rate limited by httpbin.org. If you want to be nice to
the maintainers of httpbin.org, you can run a version in `docker`_ and point
the tests at it instead.  The following snippet shows how to do this using
`docker-machine`_ installed via `docker-toolbox`_:

.. code-block:: bash

   $ eval "$(docker-machine env default)"
   $ machine_id=$(docker run -d -p 8000 citizenstig/httpbin)
   $ export HTTPBIN_HOST=$(docker-machine ip default)
   $ export HTTPBIN_PORT=$(docker port $machine_id 8000 | cut -d: -f2)
   $ env/bin/nosetests
   ........
   ----------------------------------------------------------------------
   Ran 8 tests in 0.255s

   OK
   $ docker stop $machine_id | xargs docker rm

.. _docker: https://www.docker.com
.. _docker-machine: https://www.docker.com/products/docker-machine
.. _docker-toolbox: https://www.docker.com/products/docker-toolbox