与restfulapi交互的一种简洁的表达方式。
chain-p的Python项目详细描述
与restfulapi交互的一种简洁的表达方式。它的灵感来自zmallen’s pygraylog。
chain是流行的requests包的一个小增强。通过将端点引用为 属性,它有效地将端点“链接”在一起,构建目标url。
chain是通过删除 代码中硬编码的url字符串。
chain使用requests包作为其http客户端,保留其参数和响应对象。 如果您已经使用requests作为http客户端,那么采用chain将很容易。
用法
例如this API:
# create a new client for the API >>> importchain>>> blogs=chain.Client('http://jsonplaceholder.typicode.com') # if you want the posts: >>> response=blogs.get.posts() # the response is the response object from the requests package >>> printresponse.json()
数字和特殊字符也支持使用字典符号
# Get the first post >>> response=blogs.get.posts[1]()>>> printresponse.json()
链参数是请求。请求参数:
# parameters are the same as requests.requests parameters >>> response=blogs.get.comments(params={'postId':'1'})>>> printresponse.json()>>> comment={'postId':1,'id':501,'name':'chain','email':'chain@code.com','body':'meh.'}>>> response=blogs.post.comments(json=comment)
安装
pip install chain_py
测试
python setup.py test
或:
tox
兼容性
适用于Python 2.7、3.3、3.4、3.5和3.6版
许可证
麻省理工学院许可。请求由apache许可证授权。请参见完整的LICENSE