Swagger/OpenAPI UI integration utilities for aiohttp applications
Project description
aiohttp-swagger-utils
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aiohttp_swagger_utils-0.1.0.tar.gz.
File metadata
- Download URL: aiohttp_swagger_utils-0.1.0.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3cd232e8afa3d18f92e0f3f2c154c55734ab7077d8c7edd55172198d1b5e2289
|
|
| MD5 |
3d08a09e70e0e97759a5636ebe22469e
|
|
| BLAKE2b-256 |
7cddbbd19d27a61f03b6922eeca06a5e627aaa60cbdaf2d39f17462d194a4296
|
File details
Details for the file aiohttp_swagger_utils-0.1.0-py3-none-any.whl.
File metadata
- Download URL: aiohttp_swagger_utils-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9795a9f334465e7d9967db95276cbe8d9c92dce7dd6376608b4de4bbf7f69e1a
|
|
| MD5 |
a39464c2ac9113800caedf8b7d3d102a
|
|
| BLAKE2b-256 |
08953016e7bfb8312c98866571905ee9bbeac0cfa0ffc8a947dcad39a786cb1b
|