未提供项目描述
项目描述
基于 requests 的RESTful API通用客户端。
关于基于 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)。
端点
端点作为主实例的属性和项目可用
myclient.posts.all() # GET /posts/
myclient["active-users"].all() # GET /active-users/
.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路径中使用该值。
否则,将以kwargs作为查询字符串值调用GET。
如果返回的列表为空,将引发ResourceNotFound。
如果返回的列表包含多于1个资源,将引发MultipleResourcesFound。
请注意,.get()将返回一个Resource,而不是Resource列表。
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(一个dict)作为请求体,返回一个新的Resource。
post = myclient.posts.create({'blog': 12, 'status': 1}) # POST /posts/
.get_or_create(defaults, **kwargs)
发出GET请求以获取资源。如果找不到资源,则发出POST请求以创建资源。
# 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请求。如果payload不包含'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。
Resource只是一个用于dict的包装类,其中键可以作为属性访问。
此外,Resource有一个特殊属性名为.payload,其中包含从服务器接收到的原始有效负载。
Resource有以下方法
Resource.delete()将导致一个DELETE请求,其中Resource.id作为URL的一部分。
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的一部分。如果服务器返回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只是一个列表对象,它还包含一个名为.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在主要端点(有时称为detail_route和list_route)中有一些非RESTful调用,您可以使用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实例。
许可证
版权所有 MIT许可证。
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。