Skip to main content

A tiny library for creating wrappers around web APIs

Project description

Build Status Coverage Docs Version Python versions License

Wrapping web APIs made easy.

Installation via PIP:

pip install tortilla

Quick usage overview:

>>> import tortilla
>>> github = tortilla.wrap('https://api.github.com')
>>> user = github.users.get('octocat')
>>> user.location
u'San Francisco'

The Basics

Tortilla uses a bit of magic to wrap APIs. Whenever you get or call an attribute of a wrapper, the URL is appended by that attribute’s name or method parameter. Let’s say we have the following code:

id, count = 71, 20
api = tortilla.wrap('https://api.example.org')
api.video(id).comments.get(count)

Every attribute and method call represents a part of the URL:

api         -> https://api.example.org
.video      -> /video
(id)        -> /71
.comments   -> /comments
.get(count) -> /20
Final URL   -> https://api.example.org/video/71/comments/20

The last part of the chain (.get()) executes the request. It also (optionally) appends one last part to the URL. Which allows you to do stuff like this:

api.video.get(id)
# instead of this
api.video(id).get()

So to summarize, getting attributes is used to define static parts of a URL and calling them is used to define dynamic parts of a URL.

Once you’ve chained everything together, Tortilla will execute the request and parse the response for you.

At the moment, Tortilla only accepts JSON-formatted responses. Supporting more formats is on the roadmap for future Tortilla versions.

The parsed response will be bunchified which makes dictionary keys accessible through attributes. So, say we get the following JSON response for the user ‘john’:

{"name": "John Doe"}

If we request this with an already created wrapper, we can access the response data through attributes:

>>> user = api.users.get('john')
>>> user.name
u'John Doe'

Headers

A common requirement for accessing APIs is providing authentication data. This usually has to be described in the headers of each request. Tortilla makes it very easy for you to describe those recurring headers:

api.headers.token = 'secret authentication token'

You can also define custom headers per request:

api.endpoint.get(headers={'this': 'that'})

These headers will be appended to the existing headers of the wrapper.

Parameters

URL parameters can be defined per request in the params option:

api.search.get(params={'q': 'search query'})

Caching

Some APIs have a limit on the amount of requests you can make. In these cases, caching can be very helpful. You can activate this with the cache_lifetime parameter:

api = tortilla.wrap('https://api.example.org', cache_lifetime=100)

All the requests made on this wrapper will now be cached for 100 seconds. If you want to ignore the cache in a specific situation, you can use the ignore_cache parameter:

api.special.request.get(ignore_cache=True)

The response will now be reloaded.

URL Extensions

APIs like Twitter’s require an extension in the URL that specifies the response format. This can be defined in the extension parameter:

api = tortilla.wrap('https://api.twitter.com/1.1', extension='json')

This option can be overridden with every request or subwrap:

api.special.endpoint.extension = 'xml'
api.special.endpoint.get(extension='xml')

Debugging

Activating debug mode can be done with the debug parameter:

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

You can override the debug parameter per request:

api.stuff.get(debug=False)
api.other.stuff.get(debug=True)

An example using the GitHub API:

>>> user = github.users.get('octocat')
Executing GET request:
    URL:     https://api.github.com/users/octocat
    headers: {}
    query:   None
    data:    None

Got 200 OK:
    {u'public_repos': 5, u'site_admin': ...

Enjoy your data.

Project details


Download files

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

Source Distribution

tortilla-0.4.0b1.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

tortilla-0.4.0b1-py2.py3-none-any.whl (10.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file tortilla-0.4.0b1.tar.gz.

File metadata

  • Download URL: tortilla-0.4.0b1.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for tortilla-0.4.0b1.tar.gz
Algorithm Hash digest
SHA256 3b8c0ecac5988304fd2c15f17d68d7521a413282691258457e5768986ea3a4af
MD5 a238e0681ec35d40af93bbd37f070684
BLAKE2b-256 3c12a1ca08db7aa526f5626b32b76061380eca96f6b214f08447a960729a417d

See more details on using hashes here.

File details

Details for the file tortilla-0.4.0b1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for tortilla-0.4.0b1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 69e8ee2108f66db74eda2f477b47f990e4fe2792d68eda15f0a9ebc0e4b52235
MD5 e1b7b71c4e21b2f1cad9ecc20d6aa232
BLAKE2b-256 dc50447ad2f61f77cb5eb45c80bae808cde9a999e722f16e03d6e6fab82bdcfd

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