没有项目描述
genericclient的Python项目详细描述
基于requests的restfulapi的通用客户机。
对于基于aiohttp的异步版本,请参见genericclient-aiohttp(仅限python 3.5+)。
安装
$ pip install genericclient
快速启动
from genericclient import GenericClient myclient = GenericClient(api_url) myresource = myclient.resources.get(id=1) actives = myclient.posts.filter(active=True)
用法
实例化
myclient = GenericClient(url, auth=None, session=None, adapter=None, trailing_slash=False, autopaginate=None)
参数:
- url:api的根url
- auth:API的身份验证。您可以传递requests可以接受为身份验证的任何内容。
- session:传递一个会话实例,让requests使用该会话。如果None(默认值),它将为您实例化requests.session的实例。
- adapter:用于requests的可选会话适配器。
- trailing_slash:如果api的url以/ 结尾,则可以将其设置为True。
- autopaginate:您可以将其设置为可调用,以获取请求产生的所有页面。目前,唯一可调用的是genericclient.pagination.link_header,它支持[rfc5988](https://tools.ietf.org/html/rfc5988)。
终点
端点在主实例上可用作属性。
.all()
检索所有资源(实际上是端点上的一个简单的GET):
myclient.posts.all() # GET /posts/
.filter()
.filter(**kwargs)用kwargs作为查询字符串值调用GET:
myclient.posts.filter(blog=12, status=1) # GET /posts/?blog=12&status=1
.get(**kwargs)
.filter()的特例。
如果kwargs包含id、pk、slug或username,则该值将 按顺序在url路径中使用。
否则,它调用GET,并将kwargs作为查询字符串值。
如果返回的列表为空,则将引发ResourceNotFound。
如果返回的列表包含多个资源,则将引发MultipleResourcesFound
注意.get()将返回Resource,而不是Resources的列表
myclient.posts.filter(blog=12, status=1) # GET /posts/?blog=12&status=1 myclient.posts.filter(id=12) # GET /posts/12/ myclient.posts.filter(slug='12-ways-clickbait') # GET /posts/12-ways-clickbait/
.create(payload)
将生成一个POST,其中payload(adict)是请求的主体, 返回新的Resource:
post = myclient.posts.create({'blog': 12, 'status': 1}) # POST /posts/
.get_or_create(defaults, **kwargs)
发出获取资源的get。如果找不到资源,则发布帖子 创建资源:
# Assuming it doesn't exist post = myclient.posts.get_or_update(slug='my-post', defaults={'status': 1}) # GET /posts/my-post/, then POST /posts/
.create_or_update(payload)
如果payload包含名为'id'的密钥,则将发出PUT。如果 服务器返回400错误,将重新发出PATCH请求。 如果有效载荷`不包含'id',它将发出POST:
post = myclient.posts.create_or_update({'status': 1}) # POST /posts/ post = myclient.posts.create_or_update({'id': 1234, 'status': 1}) # PUT /posts/1234/ post = myclient.posts.create_or_update({'id': 1234}) # PUT /posts/1234/ # <- server returns 400 # -> PATCH /posts/1234/
.delete(pk)
将发出DELETE,并将使用pk作为url的一部分:
myclient.posts.delete(24) # DELETE /posts/24/
资源
所有终结点方法(除了.delete())都返回 Resource或Resources的列表。
Resource只是dict的包装类,在这里可以访问键 作为财产。
另外,Resources有一个名为.payload的特殊属性,它 包含从服务器接收的原始负载。
Resources有以下方法:
Resource.delete()将产生一个DELETE,其中Resource.id为 url的par:
blog = myclient.posts.create({'blog': 12, 'status': 1}) # POST /posts/ blog.delete() # DELETE /blog/345/ -- the ID 345 was returned by the server in the previous response
Resource.save()将产生一个PUT,其中Resource.id为 url的par。如果 服务器返回400错误,将重新发出PATCH请求:
post = myclient.posts.create({'blog': 12, 'status': 1}) # POST /posts/ post.status = 2 post.save() # PUT /posts/345/ post = Resource(id=345, status=1) post.save() # PUT /posts/345/ # <- server returns 400 # -> PATCH /posts/345/
资源集
每当一个方法返回一个资源列表时,它们的列表将被包装在一个ResultSet中。
resultset只是一个list对象,添加的.response包含来自服务器的原始响应。
自定义端点和资源
可以通过子类化genericclient.Resource自定义资源。
最常见的原因是指定主键的名称:
from genericclient import Resource class PostResource(Resource): pk_name = 'slug'
端点可以通过子类化genericclient.Endpoint:
form genericclient import Endpoint class PostEndpoint(Endpoint): resource_class = PostResource
然后,您可以子类genericclient.GenericClient来告诉客户机在每个端点上使用哪个端点类:
from genericclient import GenericClient class Client(GenericClient): endpoint_classes = { 'posts': PostEndpoint, }
路线
如果你的api有主终结点内的某些非restful调用(有时称为detail_route和list_route),可以使用genericclient调用它们:
myclient.posts(id=123).publish(date=tomorrow)
myclient.blogs().ping()
路由http调用在默认情况下使用POST,但您可以使用_method参数指定其他内容:
myclient.posts(_method='get', id=123).pingbacks()
myclient.blogs(_method='get').visits()
注意,此调用将返回genericclient.ParsedResponse的实例,而不是genericclient.Resource,
许可证
根据麻省理工学院的许可证。