tornado-openapi3/tornado_openapi3/responses.py
Correl Roush 4102a14883 Leave request and response bodies as binary data
Decoding to UTF-8 text is the responsibility of the content-type loader
used in the validator, and should not be done prior.
2021-01-06 14:50:15 -05:00

30 lines
1,004 B
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 # type: ignore
from .requests import TornadoRequestFactory
class TornadoResponseFactory:
@classmethod
def create(cls, response: HTTPResponse) -> OpenAPIResponse:
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):
def validate(self, response: HTTPResponse) -> ResponseValidationResult:
return super().validate(
TornadoRequestFactory.create(response.request),
TornadoResponseFactory.create(response),
)
__all__ = ["ResponseValidator", "TornadoResponseFactory"]