Skip to main content

aiohttp-swagger-utils

PyPI version PyPI downloads License: MIT Python Versions

A utility package for integrating Swagger/OpenAPI UI into aiohttp applications. Simplifies setup, adds middleware for basic request validation, and supports both Swagger UI and ReDoc.

Features

  • 🚀 Easy Integration: Simple setup with minimal configuration
  • 📋 Swagger UI Support: Automatic Swagger UI endpoint generation
  • 📖 ReDoc Support: Alternative API documentation viewer
  • ✅ Request Validation: Middleware for basic OpenAPI schema validation
  • 📄 Multiple Spec Formats: Support for YAML and JSON OpenAPI specifications
  • 🔧 Flexible Configuration: Customizable UI options and validation settings
  • 🧪 Type Hints: Full type annotations for better IDE support
  • 📚 Well Documented: Comprehensive documentation and examples

Installation

Install via pip:

pip install aiohttp-swagger-utils

Quick Start

Basic Usage

from aiohttp import web
from aiohttp_swagger_utils import setup_swagger_ui, setup_redoc

app = web.Application()

# Load your OpenAPI specification
spec_path = "openapi.yaml"

# Setup Swagger UI
setup_swagger_ui(app, spec_path=spec_path, url_prefix="/api/docs")

# Setup ReDoc (optional)
setup_redoc(app, spec_path=spec_path, url_prefix="/api/redoc")

# Your routes
async def hello(request):
    return web.json_response({"message": "Hello World"})

app.router.add_get("/", hello)

web.run_app(app)

With Request Validation

from aiohttp import web
from aiohttp_swagger_utils import setup_swagger_ui, SwaggerValidationMiddleware

app = web.Application()

# Add validation middleware
app.middlewares.append(SwaggerValidationMiddleware("openapi.yaml").middleware)

# Setup Swagger UI
setup_swagger_ui(app, spec_path="openapi.yaml")

async def create_user(request):
    # Request will be validated against OpenAPI spec
    data = await request.json()
    return web.json_response({"status": "created", "data": data})

app.router.add_post("/users", create_user)

web.run_app(app)

Configuration Options

setup_swagger_ui()

setup_swagger_ui(
    app: web.Application,
    spec_path: str,
    url_prefix: str = "/api/docs",
    title: str = "API Documentation",
    enable_validator: bool = True,
    oauth2_redirect_url: Optional[str] = None,
    custom_css_url: Optional[str] = None,
    custom_js_url: Optional[str] = None
)

setup_redoc()

setup_redoc(
    app: web.Application,
    spec_path: str,
    url_prefix: str = "/api/redoc",
    title: str = "API Documentation",
    expand_responses: str = "200,201",
    required_props_first: bool = True
)

SwaggerValidationMiddleware

middleware = SwaggerValidationMiddleware(
    spec_path: str,
    validate_request: bool = True,
    validate_response: bool = False,
    raise_on_error: bool = True
)

Advanced Example

from aiohttp import web
from aiohttp_swagger_utils import (
    setup_swagger_ui,
    setup_redoc,
    SwaggerValidationMiddleware
)

async def init_app():
    app = web.Application()
    
    # Load OpenAPI spec
    spec_path = "openapi.yaml"
    
    # Add validation middleware
    validation_middleware = SwaggerValidationMiddleware(
        spec_path=spec_path,
        validate_request=True,
        validate_response=False,
        raise_on_error=True
    )
    app.middlewares.append(validation_middleware.middleware)
    
    # Setup Swagger UI
    setup_swagger_ui(
        app=app,
        spec_path=spec_path,
        url_prefix="/docs",
        title="My Awesome API",
        enable_validator=True,
        custom_css_url="/static/custom-swagger.css"
    )
    
    # Setup ReDoc as alternative
    setup_redoc(
        app=app,
        spec_path=spec_path,
        url_prefix="/redoc",
        title="My Awesome API - ReDoc",
        expand_responses="200,201,204",
        required_props_first=True
    )
    
    # Add routes
    app.router.add_get("/", lambda r: web.json_response({"status": "ok"}))
    app.router.add_post("/items", create_item)
    
    return app

if __name__ == "__main__":
    web.run_app(init_app(), port=8080)

Testing

Run tests with pytest:

pytest tests/

Or with unittest:

python -m unittest discover tests

Metadata

Release files for aiohttp-swagger-utils 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiohttp-swagger-utils 0.1.0
File Size Uploaded Provenance
aiohttp_swagger_utils-0.1.0.tar.gz 8.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for aiohttp-swagger-utils 0.1.0
File Interpreter ABI Platform
aiohttp_swagger_utils-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.1 kB

Release files / aiohttp_swagger_utils-0.1.0.tar.gz

Download URL aiohttp_swagger_utils-0.1.0.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3cd232e8afa3d18f92e0f3f2c154c55734ab7077d8c7edd55172198d1b5e2289
BLAKE2b-256 checksum
How to use checksums
7cddbbd19d27a61f03b6922eeca06a5e627aaa60cbdaf2d39f17462d194a4296
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.0

Release files / aiohttp_swagger_utils-0.1.0-py3-none-any.whl

Download URL aiohttp_swagger_utils-0.1.0-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9795a9f334465e7d9967db95276cbe8d9c92dce7dd6376608b4de4bbf7f69e1a
BLAKE2b-256 checksum
How to use checksums
08953016e7bfb8312c98866571905ee9bbeac0cfa0ffc8a947dcad39a786cb1b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.0

Security for aiohttp-swagger-utils 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page