Skip to main content

OpenAPI v3 support for Sanic

Project description

Sanic OpenAPI v3

Build Status PyPI PyPI

Give your Sanic API an OpenAPI v3 specification. Based on original Sanic OpenAPI extension.

Installation

pip install sanic-openapi3

Usage

Import blueprint and use simple decorators to document routes:

from sanic_openapi3 import openapi, openapi_blueprint

@app.get("/user/<user_id:int>")
@openapi.summary("Fetches a user by ID")
@openapi.response(200, { "user": { "name": str, "id": int } })
async def get_user(request, user_id):
    ...

@app.post("/user")
@openapi.summary("Creates a user")
@openapi.body({"user": { "name": str }})
async def create_user(request):
    ...

app.blueprint(openapi_blueprint)

You'll now have a specification at the URL /openapi.json. Your routes will be automatically categorized by their blueprints.

Model your input/output

class Car:
    make = str
    model = str
    year = int

class Garage:
    spaces = int
    cars = [Car]

@app.get("/garage")
@openapi.summary("Gets the whole garage")
@openapi.response(200, Garage)
async def get_garage(request):
    return json({
        "spaces": 2,
        "cars": [{"make": "Nissan", "model": "370Z"}]
    })

Get more descriptive

class Car:
    make = doc.String("Who made the car")
    model = doc.String("Type of car.  This will vary by make")
    year = doc.Integer("4-digit year of the car", required=False)

class Garage:
    spaces = doc.Integer("How many cars can fit in the garage")
    cars = doc.List(Car, description="All cars in the garage")

Configure all the things

app.config.OPENAPI_VERSION = '1.0.0'
app.config.OPENAPI_TITLE = 'Car API'
app.config.OPENAPI_DESCRIPTION = 'Car API'
app.config.OPENAPI_TERMS_OF_SERVICE = 'https://example.com/terms'
app.config.OPENAPI_CONTACT_EMAIL = 'mail@example.com'
app.config.OPENAPI_CONTACT_NAME = 'mail@example.com'

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

sanic-openapi3-0.0.2.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

sanic_openapi3-0.0.2-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file sanic-openapi3-0.0.2.tar.gz.

File metadata

File hashes

Hashes for sanic-openapi3-0.0.2.tar.gz
Algorithm Hash digest
SHA256 3e93ed8390fa54c6695817a0f4b6ba415bea837850f921718d2cec9c3785130b
MD5 1611b5310086c0ad140ce7e976249adf
BLAKE2b-256 99373a136e8c7a9465d05166d48a3e90d7d23dabdab9cebb426d8f557474fd7b

See more details on using hashes here.

File details

Details for the file sanic_openapi3-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for sanic_openapi3-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 76ddfcd24e087bdaa7c662f76aa88792211623672e895759a299cf406d3b08e4
MD5 7f9a10453e4cfc9c57da423b0a04ecde
BLAKE2b-256 53de941491e368c6e307694c70adc3becae83633e39a45c9e41a7e849ab28345

See more details on using hashes here.

Supported by

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