tornado-openapi3/tornado_openapi3/responses.py
Correl Roush a2c25247d9 Update supported tornado and python versions
- Drop support for tornado<6
- Drop support for Python<3.9
- Add support for Python 3.10, 3.11, 3.12, and 3.13
2024-10-20 22:19:57 -04:00

37 lines
1.3 KiB
Python

from openapi_core.validation.response.datatypes import ( # type: ignore
OpenAPIResponse,
ResponseValidationResult,
)
from openapi_core.validation.response import validators # type: ignore
from tornado.httpclient import HTTPResponse
from .requests import TornadoRequestFactory
from .util import parse_mimetype
class TornadoResponseFactory:
"""Factory for converting Tornado responses to OpenAPI response objects."""
@classmethod
def create(cls, response: HTTPResponse) -> OpenAPIResponse:
"""Creates an OpenAPI response from Tornado response objects."""
mimetype = parse_mimetype(response.headers.get("Content-Type", "text/html"))
return OpenAPIResponse(
data=response.body if response.body else b"",
status_code=response.code,
mimetype=mimetype,
)
class ResponseValidator(validators.ResponseValidator):
"""Validator for Tornado HTTP Responses."""
def validate(self, response: HTTPResponse) -> ResponseValidationResult:
"""Validate a Tornado HTTP response object."""
return super().validate(
TornadoRequestFactory.create(response.request),
TornadoResponseFactory.create(response),
)
__all__ = ["ResponseValidator", "TornadoResponseFactory"]