Skip to main content

A tiny library for creating wrappers around external APIs

Project description

Tortilla
========

.. image:: https://travis-ci.org/redodo/tortilla.svg
:target: https://travis-ci.org/redodo/tortilla

Wrapping web APIs made easy.

Installation:

.. code:: bash

pip install --pre tortilla

Quick usage:

.. code:: python

>>> import tortilla
>>> github = tortilla.wrap('https://api.github.com')
>>> redodo = github.users.get('redodo')
>>> redodo.id
2227416

Intro
-----

In the following examples we can assume that the following code is
already executed:

.. code:: python

import tortilla
api = tortilla.wrap('https://api.example.org')

Tortilla 101
------------

A quick overview of Tortilla's flow:

1. You form a chain of URL parts
2. You invoke a ``get``, ``post``, ``patch``, ``put`` or ``delete``
method at the end of the chain
3. Tortilla requests the URL
4. Tortilla tries to JSON decode to response and puts it in a ``Bunch``
object (dictionary accessible via dots)
5. Tortilla returns the *bunched* object

Headers
-------

Most APIs require a authentication token before you can access its
endpoints.

Those can be set in the headers of a wrapped object:

.. code:: python

api.headers.token = 'super secret token'

Or if you first need to login before getting a token:

.. code:: python

auth = api.auth.post(data={'username': 'foo', 'password': 'bar'})
api.headers.token = auth.token

If the header key contains a dot or is a reserved keyword you can use
the key instead of the attribute:

.. code:: python

api.headers['from'] = 'stuff'
api.headers['secret.key'] = 'more stuff'

Caching
-------

Caching stuff is easy:

.. code:: python

api.cache_lifetime = 3600 # seconds
# OR
api = tortilla.wrap('https://api.example.org', cache_lifetime=3600)

If you want to ignore the cache and force a reload:

.. code:: python

api.some.endpoint.get(ignore_cache=True)

URL Extensions
--------------

If the endpoints of your target API require an extension for the
response formatting you can set the ``extension`` parameter:

.. code:: python

api.extension = 'json'
# OR
api = tortilla.wrap('https://api.example.org', extension='json')

This can be overwritten per request or URL part:

.. code:: python

api.special.case.extension = 'json'
api.special.case.stuff.get()
# requests: https://api.example.org/special/case/stuff.json

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

tortilla-0.1.0.dev3-py2.py3-none-any.whl (8.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file tortilla-0.1.0.dev3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for tortilla-0.1.0.dev3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fce92c2701ad069f03cc9610a23f2b9d9743d363747d193754ae5fb52b5e7d7b
MD5 6a0a8c8b75dd05bdb7db92862040f50e
BLAKE2b-256 f813549b3eee7702aab912d5350f1f3546c848b52f7ca60545f2f73de33d20cf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page