Represents a remote HTTP-accessible resource (and operations thereon).
Example usage:
>>> api = API(u'https://graph.facebook.com/')
>>> api
<intessa.API(u'https://graph.facebook.com/')>
Build paths using attribute access or subscription syntax:
>>> api[u'19292868552']
<intessa.API(u'https://graph.facebook.com/19292868552')>
>>> api.cocacola
<intessa.API(u'https://graph.facebook.com/cocacola')>
>>> api[u'btaylor'][u'picture']
<intessa.API(u'https://graph.facebook.com/btaylor/picture')>
Perform requests and receive responses by calling the object:
>>> api[u'19292868552']()
<intessa.Response[200, text/javascript]>
Simple string concatenation is reflexive.
>>> API(u'http://example.com/') + u'foo'
<intessa.API(u'http://example.com/foo')>
Add parameters to this URL.
Accepts pairs or dictionaries:
>>> API(u'http://example.com/') & ('a', 'b')
<intessa.API(u'http://example.com/?a=b')>
>>> API(u'http://example.com/') & {'a': 'b'}
<intessa.API(u'http://example.com/?a=b')>
Multiple additions of the same parameter will keep all of them, in the order they were specified:
>>> API(u'http://example.com/') & ('a', 'b') & ('a', 'c')
<intessa.API(u'http://example.com/?a=b&a=c')>
Perform an HTTP request and return a Response.
Parameters: |
|
---|
Note that if a non-successful response is returned, an instance of intessa.Error will be raised.
Dynamic attribute access adds path components to the current URL.
>>> API(u'http://example.com/').users
<intessa.API(u'http://example.com/users')>
Subscription syntax is overridden to extend or modify the current URL.
Example usage:
>>> api = API(u'http://example.com/')
In the simplest case, add a path component:
>>> api['foo']
<intessa.API(u'http://example.com/foo')>
>>> api['foo']['bar']
<intessa.API(u'http://example.com/foo/bar')>
Replace the whole path by prefixing with a ‘/’:
>>> api['foo']['bar']['/baz']
<intessa.API(u'http://example.com/baz')>
Add or modify a fragment identifier:
>>> api['#foo']
<intessa.API(u'http://example.com/#foo')>
>>> api['#foo']['#bar']
<intessa.API(u'http://example.com/#bar')>
Add or modify a file extension:
>>> api['foo']['.json']
<intessa.API(u'http://example.com/foo.json')>
>>> api['foo']['.json']['.xml']
<intessa.API(u'http://example.com/foo.xml')>
Adding a file extension to a non-leaf node will add an ‘index’ component:
>>> api['.json']
<intessa.API(u'http://example.com/index.json')>
Any necessary escaping will be taken care of:
>>> api['foo bar baz']
<intessa.API(u'http://example.com/foo%20bar%20baz')>
>>> api['#foo bar#baz']
<intessa.API(u'http://example.com/#foo%20bar%23baz')>
>>> api['foo']['.js o%n']
<intessa.API(u'http://example.com/foo.js%20o%25n')>
Add parameters to this URL, replacing existing parameters.
Accepts pairs or dictionaries:
>>> API(u'http://example.com/') | ('a', 'b')
<intessa.API(u'http://example.com/?a=b')>
>>> API(u'http://example.com/') | {'a': 'b'}
<intessa.API(u'http://example.com/?a=b')>
Multiple additions of the same parameter will replace old values:
>>> API(u'http://example.com/') | ('a', 'b') | ('a', 'c')
<intessa.API(u'http://example.com/?a=c')>
Add/replace a file extension on this URL.
Example:
>>> API(u'http://example.com/foo')._with_file_ext('.json')
<intessa.API(u'http://example.com/foo.json')>
The leading period is optional:
>>> API(u'http://example.com/foo')._with_file_ext('json')
<intessa.API(u'http://example.com/foo.json')>
Existing file extensions will be replaced:
>>> API(u'http://example.com/foo.json')._with_file_ext('xml')
<intessa.API(u'http://example.com/foo.xml')>
This method only works on leaf nodes:
>>> API(u'http://example.com/foo/')._with_file_ext('json')
Traceback (most recent call last):
...
ValueError: Cannot add a file extension to directories.
Replace the URL on this API with another.