Skip to main content

Framework for Quart to add swagger generation to routes and restful resources

Project description

https://travis-ci.com/factset/quart-openapi.svg?branch=master

Documentation can be found on https://factset.github.io/quart-openapi/

Quart-OpenAPI is an extension for Quart that adds support for generating a openapi.json file using openapi 3.0. If you are familiar with Quart, this just wraps around it to add a openapi.json route similar to Flask-RESTX generating a swagger.json route and adds a Resource base class for building RESTful APIs.

Compatibility

Quart-OpenAPI requires Python 3.6+ because Quart requires it.

Starting from version 1.6.0, Quart-OpenAPI requires python 3.7+ in order to avoid having to maintain multiple versions of function definitions for compatibility with the older versions of Quart that supported Python 3.6.

Installation

You can install via pip

$ pip install quart-openapi

If you are developing the module and want to also be able to build the documentation, make sure to also install the dependencies from the extras ‘doc’ package like so:

$ pip install 'quart-openapi[doc]'
$ python setup.py build_sphinx

Quick Start

If you’re familiar with Quart then the quick start doesn’t change much:

from quart_openapi import Pint, Resource

app = Pint(__name__, title='Sample App')

@app.route('/')
class Root(Resource):
  async def get(self):
    '''Hello World Route

    This docstring will show up as the description and short-description
    for the openapi docs for this route.
    '''
    return "hello"

This is equivalent to using the following with Quart as normal:

from quart import Quart
app = Quart(__name__)

@app.route('/')
async def hello():
  return "hello"

Except that by using Pint and Resource it will also add a route for ‘/openapi.json’ which will contain the documentation of the route and use the docstring for the description.

Unit Tests

Unit tests can be run through setuptools also:

$ python setup.py test

Request Validation

Request validation like you can get with Flask-RESTX!

You can either create validator models on the fly or you can create a jsonschema document for base models and then use references to it. For an on-the-fly validator:

expected = app.create_validator('sample_request', {
  'type': 'object',
  'properties': {
    'foobar': {
      'type': 'string'
    },
    'baz': {
      'oneOf': [
        { 'type': 'integer' },
        { 'type': 'number', 'format': 'float' }
      ]
    }
  }
})

@app.route('/')
class Sample(Resource):
  @app.expect(expected)
  async def post(self):
    # won't get here if the request didn't match the expected schema
    data = await request.get_json()
    return jsonify(data)

The default content type is ‘application/json’, but you can specify otherwise in the decorator:

{
  "$schema": "http://json-schema.org/schema#",
  "id": "schema.json",
  "components": {
    "schemas": {
      "binaryData": {
        "type": "string",
        "format": "binary"
      }
    }
  }
}
app = Pint(__name__, title='Validation Example',
              base_model_schema='schema.json')
stream = app.create_ref_validator('binaryData', 'schemas')

@app.route('/')
class Binary(Resource):
  @app.expect((stream, 'application/octet-stream',
               {'description': 'gzip compressed data'}))
  @app.response(HTTPStatus.OK, 'Success')
  async def post(self):
    # if the request didn't have a 'content-type' header with a value
    # of 'application/octet-stream' it will be rejected as invalid.
    raw_data = await request.get_data(raw=True)
    # ... do something with the data
    return "Success!"

In the example above, it’ll open, read, and json parse the file schema.json and then use it as the basis for referencing models and creating validators. Currently the validator won’t do more than validate content-type for content-types other than ‘application/json’.

Project details


Download files

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

Source Distribution

quart-openapi-1.6.0.tar.gz (21.3 kB view details)

Uploaded Source

Built Distributions

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

quart_openapi-1.6.0-py3.7.egg (47.1 kB view details)

Uploaded Egg

quart_openapi-1.6.0-py3-none-any.whl (22.5 kB view details)

Uploaded Python 3

File details

Details for the file quart-openapi-1.6.0.tar.gz.

File metadata

  • Download URL: quart-openapi-1.6.0.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.6

File hashes

Hashes for quart-openapi-1.6.0.tar.gz
Algorithm Hash digest
SHA256 dca690853a28f66f144bbcfbaa08a295e51172556870e968da031b0e9d4cd372
MD5 2c36464e24e8d66e0e44dbedb3f825ad
BLAKE2b-256 e00ec203000d359ac869b3b7b97ccda7c4defd8c5a6e3dc5ab05501840875ca6

See more details on using hashes here.

File details

Details for the file quart_openapi-1.6.0-py3.7.egg.

File metadata

  • Download URL: quart_openapi-1.6.0-py3.7.egg
  • Upload date:
  • Size: 47.1 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.6

File hashes

Hashes for quart_openapi-1.6.0-py3.7.egg
Algorithm Hash digest
SHA256 4c22bd5c00141ce2307ff1174ef0ab289de1c2b4ff743036c8a046e9f0765d97
MD5 ace15c20f4b2c47a8b15bb595f585f5f
BLAKE2b-256 6be560791a2e4c77d8b0c250aa310b77e1c9af59bf4884ac10b65888eb457958

See more details on using hashes here.

File details

Details for the file quart_openapi-1.6.0-py3-none-any.whl.

File metadata

  • Download URL: quart_openapi-1.6.0-py3-none-any.whl
  • Upload date:
  • Size: 22.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.6

File hashes

Hashes for quart_openapi-1.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 03ae3ffbacc1c183af456d29403ccfca05a197578c7f20e6ed95d1c6d6a73671
MD5 af094e5f51d7b0d20e1c86a3b50a1978
BLAKE2b-256 9825dc81dfa2292a043cd422ffde4679271fc45de8057da135dc21125aaa83e4

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