Add kaya-openapi package for automatic OpenAPI spec generation
CI / Build Pip package (push) Successful in 2m49s
CI / Build Pip package (push) Successful in 2m49s
- New packages/kaya-openapi with OpenAPIMixin, @operation decorator, and generate_spec() that walks the routing tree - Enables kaya-core's Tree.register to expose the original handler callback as an instance attribute for metadata introspection - Registers GET /openapi.json and GET /docs (Swagger UI) routes - Supports and path parameters, docstring descriptions, @operation metadata, and excludes wildcard/WS routes - Adds example/openapi.py, updates CI, README, and requirements
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
from kaya.core import HttpContext, KayaApp
|
||||
from kaya.openapi import OpenAPIMixin, operation
|
||||
|
||||
app = KayaApp(mixins=[OpenAPIMixin(
|
||||
title='Greeting API',
|
||||
version='1.0.0',
|
||||
description='Example API documented with kaya-openapi',
|
||||
)])
|
||||
|
||||
|
||||
@app.GET('/hello')
|
||||
async def hello(ctx: HttpContext) -> None:
|
||||
"""Say hello to the world."""
|
||||
await ctx.send_str(200, 'Hello World')
|
||||
|
||||
|
||||
@app.GET('/hello/${name}')
|
||||
@operation(summary='Greet someone',
|
||||
tags=['greetings'],
|
||||
responses={200: {'description': 'A personalized greeting'}})
|
||||
async def hello_name(ctx: HttpContext, name: str) -> None:
|
||||
await ctx.send_str(200, f'Hello {name}')
|
||||
|
||||
|
||||
@app.GET('/square/${x:int}')
|
||||
@operation(summary='Compute the square of a number', tags=['math'])
|
||||
async def square(ctx: HttpContext, x: int) -> None:
|
||||
await ctx.send_str(200, str(x * x))
|
||||
|
||||
# serve with an ASGI/RSGI server, e.g.:
|
||||
# granian --interface rsgi example.openapi:app
|
||||
# then open http://localhost:8000/docs to browse the API
|
||||
Reference in New Issue
Block a user