从apistar继承的starlette api层
starlette-api的Python项目详细描述
api启动starlette
文档:https://starlette-api.perdy.io
Starlette API
Starlette API旨在在Starlette之上添加一层,为构建高性能的 restapi
生产准备就绪,并提供以下功能:
- 泛型类用于在sqlalchemy表上提供标准crud方法的api资源。
- Schema系统基于Marshmallow,允许declare端点的输入和输出,并提供 根据这些模式验证数据的可靠方法validate
- 依赖注入简化了管理端点所需参数的过程。星体asgi对象
像
Request
、Response
、Session
等等都被定义为组件并准备好注入到端点中。 - Components作为插件生态系统的基础,允许您创建自定义或使用 你的端点,作为参数注入。
- 使用openapi标准自动生成api架构。它使用端点的模式系统来提取 生成API架构所需的信息。
- 自动生成的文档提供Swagger UI或ReDoc端点。
- 分页使用多种方法(如限制和偏移、页码)自动处理…
要求
- Python3.6+
- Starlette0.10+
- Marshmallow3.0+
安装
$ pip install starlette-api
示例
frommarshmallowimportSchema,fields,validatefromstarlette_api.applicationsimportStarlette# Data SchemaclassPuppy(Schema):id=fields.Integer()name=fields.String()age=fields.Integer(validate=validate.Range(min=0))# Databasepuppies=[{"id":1,"name":"Canna","age":6},{"id":2,"name":"Sandy","age":12},]# Applicationapp=Starlette(components=[],# Without custom componentstitle="Foo",# API titleversion="0.1",# API versiondescription="Bar",# API descriptionschema="/schema/",# Path to expose OpenAPI schemadocs="/docs/",# Path to expose Swagger UI docsredoc="/redoc/",# Path to expose ReDoc docs)# Views@app.route("/",methods=["GET"])deflist_puppies(name:str=None)->Puppy(many=True):""" description: List the puppies collection. There is an optional query parameter that specifies a name for filtering the collection based on it. responses: 200: description: List puppies. """return[puppyforpuppyinpuppiesifpuppy["name"]==name]@app.route("/",methods=["POST"])defcreate_puppy(puppy:Puppy)->Puppy:""" description: Create a new puppy using data validated from request body and add it to the collection. responses: 200: description: Puppy created successfully. """puppies.append(puppy)returnpuppy
依赖关系
遵循Starlette哲学Starlette API将硬依赖项的数量减少到用作核心的数量:
它没有更多的硬依赖项,但其中一些是使用某些功能所必需的:
您可以使用pip3 install starlette-api[full]
安装所有这些
学分
这个库最初是对APIStar的一个改编,用于处理Starlette,但是大量的代码 重写以使用Marshmallow作为架构系统。
贡献
这个项目是绝对开放的贡献,所以如果你有一个好主意,创建一个问题,让社区 讨论一下。