用于处理HTTP API的异步辅助工具
项目描述
aio-apiclient – 用于处理HTTP API的异步辅助工具
特性
方便地处理任何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
贡献
许可
遵循MIT许可协议。
项目详情
下载文件
下载适用于您的平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。
源分布
aio-apiclient-1.7.0.tar.gz (8.0 kB 查看哈希)
构建分布
aio_apiclient-1.7.0-py3-none-any.whl (9.7 kB 查看哈希)
关闭
aio-apiclient-1.7.0.tar.gz的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 726e4facf48b1389ea89908ef556f4056707a9334616f0de5dbebeffa6d9de98 |
|
MD5 | 6727718557a4e2087666f287ef2bdda4 |
|
BLAKE2b-256 | ec25fcf1dd978878398f853e34e4be7ce108cdb1d6b78e106a58c11d31896ea6 |
关闭
aio_apiclient-1.7.0-py3-none-any.whl的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | d2fd0517c12c409b2cf240d54876d2616abc800a0c9c14807b777c8e802a56ab |
|
MD5 | fda04a4b6c70232912d023603cfdbeec |
|
BLAKE2b-256 | bac526cba5a4de9a9b65cf44f9cab38fd37c1e80957192a0e144d3a4477e718b |