跳转到主要内容

用于处理HTTP API的异步辅助工具

项目描述

aio-apiclient – 用于处理HTTP API的异步辅助工具

Tests Status PYPI Version Python Versions

特性

  • 方便地处理任何HTTP API(特别是REST)

  • 支持使用httpx和aiohttp作为后端进行请求

  • 非常可配置和可用

  • 自动解析响应的能力

要求

  • python >= 3.7

安装

aio-apiclient应使用pip安装

pip install aio-apiclient

请注意,aio-apiclient需要安装支持的后端之一(aiohttp,httpx)才能工作。

您可以选择使用库安装要求

# For httpx
pip install aio-apiclient[httpx]

# For aiohttp
pip install aio-apiclient[aiohttp]

快速入门

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)

等等

使用

初始化

客户端初始化需要一个所需API的根URL。

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, ...)
    # ...

)

应用程序关闭

API客户端支持优雅的关闭。当您完成应用程序时,运行await client.shutdown()(不是必需的)。

中间件

您可以使用中间件动态更改请求参数(方法、URL、其他后端参数)。

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

错误跟踪器

如果您有任何建议、错误报告或不满,请通过以下问题跟踪器报告:https://github.com/klen/aio-apiclient/issues

贡献

项目的开发地址:https://github.com/klen/aio-apiclient

许可

遵循MIT许可协议

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

aio-apiclient-1.7.0.tar.gz (8.0 kB 查看哈希)

上传时间: 源代码

构建分布

aio_apiclient-1.7.0-py3-none-any.whl (9.7 kB 查看哈希)

上传时间: Python 3

支持者: