没有项目描述

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如何使用JNA创建同一库的多个实例?   java在将Graphql查询作为JSON字符串传递时收到意外的令牌错误   OAuth2 oltu的java问题   java桌面应用程序使用的好的嵌入式数据库是什么?   java Firebase数据库高级查询选项   java正在使磁盘上的EhCache元素过期   java 安卓还原处于backstack中的片段的实例状态   XMemcached中的java异步集   java TimescaleDB是否使用与Postgresql完全相同的JDBC驱动程序?   java从网站c读取信息#   检查java Android中的字符串是否只包含数字和空格   c#如何向web服务发送特殊字符?   grails无法调用需要java的方法。lang.类参数?   java我在组合框中调用的方法不会运行所有代码,它只运行部分代码   java发送带有标头的HTTP GET请求