connexion-api-first应用程序与openapi/swagger和flask

connexion-aiohttp的Python项目详细描述


连接,连接,连接图像:https://badges.gitter.im/zalando/connection.svg
:alt:join the cat at https://gitter.im/zalando/connection
:target:https://gitter.im/zalando/connection?UTM=UTM&UTM Medium=BADGE&UTM=UTM&UTM运动=PR-BADGE&UTM=UTM COntent=BADGE
图像:https://travis-ci.org/zalando/connection.svg?Branch=master
:target:https://travis-ci.org/zalando/connection
:alt:travis ci build status
.图像:https://coveralls.io/report/zalando/connection/badge.svg?目标:https://coveralls.io/r/zalando/connection?分部=硕士
图像:https://img.shields.io/pypi/v/connection.svg
:target:https://pypi.python.org/pypi/connection
:alt:latest version
。图像:https://img.shields.io/pypi/status/connection.svg
:target:https://pypi.python.org/pypi/connection
:alt:development status
。图像:https://img.shields.io/pypi/pyversions/connection.svg
:target:https://pypi.python.org/pypi/connection
:alt:python versions
。图像:https://img.shields.io/pypi/l/connection.svg
:target:https://github.com/zalando/connection/blob/master/license
:alt:license
br/>connection is a framework on top of flask>swagger spec>of your api described in ` yaml format'。连接
允许您写一个swagger规范,然后映射到您的蟒蛇功能的末端点;这使得它独一无二,就像许多工具生成了基于您的蟒蛇的规范。你可以像你想要的那样详细描述你的余下部分。然后,连接保证它将按照
you specified的方式进行。
br/>we built this way connection to order:
-simplify the development process
-confirm expections about your API will look like

br/---------------------------------------------------------------Requests and endpoint parameters automatically,以
为基础的规格提供了一个互联网交换控制器,使用户可以
生存文件,甚至可以呼叫你的API终点站
通过它
handles oauth 2 token-based authentification
-supports api versioning
Payloads Serialization.如果您的规格确定了一个终点回归JSON,连接将自动Serialize the return value for you and set the right
content type in the header.
br/>为什么连接
-----------------------------------------------------------------
br/>自动Seriali接着叫你的蟒蛇代码,处理从规范到代码的映射。在你写一条单一的代码线之前,即使在你写一条单一的代码线之前,如果多个团队依靠你的帐户,你也可以使用连接,容易地向他们提供你的API文件。这个保证你的API会遵循你写的规格。这是一个不同的过程,由框架提供,例如Hug \,在你写下代码之后产生一个规范。基于代码生成规格的一些缺陷是,它们常常在缺少细节或将你的文献与应用程序的逻辑代码混淆。 on Swagger_'s website
- Blog post: `Crafting effective Microservices in Python`_

How to Use
==========

Prerequisites
-------------

Python 2.7 or Python 3.4+

Installing It
-------------

In your command line, type:

.. 代码-block:Bash
$pip install connection
br/>running it
-——

将api yaml放在应用程序根路径(例如"swagger/")中的文件夹中。然后运行:

…代码块::python


app=connexion.app(u name_u,规格'u dir='swagger/')
app.add_api('my_api.yaml')
app.run(port=8080)


请参阅'connexion pet store示例应用程序',以获取样本
规格。

现在您可以运行和使用connexion了!



oauth 2身份验证和授权
--------


connexion支持三种oauth 2处理方法之一。(请参见下面的
"todo")通过connexion,api安全定义**必须**
包括"x-tokeninfo url"或"x-tokeninfo func"(或分别设置"tokeninfo"url``
或"tokeninfo"func``env var)。"x-token info url'必须包含
url才能验证和获取"token information";'x-tokeninfofunc必须
包含对用于获取令牌信息的函数的引用。当同时使用"x-tokeninfourl"
和"x-tokeninfofunc"时,connexion将优先处理函数方法。connexion希望
以"rfc 6750<;rfc6750>;"第2.1节中描述的格式在"authorization"头字段中接收oauth令牌。此方面与通常的OAuth流有显著的不同。


动态呈现您的规范
您可以为应用程序全局(通过"connexion.app"构造函数)或每个特定的API(通过"connexion.app"add"api"方法)定义规范参数:

。代码块::python

app=connexion.app({name''uu,specification''u dir='swagger/',
arguments={'global':'global'})
app.add''u api('my''u api.yaml',arguments={'api'@local':'local'})
app.run(port=8080)

在全球范围和API上,API值将优先。

endpoint routing to your python views
路由**:

…代码块::yaml

路径:
/hello-world:
post:
operationid:myapp.api.hello-world

`` myapp.api``模块。或者,您可以在操作定义中包含
``x-swagger-router-controller`,使
``operationid``相对:

