python的声明性http客户端。
uplink的Python项目详细描述
- 为使用webapi构建可重用对象。
- 与requests、aiohttp和twisted一起使用。
- 灵感来自Retrofit
使用Github API v3
上行链路将您的http api转换为python类。
fromuplinkimportConsumer,get,Path,QueryclassGitHub(Consumer):"""A Python Client for the GitHub API."""@get("users/{user}/repos")defget_repos(self,user:Path,sort_by:Query("sort")):"""Retrieves the user's public repositories."""
构建一个与webservice交互的实例。
github=GitHub(base_url="https://api.github.com/")
然后,执行http请求就像调用方法一样简单。
repos=github.get_repos(user="octocat",sort_by="created")
返回的对象是友好的^{tt1}$:
print(repos.json())# Output: [{'id': 64778136, 'name': 'linguist', ...
对于发送非阻塞请求,上行链路支持 ^{tt2}$ and ^{tt3}$。
准备好用上行链路启动第一个api客户端了吗?从这个开始quick tutorial!
特点
- 快速定义结构化API客户端
- 使用decorators和类型提示来描述每个http请求
- json、url编码、多部分请求正文和文件上载
- URL参数替换、请求头和查询参数支持
- 自带HTTP库
- Non-blocking I/O support用于aiohttp和twisted
- Supply your own session(例如requests.Session)用于更大的控制
- 简单透明的反序列化/序列化
- 为自己的对象定义custom converters
- 支持^{tt5}$模式和handling collections(例如,用户列表)
- 可扩展
- 安装附加功能的可选插件(例如protobuf support)
- 作为中间件组成custom response and error handling函数
- 身份验证
- 内置支持Basic Authentication
使用现有的Auth-Cub支持客户端(如^{tt6}$)
Uplink正式支持Python2.7&3.3-3.7。
安装
要安装最新的稳定版本,可以使用pip(或pipenv):
$ pip install -U uplink
如果您对最新版本感兴趣,请使用以下工具预览即将发布的版本:
$ pip install https://github.com/prkumar/uplink/archive/master.zip
额外的额外的!
此外,上行链路具有可选的集成和功能。您可以查看完整的列表 可获得的额外费用的here。
使用pip安装上行链路时,可以使用以下格式选择附加功能:
$ pip install -U uplink[extra1, extra2, ..., extraN]
例如,要安装aiohttp和marshmallow支持:
$ pip install -U uplink[aiohttp, marshmallow]
用户证明
Michael Kennedy(@mkennedy),播客Talk Python和Python Bytes的主持人-
Of course our first reaction when consuming HTTP resources in Python is to reach for Requests. But for structured APIs, we often want more than ad-hoc calls to Requests. We want a client-side API for our apps. Uplink is the quickest and simplest way to build just that client-side API. Highly recommended.
或Carmi(@liiight),notifiers维护者-
Uplink’s intelligent usage of decorators and typing leverages the most pythonic features in an elegant and dynamic way. If you need to create an API abstraction layer, there is really no reason to look elsewhere.
社区
加入Gitter上的对话,提出问题,提供反馈, 与其他用户见面!
贡献
想报告一个错误,请求一个特性,或贡献代码上行链路? 在Contribution Guide中查找起始位置。 感谢您花时间改进一个开源项目:purple_heart: