Easy APISpec integration for Starlette
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:
Testing
Clone the repo
Activate venv . venv/bin/activate
Install dependencies
poetry install
Run tests
./scripts/test
Contributing
PRs are welcome!
Release files for starlette-apispec 2.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| starlette_apispec-2.2.1.tar.gz | 4.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| starlette_apispec-2.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.4 kB
Release files / starlette_apispec-2.2.1.tar.gz
| Download URL | starlette_apispec-2.2.1.tar.gz |
|---|---|
| Size | 4.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b2cd3721a3cc7ebc31610e8470c12c6efff453a5732892285e4076c73f164f96
|
|
BLAKE2b-256 checksum How to use checksums |
06f3f8678f7fc5e432c52ea6dac6072c6083d56c69e8f1e0e170c908681284bc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.12.1 Linux/6.2.0-1019-azure
|
Release files / starlette_apispec-2.2.1-py3-none-any.whl
| Download URL | starlette_apispec-2.2.1-py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
14b64f8551a06f2d4f1762940a0b4e1ec6c87b85d945bbc8dc8bfcf30e592ce6
|
|
BLAKE2b-256 checksum How to use checksums |
34004155910178cd10828746e005e957ffae6d1f04e943b0ff6155912110e17b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.12.1 Linux/6.2.0-1019-azure
|