……代码块::yaml

路径:
/hello\u world:
post:
x-swagger-router-controller:myapp.api
操作ID:hello\u world

connexion可以使用替代的
``resolvers`--例如,``restyresolver``。"`restyresolver`
将根据您规范中的端点


的路径和http方法组成一个``operationid`'。代码块::python

代码块::yaml

路径:
/:
获取:
获取:
隐含操作id:api.foo.get
放置:
隐含操作id:api.foo.put
复制:
隐含operationid:api.foo.copy
delete:
它还将尊重
``x-router-controller``。您可以导入并扩展"connexion.resolver.resolver",以实现自己的"operationid"`
(和函数)解析算法。

将python视图的参数作为命名参数,并尽可能使用值转换。只需使用与视图参数相同的名称定义端点的参数。

例如,假设您有一个指定为:

代码块::yaml

path:
/foo:
get:
operationid:api.foo\u get
参数:
-名称:消息
描述:某些消息。
in:query
type:string
必需:true

和view函数:

…代码块::python

connexion自动识别view
函数需要名为"message"的参数,并将端点参数"message"的值
分配给view函数。

。警告:当您在端点将参数定义为*不*必需时,并且您的python视图具有
非命名参数,无论何时调用此终结点而不使用参数,都会出现"缺少位置参数"异常。


类型转换
^^^^^^^^^^^

只要可能,connexion将尝试分析参数值并将类型转换为相关的python本机值。当前可用的类型铸件有:

+——+——+——+——+——
夸张类型python类型
+——+————————+————+——————————————————————————————————————————————————————————————————————————————————————————————————————————|
+——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————|
+————————————————————————————————————————————————————————————————————————————————————————————connexion当前支持收集格式"pipes"和"csv"。默认格式为"csv"。


启用此选项后,包含未在swagger规范中定义的参数的请求将返回400错误。您可以在将api添加到应用程序时启用它:

。代码块::python

app.add_-api('my_-apy.yaml',strict_-validation=true)


api版本控制和基址路径
这对于版本化的api很有用。若要服务于"http://myhost/1.0/hello-world"中的前一个端点,请键入:

。代码块::yaml

base path:/1.0


路径:
/hello_world:
post:
operationid:myapp.api.hello_world

您可以在将api添加到应用程序时提供它:

…代码块::python

app.add_api('my_api.yaml',base_path='/1.0')


swagger json
----
connexion以json格式生成openapi/swagger规范
可从api基本路径中的"swagger.json"获得。

代码块::python

app=connexion.app(uu name_uuu,specification_u dir="swagger/",
swagger_u json=false)
app.add_u api("my_u api.yaml")

代码块::python

app=connexion.app(u name_uu,specification_u dir='swagger/')
app.add_u api('my_u api.yaml',swagger_json=false)

----

服务的swagger ui中的所有uri
都是https端点。问题是:默认运行的
服务器是一个"普通"http服务器。这意味着
swagger ui不能用于播放api。使用connexion时,启动https服务器的正确方法是什么?

"烧瓶描述"的一种方法是这样的:

…代码块::python

from openssl import ssl
context=ssl.context(ssl.sslv23-u方法)
context.use_privatekey-u文件('yourserver.key')
context.use_certificate-u文件('yourserver.crt')

app.run(host='127.0.0.1',port='12344',
debug=false/true,ssl_context=context)


但是,connexion不提供ssl_context参数。这是
,因为flask也不需要——但它使用`**kwargs`将
参数发送到底层的[werkzeug](http://werkzeug.pocoo.org/)服务器。


`{base_path}/ui/``中,``base_path``是api的基路径。

代码块::python

app=connexion.app(uu name_uu,specification_u dir="swagger/",
swagger_u ui=false)
app.add_u api('my_u api.yaml')


代码块::python

app=connexion.app(uu name_uuu,specification_u dir='swagger/')
app.add_u api('my_u api.yaml',swagger_u ui=false)

