Tornado Interface¶
pydantictornado.handlers.OpenAPIApplication
¶
Bases: ResponseFormatter
, Application
find_rule_by_name(rule_name)
¶
Find a named rule in the active routes.
:raises ValueError: if the rule name is not found
register_error_model(status_code, error_model, *, description=None)
¶
Register a model as the default response for a HTTP status code.
tag_operation(rule_name, method, *tags)
¶
Tag an operation in the OpenAPI document.
You can pass the tag as a string if the tag already exists
in openapi_doc
. Otherwise, pass a Tag
instance to create
a new tag and apply it to the operation.
:raises ValueError: if the rule name is not found
pydantictornado.handlers.OpenAPIDocHandler
¶
Bases: RequestHandler
pydantictornado.handlers.OpenAPISpecHandler
¶
Bases: RequestHandler
pydantictornado.api.wrap_error(status_code, body, log_message=None, *args, **kwargs)
¶
Wrap a pydantic model in an exception that can be raised.
pydantic.BaseModel instances cannot be raised directly as exceptions
which makes them difficult to use for structured error responses. This
function wraps the model instance in an exception that can be raised.
Call this function from within a handlers.PydanticErrorHandler instance to serialize
body
as the response body when an error is caught.
import pydantic
from pydantictornado import api, handlers
from tornado import web
class NotFoundError(pydantic.BaseModel):
item_id: str
@pydantic.computed_field
@property
def message(self) -> str:
return f'Item {self.item_id} not found'
class MyHandler(handlers.PydanticErrorHandler, web.RequestHandler):
@api.expose_operation
@api.add_error_response(404, model=NotFoundError)
async def get(self, item_id: str) -> None:
raise api.wrap_error(404, NotFoundError(item_id=item_id))
pydantictornado.handlers.PydanticErrorHandler
¶
Bases: RequestHandler