茄子:使用api-first方法创建restapi。
aubergine的Python项目详细描述
aubergine是一个库,可以帮助您轻松地使用 API优先方法。
在aubergine中创建api非常简单,只需要以下步骤:
- 创建openapi 3规范文件,将operationId设置为您选择的可调用文件。
openapi:"3.0.0"servers:-url:/v1/restinfo:version:1.0.0title:Hello Worlddescription:My first api.paths:/hello:get:operationId:hello_world.helloparameters:-who:whoin:querydescription:who to greatrequired:trueschema:type:stringresponses:'200':description:Greeting dedicated to given name.content:application/json:schema:$ref:'#/components/schemas/Greeting'components:schemas:Greeting:required:-messageproperties:message:type:string
- 实现您在规范中声明的操作。将包含以下内容的hello_world模块放在可导入目录中。
defhello(who):"""Handles v1/rest/hello"""return{'message':'Hello {}!'.format(who)}
- 告诉茄子为你创建一个应用程序。创建包含以下内容的hello_app.py:
fromaubergineimportAubergineapp=Aubergine.from_file('hello_world.yml')api=app.build_api()
- 你的应用程序已准备就绪。您可以在任何wsgi服务器上运行它,就像这样:
gunicorn hello_app:api
导航到http://localhost:8000/v1/rest/hello?who=World