Skip to main content

APISpec support for starlette

Project description

Easy APISpec integration for Starlette

Travis Codecov PyPI PyPI - Python Version
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from starlette.applications import Starlette
from starlette_apispec import APISpecSchemaGenerator

app = Starlette()

schemas = APISpecSchemaGenerator(
    APISpec(
        title="Example API",
        version="1.0",
        openapi_version="3.0.0",
        info={"description": "explanation of the api purpose"},
        plugins=[MarshmallowPlugin()],
    )
)

@app.route("/schema", methods=["GET"], include_in_schema=False)
def schema(request):
    return schemas.OpenAPIResponse(request=request)

Installation

pip install -U starlette-apispec

Alternatively you can do

poetry add starlette-apispec

About

This library helps you easily document your REST API built with starlette.

Starlette is a is a lightweight ASGI framework/toolkit, which is ideal for building high performance asyncio services.

APISpec supports the OpenApi Specification and it has some useful plugins like marshmallow support.

Version supported: ^1.0.0

Usage

This example includes marshmallow integration

from apispec import APISpec

from starlette.applications import Starlette
from starlette.endpoints import HTTPEndpoint
from starlette.testclient import TestClient

from starlette_apispec import APISpecSchemaGenerator


app = Starlette()

schemas = APISpecSchemaGenerator(
    APISpec(
        title="Example API",
        version="1.0",
        openapi_version="3.0.0",
        info={"description": "explanation of the api purpose"},
    )
)


@app.websocket_route("/ws")
def ws(session):
    """ws"""
    pass  # pragma: no cover


@app.route("/users", methods=["GET", "HEAD"])
def list_users(request):
    """
    responses:
    200:
        description: A list of users.
        examples:
        [{"username": "tom"}, {"username": "lucy"}]
    """
    pass  # pragma: no cover


@app.route("/users", methods=["POST"])
def create_user(request):
    """
    responses:
    200:
        description: A user.
        examples:
        {"username": "tom"}
    """
    pass  # pragma: no cover


@app.route("/orgs")
class OrganisationsEndpoint(HTTPEndpoint):
    def get(self, request):
        """
        responses:
        200:
            description: A list of organisations.
            examples:
            [{"name": "Foo Corp."}, {"name": "Acme Ltd."}]
        """
        pass  # pragma: no cover

    def post(self, request):
        """
        responses:
        200:
            description: An organisation.
            examples:
            {"name": "Foo Corp."}
        """
        pass  # pragma: no cover


@app.route("/schema", methods=["GET"], include_in_schema=False)
def schema(request):
    return schemas.OpenAPIResponse(request=request)

More documentation

This package is basically a proxy, so if you wonder how to do something, here are the sources you need:

Starlette documentation

APISpec documentation

Testing

  1. Clone the repo

  2. Activate venv . venv/bin/activate

  3. Install dependencies

poetry install
  1. Run tests

./scripts/test

Contributing

PRs are welcome!

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

starlette-apispec-1.0.3.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

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

starlette_apispec-1.0.3-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file starlette-apispec-1.0.3.tar.gz.

File metadata

  • Download URL: starlette-apispec-1.0.3.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0b3 CPython/3.7.3 Linux/5.0.0-32-generic

File hashes

Hashes for starlette-apispec-1.0.3.tar.gz
Algorithm Hash digest
SHA256 ed961c898dcc03b580c03915dbaa0e1d52c17b51c20be27ee580b196fbbc1f97
MD5 4f1f509333a15d329d8ab0b4166b0793
BLAKE2b-256 1303074e77fcf292b150f099f5121f445ba28e5520d61eb72ad8a255917f0776

See more details on using hashes here.

File details

Details for the file starlette_apispec-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: starlette_apispec-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0b3 CPython/3.7.3 Linux/5.0.0-32-generic

File hashes

Hashes for starlette_apispec-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5c8e0ec681ad375f12ace236ea3c8229d999a65fb086a887875d7a0be439bfc2
MD5 ec871ff1d0da50b0b276546a6837b5bb
BLAKE2b-256 6654233ba4e32519d2195ddb711e2c5de78924e14c4a9f954c01ce34415f02c9

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