没有项目描述

sanic-transmute的Python项目详细描述


Build
Travis-CI Build Statuscoverage status
DocsDocumentation Status
Package
PyPI Package latest releasePyPI WheelSupported versionsSupported implementations

使用swagger ui、参数验证和模型序列化轻松地记录sanic api。

您可以在此处了解更多信息:

http://sanic-transmute.readthedocs.io/en/latest/

什么是神智蜕变?

一个transmutesanic的框架。这个 框架提供:

  • 通过解析函数注释声明性地生成http处理程序接口
  • 对各种内容类型(如json或yaml)进行验证和序列化。
  • 使用attrsschematics在本地python对象之间进行验证和序列化。
  • 通过swagger以这种方式生成的所有处理程序的自动文档。

快速启动

概述:

code-to-ui.png

在此处查找示例:

带示意图模型的简单示例。

fromsanicimportSanic,Blueprintfromsanic.responseimportjsonfromsanic_transmuteimportdescribe,add_route,add_swagger,APIExceptionfromsanic.exceptionsimportServerErrorfromschematics.modelsimportModelfromschematics.typesimportIntTypeclassUser(Model):points=IntType()app=Sanic()bp=Blueprint("test_blueprints",url_prefix="/blueprint")@describe(paths="/api/v1/user/{user}/",methods="GET")asyncdeftest_transmute(request,user:str,env:str=None,group:[str]=None):"""
    API Description: Transmute Get. This will show in the swagger page (localhost:8000/api/v1/).
    """return{"user":user,"env":env,"group":group,}@describe(paths="/killme")asyncdefhandle_exception(request)->User:"""
    API Description: Handle exception. This will show in the swagger page (localhost:8000/api/v1/).
    """raiseServerError("Something bad happened",status_code=500)@describe(paths="/api/v1/user/missing")asyncdefhandle_api_exception(request)->User:"""
    API Description: Handle APIException. This will show in the swagger page (localhost:8000/api/v1/).
    """raiseAPIException("Something bad happened",code=404)@describe(paths="/multiply")asyncdefget_blueprint_params(request,left:int,right:int)->str:"""
    API Description: Multiply, left * right. This will show in the swagger page (localhost:8000/api/v1/).
    """res=left*rightreturn"{left}*{right}={res}".format(left=left,right=right,res=res)if__name__=="__main__":add_route(app,test_transmute)add_route(app,handle_exception)add_route(app,handle_api_exception)# register blueprintsadd_route(bp,get_blueprint_params)app.blueprint(bp)# add swaggeradd_swagger(app,"/api/v1/swagger.json","/api/v1/")app.run(host="0.0.0.0",port=8000)

带attrs模型的简单示例。

fromsanicimportSanic,Blueprintfromsanic.responseimportjsonfromsanic_transmuteimportdescribe,add_route,add_swagger,APIExceptionfromsanic.exceptionsimportServerErrorimportattr@attr.sclassUser:points=attr.ib(type=int)app=Sanic()bp=Blueprint("test_blueprints",url_prefix="/blueprint")@describe(paths="/api/v1/user/{user}/",methods="GET")asyncdeftest_transmute_get(request,user:str,env:str=None,group:[str]=None):"""
    API Description: Transmute Get. This will show in the swagger page (localhost:8000/api/v1/).
    """return{"user":user,"env":env,"group":group,}@describe(paths="/api/v1/user/",methods="POST")asyncdeftest_transmute_post(request,user:User)->User:"""
    API Description: Transmute Post. This will show in the swagger page (localhost:8000/api/v1/).
    """returnuser@describe(paths="/killme")asyncdefhandle_exception(request)->User:"""
    API Description: Handle exception. This will show in the swagger page (localhost:8000/api/v1/).
    """raiseServerError("Something bad happened",status_code=500)@describe(paths="/api/v1/user/missing")asyncdefhandle_api_exception(request)->User:"""
    API Description: Handle APIException. This will show in the swagger page (localhost:8000/api/v1/).
    """raiseAPIException("Something bad happened",code=404)@describe(paths="/multiply")asyncdefget_blueprint_params(request,left:int,right:int)->str:"""
    API Description: Multiply, left * right. This will show in the swagger page (localhost:8000/api/v1/).
    """res=left*rightreturn"{left}*{right}={res}".format(left=left,right=right,res=res)if__name__=="__main__":add_route(app,test_transmute_get)add_route(app,test_transmute_post)add_route(app,handle_exception)add_route(app,handle_api_exception)# register blueprintsadd_route(bp,get_blueprint_params)app.blueprint(bp)# add swaggeradd_swagger(app,"/api/v1/swagger.json","/api/v1/")app.run(host="0.0.0.0",port=8000)

开发

建造。

./uranium

运行单元测试。

./uranium test

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

推荐PyPI第三方库


热门话题
java线程;创建一个单独的线程来定期执行某些操作   用java将SonarQube升级到5.2失败。util。同时发生的CancellationException:空   在线程对象上调用start时,线程上设置的java优先级无效   java如何在不使用reduce()或last()折叠的情况下获取通量的最后一项   java如何从ArrayList中获取元素?   应用了逻辑的JavaTwitterAPI   在这种情况下,怎样才能不让自己重复?安卓的java   JavaAndroid:线程可运行和处理程序。在run方法中发布问题(Handler.postXX在预期情况下不会按时运行)   java log4j grok与管道故障   使用循环返回命令选项时出现问题/仅运行一个命令(JAVA)   java双重比较问题   使用vertx的JWT公钥/私钥身份验证中的java握手   用Java将WMA和AAC音频转换为MP3   解析字符串java get index   与具有不同数据类型的密钥同名的java JSON