mirror of
https://github.com/correl/tornado-openapi3.git
synced 2024-11-14 19:19:32 +00:00
Add support for tornado.httpclient.HTTPRequest
This commit is contained in:
parent
a5aa5adc9f
commit
53471f3fce
3 changed files with 50 additions and 16 deletions
|
@ -16,6 +16,7 @@ from openapi_core.validation.request.datatypes import ( # type: ignore
|
|||
OpenAPIRequest,
|
||||
)
|
||||
from openapi_core.validation.request.validators import RequestValidator # type: ignore
|
||||
from tornado.httpclient import HTTPRequest # type: ignore
|
||||
from tornado.httputil import HTTPHeaders, HTTPServerRequest # type: ignore
|
||||
from tornado.testing import AsyncHTTPTestCase # type: ignore
|
||||
from tornado.web import Application, RequestHandler # type: ignore
|
||||
|
@ -80,7 +81,21 @@ def parameters(draw, min_headers=0, min_query_parameters=0) -> Parameters:
|
|||
|
||||
|
||||
class TestRequestFactory(unittest.TestCase):
|
||||
def test_request(self) -> None:
|
||||
def test_http_request(self) -> None:
|
||||
tornado_request = HTTPRequest(
|
||||
method="GET", url="http://example.com/foo?bar=baz"
|
||||
)
|
||||
expected = OpenAPIRequest(
|
||||
full_url_pattern="http://example.com/foo",
|
||||
method="get",
|
||||
parameters=RequestParameters(query=ImmutableMultiDict([("bar", "baz")])),
|
||||
body="",
|
||||
mimetype="application/x-www-form-urlencoded",
|
||||
)
|
||||
openapi_request = TornadoRequestFactory.create(tornado_request)
|
||||
self.assertEqual(attr.asdict(expected), attr.asdict(openapi_request))
|
||||
|
||||
def test_http_server_request(self) -> None:
|
||||
tornado_request = HTTPServerRequest(
|
||||
method="GET", uri="http://example.com/foo?bar=baz"
|
||||
)
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
from tornado_openapi3.requests import TornadoRequestFactory
|
||||
from tornado_openapi3.requests import RequestValidator, TornadoRequestFactory
|
||||
|
||||
__all__ = ["TornadoRequestFactory"]
|
||||
__all__ = [
|
||||
"RequestValidator",
|
||||
"TornadoRequestFactory",
|
||||
]
|
||||
|
|
|
@ -1,36 +1,49 @@
|
|||
import itertools
|
||||
from urllib.parse import parse_qsl
|
||||
from typing import Union
|
||||
|
||||
from openapi_core.validation.request.datatypes import ( # type: ignore
|
||||
RequestParameters,
|
||||
OpenAPIRequest,
|
||||
)
|
||||
from openapi_core.validation.request import validators # type: ignore
|
||||
from tornado.httpclient import HTTPRequest # type: ignore
|
||||
from tornado.httputil import HTTPServerRequest # type: ignore
|
||||
from werkzeug.datastructures import ImmutableMultiDict, Headers
|
||||
|
||||
|
||||
class TornadoRequestFactory:
|
||||
@classmethod
|
||||
def create(cls, request: HTTPServerRequest) -> OpenAPIRequest:
|
||||
if request.uri:
|
||||
path, _, _ = request.uri.partition("?")
|
||||
def create(cls, request: Union[HTTPRequest, HTTPServerRequest]) -> OpenAPIRequest:
|
||||
if isinstance(request, HTTPRequest):
|
||||
if request.url:
|
||||
path, _, querystring = request.url.partition("?")
|
||||
query_arguments: ImmutableMultiDict[str, str] = ImmutableMultiDict(
|
||||
parse_qsl(querystring)
|
||||
)
|
||||
else:
|
||||
path = ""
|
||||
query_arguments = ImmutableMultiDict()
|
||||
else:
|
||||
path = ""
|
||||
query_arguments: ImmutableMultiDict[str, str] = ImmutableMultiDict(
|
||||
itertools.chain(
|
||||
*[
|
||||
[(k, v.decode("utf-8")) for v in vs]
|
||||
for k, vs in request.query_arguments.items()
|
||||
]
|
||||
if request.uri:
|
||||
path, _, _ = request.uri.partition("?")
|
||||
else:
|
||||
path = ""
|
||||
query_arguments = ImmutableMultiDict(
|
||||
itertools.chain(
|
||||
*[
|
||||
[(k, v.decode("utf-8")) for v in vs]
|
||||
for k, vs in request.query_arguments.items()
|
||||
]
|
||||
)
|
||||
)
|
||||
)
|
||||
return OpenAPIRequest(
|
||||
full_url_pattern=path,
|
||||
method=request.method.lower() if request.method else "get",
|
||||
parameters=RequestParameters(
|
||||
query=query_arguments, header=Headers(request.headers.get_all())
|
||||
),
|
||||
body=request.body.decode("utf-8"),
|
||||
body=request.body.decode("utf-8") if request.body else "",
|
||||
mimetype=request.headers.get(
|
||||
"Content-Type", "application/x-www-form-urlencoded"
|
||||
),
|
||||
|
@ -38,5 +51,8 @@ class TornadoRequestFactory:
|
|||
|
||||
|
||||
class RequestValidator(validators.RequestValidator):
|
||||
def validate(self, request: HTTPServerRequest):
|
||||
def validate(self, request: Union[HTTPRequest, HTTPServerRequest]):
|
||||
return super().validate(TornadoRequestFactory.create(request))
|
||||
|
||||
|
||||
__all__ = ["RequestValidator", "TornadoRequestFactory"]
|
||||
|
|
Loading…
Reference in a new issue