Skip to main content

Connexion - API first applications with Swagger and Flask

Project description

Connexion

Travis CI build status Coveralls status Latest Version Development Status Python Versions License

Connexion is a framework on top of Flask to automagically handle your REST API requests based on Swagger 2.0 Specification files in YAML.

How to use

Put your API YAML inside a folder in the root path of your application (e.g swagger\) and then do

import connexion

app = connexion.App(__name__, port = 8080, specification_dir='swagger/')
app.add_api('my_api.yaml')
app.run()

See the Connexion Pet Store Example Application for details.

Parametrization

Connexion uses Jinja2 to allow the parametrization of specifications.

The specification arguments can be defined globally for the application or for each specific api:

app = connexion.App(__name__, port = 8080, specification_dir='swagger/', arguments={'global': 'global_value'})
app.add_api('my_api.yaml', arguments={'api_local', 'local_value'})
app.run()

If a value is provided both globally and on the api then the api value will take precedence.

Endpoint Routing

Connexion uses the OperationId from each Operation Object to identify which function should handle each url.

For example:

paths:
  /hello_world:
    post:
      operationId: myapp.api.hello_world

If you provided this path in your specification POST requests to http://MYHOST/hello_world would be handled by the function hello_world in myapp.api.

Additionally you can also define a basePath on the top level of the API specification, which is useful for versioned APIs. If you wanted to serve the previous endpoint from http://MYHOST/1.0/hello_world you could do:

basePath: /1.0

paths:
  /hello_world:
    post:
      operationId: myapp.api.hello_world

Other alternative if you don’t want to include the base path in your specification is provide the base path when adding the API to your application:

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

Response Serialization

If the specification defines that a endpoint returns json connexion will automatically serialize the return value for you and set the right content type in the HTTP header.

Authentication and Authorization

If the specification includes a Oauth2 Security Definition compatible with the Zalando Greendale Team’s infrastructure connexion will automatically handle token validation and authorization for operations that have Security Requirements. One main difference between the usual Oauth flow and the one connexion uses is that the API Security Definition must include a ‘x-tokenInfoUrl’ with the url to use to validate and get the token information. Connexion expects to receive the Oauth token in the Authorization header field in the format described in RFC 6750 section 2.1.

Swagger Json

Connexion makes the Swagger specification in json format available from swagger.json in the base path of the api.

Swagger UI

The Swagger UI for an API is available, by default, in {base_path}/ui/ where base_path is the base path of the api.

You can disable the swagger ui either at application level:

app = connexion.App(__name__, port = 8080, specification_dir='swagger/', swagger_ui=False)
app.add_api('my_api.yaml')

You can also disable it at api level:

app = connexion.App(__name__, port = 8080, specification_dir='swagger/')
app.add_api('my_api.yaml', swagger_ui=False)

Server Backend

By default connexion uses the default flask server but you can also use Tornado as the http server, to do so set server to tornado:

import connexion

app = connexion.App(__name__, port = 8080, specification_dir='swagger/', server='tornado')

Releasing Connexion

Build and upload new version to PyPI:

$ ./release.sh <NEW-VERSION>

License

Copyright 2015 Zalando SE

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

connexion-0.9.0.tar.gz (1.0 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

connexion-0.9.0-py3-none-any.whl (1.0 MB view details)

Uploaded Python 3

File details

Details for the file connexion-0.9.0.tar.gz.

File metadata

  • Download URL: connexion-0.9.0.tar.gz
  • Upload date:
  • Size: 1.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for connexion-0.9.0.tar.gz
Algorithm Hash digest
SHA256 5a8ce70c648941072a23b61075f4cf589e18abda73e2eaeb0cc660134d99cacb
MD5 348a8ba8a63a4f88081e21ae4e1e3943
BLAKE2b-256 fa2f1b46f1f27c5643bf7051845d74331d16b522ae4c6eb9f77ccb65490cc9df

See more details on using hashes here.

File details

Details for the file connexion-0.9.0-py3-none-any.whl.

File metadata

File hashes

Hashes for connexion-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f7a2aa9965068bf4900b6cf533e3a1b8d059b6e5ffcbe47de50a692903e372d1
MD5 ad4382f8029fa4bae1300a8f876cfce9
BLAKE2b-256 3a7b6a3d28ddd37665010c5dc4ce7bac49304fb83e9d1000ca8e9e423d575eb7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page