优雅的python rest工具包构建在falcon之上

graceful的Python项目详细描述


PyPIPyPIBuild StatusCoverage StatusDocumentation StatusJoin the chat at https://gitter.im/graceful-for-falcon/Lobby

优雅

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

制作的api的例子
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)的请求才会被合并。
  • 在构建过程中用flakes8pydocstyle检查代码,因此 这意味着必须遵守PEP-8和PEP-257。
  • 不会合并降低覆盖率的更改。

一件事:如果你提交了一个公关,请不要再回扣,除非你 是明确要求的。回顾突然出现的拉取请求 他们改写的历史让我抓狂。

许可证

请参阅LICENSE文件。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
内存Java正在运行。jar heapdump错误   java如何在安卓画布中弯曲文本区域?   java如何在Gdx 安卓游戏编程中获得矩形的真实触碰位置?   找不到java Spring MVC控制器   在Java中使用双重检查锁定单例扩展类   java在高效的时间和内存中动态执行insert(索引、数据)、delete(索引)、getAt(索引)操作。   java 安卓 Toast和视图帮助   java协议缓冲区:从文件中读取所有序列化消息   java如何在Jackson中为参数化接口类型执行通用自定义反序列化程序   与简单的空检查相比,使用(平面)映射的java优势是什么?   异步方法seam中的java Get contextparam   jar使用相同的java运行时运行另一个java程序   java访问Spring批处理中的作业参数   java给定字符串为空或null   在h2数据库1.4中找不到java类“org.h2.fulltext.FullTextLucene”。*不适用于Lucene Core 4*   java Spring Boot在使用@enableSync时不响应任何请求   java错误:在bash上找不到或加载主类pj2   “返回对象”和“返回(对象)”之间的Java差异   java Android开发:如何使用onKeyUp?