可以使用
swagger ui显式指定目录的路径,以不使用connexion embedded swagger ui发行版。
为此,应指定以下选项:

。代码块::pythonoptions=options)

代码块:

const ui=swaggeruibundle({url:"{api_url}}/swagger.json"})

server backend
----


connexion使用默认的烧瓶服务器。对于异步
应用程序,也可以使用tornado_u作为http服务器。要执行此操作,请将服务器设置为"tornado":

…代码块::python


app=connexion.app(uuu name_uuu,specification_u dir='swagger/')
app.run(server='tornado',port=8080)


代码块:python代码块::python


app=connexion.aiohttpapp(uu name_uu,specification_u dir='swagger/')
app.run(port=8080)

**注意:*还要检查aiohttp处理程序示例。

设置并运行安装代码:

代码块::bash

$sudo pip3安装uwsgi
$uwsgi--http:8080-w app-p 16#使用16个工作进程

有关详细信息,请参阅"uwsgi文档"。

…_将flask与uwsgi一起使用:http://flask.pocoo.org/docs/latest/deploying/uwsgi/
…_ uwsgi文档:https://uwsgi docs.readthedocs.org/
…_示例:https://docs.aiohttp.org/en/stable/web.html处理程序




documentation
===
其他信息可在"connexion的文档页"获得。


更改
==


在"github发布页"上维护完整的更改日志。


。_ Github发布页面:https://github.com/zalando/connexion/releases


对connexion/todos的贡献
=======


我们欢迎您的想法、问题和请求。请遵循常规/标准的github实践。


除非您事先明确声明,否则您有意将任何重要的贡献提交给此存储库的管理员(zalando se,(柏林)应遵守以下apache许可2.0的
条款和条件,无需任何
附加版权信息、条款或条件。

todos
----




我们希望您能帮助我们处理
我们有一个"我们正在寻找贡献的问题"列表。


以及炫耀/openapi以获得他们的支持。


license
==


2015 zalando se


您可以在http://www.apache.org/licenses/license-2.0.


上获取许可证副本,除非适用法律要求或书面同意,否则根据许可证分发的软件是按"原样"分发的,无任何明示或暗示的保证或条件。请参阅许可证,了解该许可证下管理权限和限制的特定语言。

。_烧瓶:http://flask.pocoo.org/
…_发布华夫饼板:https://waffle.io/zalando/connexion
…_ API优先:https://tech.zalando.com/blog/on API和Zalando API Guild/
…_拥抱:https://github.com/timothycrosley/hug
…_招摇过市:http://swagger.io/open source integrations/
…_ Jinja2:http://jinja.pocoo.org/
。_ rfc6750:https://tools.ietf.org/html/rfc6750
。_ OpenAPI 2.0规范:https://github.com/oai/OpenAPI规范/blob/master/versions/2.0.md
…_操作对象:https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md操作对象
…_ swager.spec.security_定义:https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md安全定义对象
…_ swager.spec.security悻u要求:https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md悻安全要求对象
…_ yaml格式:https://github.com/oai/openapi specification/blob/master/versions/2.0.md format
…_令牌信息:https://tools.ietf.org/html/rfc6749
…_龙卷风:http://www.tornado web.org/en/stable/
。_连接宠物商店示例应用程序:https://github.com/hjacobs/connexion example
…_由flask描述:http://flask.pocoo.org/snippets/111/
。_ Connexion的文档页面:http://connexion.readthedocs.org/en/latest/
。_在python中创建有效的微服务:http://carcio.com/2016/09/16/crafting effective microservices in python/
。_我们正在寻找贡献的问题:https://github.com/zalando/connexion/issues?Q=IS%3问题+IS%3打开+标签%3A%22帮助+需要的%22

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

推荐PyPI第三方库


热门话题
java编辑并重新运行spring引导单元测试,无需重新加载上下文即可加快测试速度   为什么我不能做演员?   java为什么是线程。join通常用于停止安卓中的线程   java从weblogic服务器调用JSON POST REST服务时收到400:错误请求   java在DeviceAdmin模式禁用时设置身份验证?   java SortedMap的keySet()能否始终安全地强制转换到SortedSet?   安卓 java。lang.NoSuchMethodException可包裹类   java JOGL库安装   javatomcat内存管理   java使用getString()中的变量   java将最小星号设置为评级栏   Java中字符串相等的println()方法。。。它到底是如何工作的?   java如何从文本中输出的数组中放入随机图像