Ian Cordasco | f453d41 | 2013-04-06 00:13:44 -0400 | [diff] [blame] | 1 | uritemplate |
| 2 | =========== |
| 3 | |
Ian Stapleton Cordasco | 6eaef88 | 2017-07-26 06:51:25 -0500 | [diff] [blame] | 4 | Documentation_ -- GitHub_ -- Travis-CI_ |
Ian Cordasco | e07a0fa | 2013-05-14 12:49:46 -0400 | [diff] [blame] | 5 | |
Ian Cordasco | 55244c2 | 2013-05-14 12:57:33 -0400 | [diff] [blame] | 6 | Simple python library to deal with `URI Templates`_. The API looks like |
| 7 | |
| 8 | .. code-block:: python |
Ian Cordasco | f453d41 | 2013-04-06 00:13:44 -0400 | [diff] [blame] | 9 | |
Ian Cordasco | 39af7fd | 2013-05-11 14:44:11 -0400 | [diff] [blame] | 10 | from uritemplate import URITemplate, expand |
Ian Cordasco | f453d41 | 2013-04-06 00:13:44 -0400 | [diff] [blame] | 11 | |
Jeff Potter | 623ab51 | 2014-05-07 15:50:12 -0600 | [diff] [blame] | 12 | # NOTE: URI params must be strings not integers |
| 13 | |
Ian Cordasco | 39af7fd | 2013-05-11 14:44:11 -0400 | [diff] [blame] | 14 | gist_uri = 'https://api.github.com/users/sigmavirus24/gists{/gist_id}' |
| 15 | t = URITemplate(gist_uri) |
Jeff Potter | 623ab51 | 2014-05-07 15:50:12 -0600 | [diff] [blame] | 16 | print(t.expand(gist_id='123456')) |
Ian Cordasco | f453d41 | 2013-04-06 00:13:44 -0400 | [diff] [blame] | 17 | # => https://api.github.com/users/sigmavirus24/gists/123456 |
| 18 | |
| 19 | # or |
Jeff Potter | 623ab51 | 2014-05-07 15:50:12 -0600 | [diff] [blame] | 20 | print(expand(gist_uri, gist_id='123456')) |
Ian Cordasco | f453d41 | 2013-04-06 00:13:44 -0400 | [diff] [blame] | 21 | |
Ian Cordasco | 39af7fd | 2013-05-11 14:44:11 -0400 | [diff] [blame] | 22 | # also |
Jeff Potter | 623ab51 | 2014-05-07 15:50:12 -0600 | [diff] [blame] | 23 | t.expand({'gist_id': '123456'}) |
| 24 | print(expand(gist_uri, {'gist_id': '123456'})) |
Ian Cordasco | f453d41 | 2013-04-06 00:13:44 -0400 | [diff] [blame] | 25 | |
Ian Cordasco | 55244c2 | 2013-05-14 12:57:33 -0400 | [diff] [blame] | 26 | Where it might be useful to have a class |
| 27 | |
| 28 | .. code-block:: python |
Ian Cordasco | e07a0fa | 2013-05-14 12:49:46 -0400 | [diff] [blame] | 29 | |
| 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 | |
| 40 | When the module containing this class is loaded, ``GitHubUser.url`` is |
| 41 | evaluated and so the template is created once. It's often hard to notice in |
| 42 | Python, 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 |
| 44 | reduce the amount of time your code takes to run. |
| 45 | |
Ian Cordasco | 780e94c | 2013-05-14 13:06:45 -0400 | [diff] [blame] | 46 | Installing |
| 47 | ---------- |
| 48 | |
| 49 | :: |
| 50 | |
Thomas Grainger | 1f546ef | 2016-11-22 15:39:28 +0000 | [diff] [blame] | 51 | pip install uritemplate |
Ian Cordasco | 780e94c | 2013-05-14 13:06:45 -0400 | [diff] [blame] | 52 | |
Ian Cordasco | e07a0fa | 2013-05-14 12:49:46 -0400 | [diff] [blame] | 53 | License |
| 54 | ------- |
| 55 | |
| 56 | Modified BSD license_ |
| 57 | |
| 58 | |
Ian Stapleton Cordasco | 6eaef88 | 2017-07-26 06:51:25 -0500 | [diff] [blame] | 59 | .. _Documentation: https://uritemplate.readthedocs.io/ |
| 60 | .. _GitHub: https://github.com/python-hyper/uritemplate |
| 61 | .. _Travis-CI: https://travis-ci.org/python-hyper/uritemplate |
Ian Cordasco | f453d41 | 2013-04-06 00:13:44 -0400 | [diff] [blame] | 62 | .. _URI Templates: http://tools.ietf.org/html/rfc6570 |
Ian Stapleton Cordasco | 6eaef88 | 2017-07-26 06:51:25 -0500 | [diff] [blame] | 63 | .. _license: https://github.com/python-hyper/uritemplate/blob/master/LICENSE |