Skip to main content

Async helper to work with HTTP APIs

Project description

aio-apiclient – Async helper to work with HTTP APIs

Tests Status PYPI Version Python Versions

Features

  • Convenient work with any HTTP API (especially with REST)

  • Supports httpx and aiohttp as backends to make requests

  • Very configurable and usable

  • An ability to parse responses automatically

Requirements

  • python >= 3.7

Installation

aio-apiclient should be installed using pip:

pip install aio-apiclient

Please note that aio-apiclient requires any of supported http backends (aiohttp, httpx) to be installed in order for it to work.

You may optionally install the requirements with the library:

# For httpx
pip install aio-apiclient[httpx]

# For aiohttp
pip install aio-apiclient[aiohttp]

QuickStart

Github API (https://developer.github.com/v4/):

from apiclient import APIClient

# First available backend (aiohttp, httpx) will be used
client = APIClient('https://api.github.com', headers={
        'Authorization': 'token OAUTH-TOKEN'
})

# Read information about the current repository
repo = await client.api.repos.klen['aio-apiclient'].get()
print(repo)  # dict parsed from Github Response JSON

Slack API (https://api.slack.com/web):

from apiclient import APIClient

client = APIClient('https://api.github.com', headers={
    'Authorization': 'token OAUTH-TOKEN'
})

# Update current user status (we don't care about this response)
await client.api['users.profile.set'].post(json={
    'profile': {
        'status_text': 'working',
        'status_emoji': ':computer:'
        'status_expiration': 30,
    }
}, read_response_body=False)

And etc

Usage

Initialization

The Client initialization requires root URL for a required API.

from apiclient import APIClient

client = APIClient(

    # Root URL for any API (required)
    'https://api.github.com',

    # Raise `client.Error` for any response with status code > 400
    raise_for_status=True,

    # Set to `False` if you only want to make a request and doesn't care about responses
    read_response_body=True,

    # Parse response's body content-type and return JSON/TEXT/Form data instead the response itself

    # Set total timeout in seconds
    timeout=10.0,

    # Set backend type for making requests (apiclient.backends.BackendHTTPX,
    # apiclient.backends.BackendAIOHTTP) by default first available would be
    # choosen

    backend_type=None,

    # Default backend options to use with every request (headers, params, data, ...)
    # ...

)

App Shutdown

The api client support graceful shutdown. Run await client.shutdown() when you are finishing your app (not necessary).

Middlewares

You are able to dinamically change request params (method, url, other backend params) using middlewares.

import time
from apiclient import APIClient

client = APIClient('https://api.github.com')

@client.middleware
async def insert_timestamp_header(method, url, options):
    options.setdefault('headers', {})
    options['headers']['X-Timestamp'] = str(time.time())
    return method, url, options

Bug tracker

If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/aio-apiclient/issues

Contributing

Development of the project happens at: https://github.com/klen/aio-apiclient

License

Licensed under a MIT license.

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

aio-apiclient-1.6.1.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

aio_apiclient-1.6.1-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file aio-apiclient-1.6.1.tar.gz.

File metadata

  • Download URL: aio-apiclient-1.6.1.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8

File hashes

Hashes for aio-apiclient-1.6.1.tar.gz
Algorithm Hash digest
SHA256 e888f293e5887079de660fa581c9e036967a4f6bdf857d6080ab856c38a2a7a4
MD5 f0342e5e5af6b1ce9c85e0cc64fe9ec2
BLAKE2b-256 a80e2a13b7987e308bc5678abaf2a8c713e13e49e1ce669ea6bf3f3441c05d3c

See more details on using hashes here.

File details

Details for the file aio_apiclient-1.6.1-py3-none-any.whl.

File metadata

  • Download URL: aio_apiclient-1.6.1-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8

File hashes

Hashes for aio_apiclient-1.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4821ddfbf691b4bb0da2722b07d6bb28f7ad113c91eb05a8f95ab555c5d97071
MD5 b9c3843a45d00a915246068313ee25c2
BLAKE2b-256 f40938049904a003531867fc3dc5f56d04a8d007b3a3c64b16f5bd145fcd9b30

See more details on using hashes here.

Supported by

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