任何HTTP API的简单异步客户端
aio-apiclient的Python项目详细描述
aio apiclient–适用于任何httpapi的简单异步客户端
Features
- 使用任何httpapi(尤其是REST)可以方便地工作
- 支持httpx和aiohttp作为发出请求的后端
- 非常可配置和可用
- 自动分析响应的能力
Requirements
- python>;=3.4
QuickStart
Github API(https://developer.github.com/v4/):
^{pr2}$松弛API(https://api.slack.com/web):
fromapiclientimportAPIClientclient=APIClient('https://api.github.com',headers={'Authorization':'token OAUTH-TOKEN'})# Update current user status (we don't care about this response)awaitclient.api['users.profile.set'].post(json={'profile':{'status_text':'working','status_emoji':':computer:''status_expiration':30,}},read_response_body=False)
等等
Usage
Initialization
客户端初始化需要所需API的根URL。在
fromapiclientimportAPIClientclient=APIClient(# Root URL for any API (required)'https://api.github.com',# Raise `client.Error` for any response with status code > 400raise_for_status=True,# Set to `False` if you only want to make a request and doesn't care about responsesread_response_body=True,# Parse response's body content-type and return JSON/TEXT/Form data instead the response itself# Set total timeout in secondstimeout=10.0,# Set backend for making requests (apiclient.backends.BackendHTTPX,# apiclient.backends.BackendAIOHTTP) by default first available would be# choosenbackend=None,# Default backend options to use with every request (headers, params, data, ...)# ...)
App Shutdown
api客户端支持优雅关闭。跑等待客户端关闭()何时 你正在完成你的应用程序(不需要)。在
Middlewares
您可以使用中间件对请求参数(方法、url、其他后端参数)进行日常更改。在
importtimefromapiclientimportAPIClientclient=APIClient('https://api.github.com')@client.middlewareasyncdefinsert_timestamp_header(method,url,options):options.setdefault('headers',{})options['headers']['X-Timestamp']=str(time.time())returnmethod,url,options
Bug tracker
如果您有任何建议,错误报告或 烦恼请向问题追踪者报告 在https://github.com/klen/aio-apiclient/issues
License
根据MIT license授权。在
- 项目
标签: