aboutsummaryrefslogtreecommitdiff
path: root/README.rst
blob: 2c9d98d2e5bdd55c438a38ee221b5be31310790f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
uritemplate
===========

Documentation_ -- GitHub_ -- Travis-CI_

Simple python library to deal with `URI Templates`_. The API looks like

.. code-block:: python

    from uritemplate import URITemplate, expand

    # NOTE: URI params must be strings not integers

    gist_uri = 'https://api.github.com/users/sigmavirus24/gists{/gist_id}'
    t = URITemplate(gist_uri)
    print(t.expand(gist_id='123456'))
    # => https://api.github.com/users/sigmavirus24/gists/123456

    # or
    print(expand(gist_uri, gist_id='123456'))

    # also
    t.expand({'gist_id': '123456'})
    print(expand(gist_uri, {'gist_id': '123456'}))

Where it might be useful to have a class

.. code-block:: python

    import requests

    class GitHubUser(object):
        url = URITemplate('https://api.github.com/user{/login}')
        def __init__(self, name):
            self.api_url = url.expand(login=name)
            response = requests.get(self.api_url)
            if response.status_code == 200:
                self.__dict__.update(response.json())

When the module containing this class is loaded, ``GitHubUser.url`` is 
evaluated and so the template is created once. It's often hard to notice in 
Python, but object creation can consume a great deal of time and so can the 
``re`` module which uritemplate relies on. Constructing the object once should 
reduce the amount of time your code takes to run.

Installing
----------

::

    pip install uritemplate

License
-------

Modified BSD license_


.. _Documentation: https://uritemplate.readthedocs.io/
.. _GitHub: https://github.com/python-hyper/uritemplate
.. _Travis-CI: https://travis-ci.org/python-hyper/uritemplate
.. _URI Templates: http://tools.ietf.org/html/rfc6570
.. _license: https://github.com/python-hyper/uritemplate/blob/master/LICENSE