优雅的python rest工具包构建在falcon之上
graceful的Python项目详细描述
优雅
graceful
是一个优雅的python rest工具包,构建于
falcon框架。它的灵感很高
通过Django REST framework-主要通过
如何进行对象序列化,但这里更强调的是
自我描述。
功能:
- 列表和单个对象资源的泛型类
- 简单但可扩展的分页
- 简单但可扩展的身份验证和授权
- 内容/元分离的结构化响应
- 声明性字段和参数
- 一切都是自描述性的:在python和
通过
OPTIONS
请求 - 无痛验证
- 100%测试覆盖率
- Falcon=0.3.0(测试高达1.4.x)
- python3独家(测试范围从3.3到3.6)
优雅背后的社区开始发展,但我们没有任何邮件 名单还没有。在Librelist上有一个 但没人用过,librelist似乎死了(参见github 发行#36)。现在我们用 在我们决定要做什么新的事情之前先聊一聊。 有空聊天here。
仅Python3
重要:graceful
是python3独占的,因为现在应该是
忘记Python的好时机。没有制定graceful
的计划
python2兼容,尽管使用python2很简单
现有工具(如六)。
用法
有关扩展教程和更多信息,请参阅 guide包含在 文档。
无论如何,这里有一个简单的使用graceful
:
importfalconfromgraceful.serializersimportBaseSerializerfromgraceful.fieldsimportIntField,RawFieldfromgraceful.parametersimportStringParamfromgraceful.resources.genericimport(RetrieveAPI,PaginatedListAPI,)api=application=falcon.API()# lets pretend that this is our backend storageCATS_STORAGE=[{"id":0,"name":"kitty","breed":"saimese"},{"id":1,"name":"lucie","breed":"maine coon"},{"id":2,"name":"molly","breed":"sphynx"},]# this is how we represent cats in our APIclassCatSerializer(BaseSerializer):id=IntField("cat identification number",read_only=True)name=RawField("cat name")breed=RawField("official breed name")classCat(RetrieveAPI):""" Single cat identified by its id """serializer=CatSerializer()defget_cat(self,cat_id):try:return[catforcatinCATS_STORAGEifcat['id']==int(cat_id)][0]exceptIndexError:raisefalcon.HTTPNotFounddefretrieve(self,params,meta,**kwargs):cat_id=kwargs['cat_id']returnself.get_cat(cat_id)classCatList(PaginatedListAPI):""" List of all cats in our API """serializer=CatSerializer()breed=StringParam("set this param to filter cats by breed")deflist(self,params,meta,**kwargs):if'breed'inparams:filtered=[catforcatinCATS_STORAGEifcat['breed']==params['breed']]returnfilteredelse:returnCATS_STORAGEapi.add_route("/v1/cats/{cat_id}",Cat())api.add_route("/v1/cats/",CatList())
假设这段代码在名为example.py
的python模块中。
现在用gunicorn:
gunicorn -b localhost:8888 example
你已经准备好查询了(这里有很棒的httpie 工具):
$ http localhost:8888/v0/cats/?breed=saimese
HTTP/1.1 200 OK
Connection: close
Date: Tue, 16 Jun 2015 08:43:05 GMT
Server: gunicorn/19.3.0
content-length: 116
content-type: application/json
{
"content": [
{
"breed": "saimese",
"id": 0,
"name": "kitty"
}
],
"meta": {
"params": {
"breed": "saimese",
"indent": 0
}
}
}
或访问发出OPTIONS
请求的api描述:
$ http OPTIONS localhost:8888/v0/cats
HTTP/1.1 200 OK
Connection: close
Date: Tue, 16 Jun 2015 08:40:00 GMT
Server: gunicorn/19.3.0
allow: GET, OPTIONS
content-length: 740
content-type: application/json
{
"details": "List of all cats in our API",
"fields": {
"breed": {
"details": "official breed name",
"label": null,
"spec": null,
"type": "string"
},
"id": {
"details": "cat identification number",
"label": null,
"spec": null,
"type": "int"
},
"name": {
"details": "cat name",
"label": null,
"spec": null,
"type": "string"
}
},
"methods": [
"GET",
"OPTIONS"
],
"name": "CatList",
"params": {
"breed": {
"default": null,
"details": "set this param to filter cats by breed",
"label": null,
"required": false,
"spec": null,
"type": "string"
},
"indent": {
"default": "0",
"details": "JSON output indentation. Set to 0 if output should not be formated.",
"label": null,
"required": false,
"spec": null,
"type": "integer"
}
},
"path": "/v0/cats",
"type": "list"
}
贡献
任何贡献都是受欢迎的。问题,建议,请求等等。 只有一小部分规则可以指导您进行项目开发 提交请求前应注意:
- 只有通过ci构建(travis)的请求才会被合并。
- 在构建过程中用
flakes8
和pydocstyle
检查代码,因此 这意味着必须遵守PEP-8和PEP-257。 - 不会合并降低覆盖率的更改。
一件事:如果你提交了一个公关,请不要再回扣,除非你 是明确要求的。回顾突然出现的拉取请求 他们改写的历史让我抓狂。
许可证
请参阅LICENSE
文件。