Skip to main content

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.

Release files for aio-apiclient 1.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aio-apiclient 1.7.0
File Size Uploaded
aio-apiclient-1.7.0.tar.gz 8.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aio-apiclient 1.7.0
File Interpreter ABI Platform
aio_apiclient-1.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.6 kB

Release files / aio-apiclient-1.7.0.tar.gz

Download URL aio-apiclient-1.7.0.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
726e4facf48b1389ea89908ef556f4056707a9334616f0de5dbebeffa6d9de98
BLAKE2b-256 checksum
How to use checksums
ec25fcf1dd978878398f853e34e4be7ce108cdb1d6b78e106a58c11d31896ea6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / aio_apiclient-1.7.0-py3-none-any.whl

Download URL aio_apiclient-1.7.0-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d2fd0517c12c409b2cf240d54876d2616abc800a0c9c14807b777c8e802a56ab
BLAKE2b-256 checksum
How to use checksums
bac526cba5a4de9a9b65cf44f9cab38fd37c1e80957192a0e144d3a4477e718b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release history Release notifications | RSS feed

This release

1.7.0 This release

2 release files

1.6.2

2 release files

1.6.1

2 release files

1.6.0

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page