HTTP for lazy, impatient, hubristic humans.
Project description
Clients provide requests wrappers which encourage best practices, particularly always using Sessions to connect to the same host or api endpoint.
Usage
Typical requests usage is redundant and inefficient, by not taking advantage of connection pooling.
r = requests.get('https://api.github.com/user', headers={'authorization': token})
r = requests.get('https://api.github.com/user/repos', headers={'authorization': token})
Using sessions is the better approach, but more verbose and in practice requires manual url joining.
s = requests.Session()
s.headers['authorization'] = token
r = s.get('https://api.github.com/user')
r = s.get('https://api.github.com/user/repos')
Clients make using sessions easier, with implicit url joining.
client = clients.Client('https://api.github.com/', headers={'authorization': token})
r = client.get('user')
r = client.get('user/repos')
Resources extend Clients to implicitly handle response content, with proper checking of status_code and content-type.
resource = clients.Resource('https://api.github.com/', headers={'authorization': token})
for repo in resource.get('user/repos'):
...
Being session based, Clients also work seamlessly with other requests adapters, such as CacheControl.
Asynchronous Clients are also provided, using aiohttp instead of requests.
See documentation for more examples.
Installation
$ pip install clients
Dependencies
Python 2.7, 3.4+
requests 2.4.2+
aiohttp (optional)
Tests
100% branch coverage.
$ pytest [--cov]
Changes
0.4
Asynchronous clients and resources
0.3
singleton decorator
0.2
Resource attribute upcasts back to a client
iter and download implement GET requests with streamed content
create implements POST request and returns Location header
update implements PATCH request with json params
__call__ implements GET request with params
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
Built Distribution
Hashes for clients-0.4-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ee2b4b08ba6395c6e0124beaef4852d10053ca9d84a002750494905d5c3ab9b |
|
MD5 | 6b77bfb9f11c6e07b68aa8e7b33db7a4 |
|
BLAKE2b-256 | b0421097d3040f9cada3bdec38826251d469269a0576bbaeabf9b6780f2bd818 |