Skip to main content

HTTP for humanitarians.

Project description

image image image image image image image image image image

Clients provide requests and httpx 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')

Client

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')

Resource

Resources extend Clients to implicitly handle response content, with proper checking of status_code and content-type.

github = clients.Resource('https://api.github.com/', headers={'authorization': token})
for repo in github.get('user/repos', params={'visibility': 'public'}):
    ...

Resources also implement syntactic support for methods such as getattr and call, providing most of the benefits of custom clients as is.

for repo in github.user.repos(visibility='public'):
    ...

Being session based, Clients work seamlessly with other requests adapters, such as CacheControl. Asynchronous variants of all client types are provided in Python 3, using httpx instead of requests. Additional clients for RPC, GraphQL, and proxies also provided.

Installation

$ pip install clients

Dependencies

  • requests >=2.4.2
  • httpx >=0.8 (if Python >=3.6)

Tests

100% branch coverage.

$ pytest [--cov]

Roadmap

aiohttp has made several incompatible changes, including forbidding subclassing. So the async client has switched to httpx.

There are no interface changes to the async client itself, other than the different response object. In the future clients may switch to only httpx or requests3.

Changes

1.1

  • Async switched to httpx

1.0

  • Allow missing content-type
  • Oauth access tokens supported in authorization header

0.5

  • AsyncClient default params
  • Remote and AsyncRemote procedure calls
  • Graph and AsyncGraph execute GraphQL queries
  • Proxy and AsyncProxy clients

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

clients-1.1.tar.gz (18.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

clients-1.1-py2.py3-none-any.whl (9.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file clients-1.1.tar.gz.

File metadata

  • Download URL: clients-1.1.tar.gz
  • Upload date:
  • Size: 18.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.15.1 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/2.7.15

File hashes

Hashes for clients-1.1.tar.gz
Algorithm Hash digest
SHA256 66a424acb18a5797535ed9219e9dfab8b86948c2b913491c1ab1c3299bdddf11
MD5 5883a06ed156a9b6c8b03919685df8e4
BLAKE2b-256 043ed74eccbfcfaea48c8f1d642e96b8e44db0e4e8eec1f25810cb85745c483c

See more details on using hashes here.

File details

Details for the file clients-1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: clients-1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.15.1 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/2.7.15

File hashes

Hashes for clients-1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b203e961e078876cf967ffd9808360c22634c48a1c6de29713f692e1b7999775
MD5 f91f24ec8ca84e88eca3a6e8866e9a51
BLAKE2b-256 735109b56a9b1e4b80a375307db0e3e911a280b0e71762612133f58a0a602463

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page