blob: 2c9d98d2e5bdd55c438a38ee221b5be31310790f [file] [log] [blame]
Ian Cordascof453d412013-04-06 00:13:44 -04001uritemplate
2===========
3
Ian Stapleton Cordasco6eaef882017-07-26 06:51:25 -05004Documentation_ -- GitHub_ -- Travis-CI_
Ian Cordascoe07a0fa2013-05-14 12:49:46 -04005
Ian Cordasco55244c22013-05-14 12:57:33 -04006Simple python library to deal with `URI Templates`_. The API looks like
7
8.. code-block:: python
Ian Cordascof453d412013-04-06 00:13:44 -04009
Ian Cordasco39af7fd2013-05-11 14:44:11 -040010 from uritemplate import URITemplate, expand
Ian Cordascof453d412013-04-06 00:13:44 -040011
Jeff Potter623ab512014-05-07 15:50:12 -060012 # NOTE: URI params must be strings not integers
13
Ian Cordasco39af7fd2013-05-11 14:44:11 -040014 gist_uri = 'https://api.github.com/users/sigmavirus24/gists{/gist_id}'
15 t = URITemplate(gist_uri)
Jeff Potter623ab512014-05-07 15:50:12 -060016 print(t.expand(gist_id='123456'))
Ian Cordascof453d412013-04-06 00:13:44 -040017 # => https://api.github.com/users/sigmavirus24/gists/123456
18
19 # or
Jeff Potter623ab512014-05-07 15:50:12 -060020 print(expand(gist_uri, gist_id='123456'))
Ian Cordascof453d412013-04-06 00:13:44 -040021
Ian Cordasco39af7fd2013-05-11 14:44:11 -040022 # also
Jeff Potter623ab512014-05-07 15:50:12 -060023 t.expand({'gist_id': '123456'})
24 print(expand(gist_uri, {'gist_id': '123456'}))
Ian Cordascof453d412013-04-06 00:13:44 -040025
Ian Cordasco55244c22013-05-14 12:57:33 -040026Where it might be useful to have a class
27
28.. code-block:: python
Ian Cordascoe07a0fa2013-05-14 12:49:46 -040029
30 import requests
31
32 class GitHubUser(object):
33 url = URITemplate('https://api.github.com/user{/login}')
34 def __init__(self, name):
35 self.api_url = url.expand(login=name)
36 response = requests.get(self.api_url)
37 if response.status_code == 200:
38 self.__dict__.update(response.json())
39
40When the module containing this class is loaded, ``GitHubUser.url`` is
41evaluated and so the template is created once. It's often hard to notice in
42Python, but object creation can consume a great deal of time and so can the
43``re`` module which uritemplate relies on. Constructing the object once should
44reduce the amount of time your code takes to run.
45
Ian Cordasco780e94c2013-05-14 13:06:45 -040046Installing
47----------
48
49::
50
Thomas Grainger1f546ef2016-11-22 15:39:28 +000051 pip install uritemplate
Ian Cordasco780e94c2013-05-14 13:06:45 -040052
Ian Cordascoe07a0fa2013-05-14 12:49:46 -040053License
54-------
55
56Modified BSD license_
57
58
Ian Stapleton Cordasco6eaef882017-07-26 06:51:25 -050059.. _Documentation: https://uritemplate.readthedocs.io/
60.. _GitHub: https://github.com/python-hyper/uritemplate
61.. _Travis-CI: https://travis-ci.org/python-hyper/uritemplate
Ian Cordascof453d412013-04-06 00:13:44 -040062.. _URI Templates: http://tools.ietf.org/html/rfc6570
Ian Stapleton Cordasco6eaef882017-07-26 06:51:25 -050063.. _license: https://github.com/python-hyper/uritemplate/blob/master/LICENSE