2020-11-26 05:05:02 +00:00
|
|
|
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 # type: ignore
|
|
|
|
|
|
|
|
from .requests import TornadoRequestFactory
|
2021-01-13 19:33:39 +00:00
|
|
|
from .util import parse_mimetype
|
2020-11-26 05:05:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TornadoResponseFactory:
|
2021-02-26 17:03:04 +00:00
|
|
|
"""Factory for converting Tornado responses to OpenAPI response objects."""
|
2021-02-26 20:34:54 +00:00
|
|
|
|
2020-11-26 05:05:02 +00:00
|
|
|
@classmethod
|
|
|
|
def create(cls, response: HTTPResponse) -> OpenAPIResponse:
|
2021-02-26 17:03:04 +00:00
|
|
|
"""Creates an OpenAPI response from Tornado response objects."""
|
2021-01-13 19:33:39 +00:00
|
|
|
mimetype = parse_mimetype(response.headers.get("Content-Type", "text/html"))
|
2020-11-26 05:05:02 +00:00
|
|
|
return OpenAPIResponse(
|
2021-01-06 19:50:15 +00:00
|
|
|
data=response.body if response.body else b"",
|
2020-11-26 05:05:02 +00:00
|
|
|
status_code=response.code,
|
|
|
|
mimetype=mimetype,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ResponseValidator(validators.ResponseValidator):
|
2021-02-26 17:03:04 +00:00
|
|
|
"""Validator for Tornado HTTP Responses."""
|
2021-02-26 20:34:54 +00:00
|
|
|
|
2020-11-26 05:05:02 +00:00
|
|
|
def validate(self, response: HTTPResponse) -> ResponseValidationResult:
|
2021-02-26 17:03:04 +00:00
|
|
|
"""Validate a Tornado HTTP response object."""
|
2020-11-26 05:05:02 +00:00
|
|
|
return super().validate(
|
|
|
|
TornadoRequestFactory.create(response.request),
|
|
|
|
TornadoResponseFactory.create(response),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = ["ResponseValidator", "TornadoResponseFactory"]
|