mirror of
https://github.com/correl/tornado-openapi3.git
synced 2024-11-23 11:09:56 +00:00
a2c25247d9
- Drop support for tornado<6 - Drop support for Python<3.9 - Add support for Python 3.10, 3.11, 3.12, and 3.13
37 lines
1.3 KiB
Python
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"]
|