openapi-core/openapi_core/contrib/falcon/requests.py

46 lines
1.2 KiB
Python
Raw Normal View History

"""OpenAPI core contrib falcon responses module"""
2020-02-17 16:33:01 +00:00
from json import dumps
2019-12-02 16:25:40 +00:00
2019-11-28 15:57:18 +00:00
from werkzeug.datastructures import ImmutableMultiDict
2020-02-17 16:33:01 +00:00
from openapi_core.validation.request.datatypes import (
OpenAPIRequest, RequestParameters,
)
class FalconOpenAPIRequestFactory:
2020-02-17 16:33:01 +00:00
@classmethod
2020-02-17 16:33:01 +00:00
def create(cls, request):
"""
Create OpenAPIRequest from falcon Request and route params.
"""
2020-02-17 16:33:01 +00:00
method = request.method.lower()
2020-02-17 16:33:01 +00:00
# gets deduced by path finder against spec
path = {}
# Support falcon-jsonify.
body = (
dumps(request.json) if getattr(request, "json", None)
else request.bounded_stream.read()
)
mimetype = request.options.default_media_type
if request.content_type:
mimetype = request.content_type.partition(";")[0]
2020-02-17 16:33:01 +00:00
query = ImmutableMultiDict(request.params.items())
parameters = RequestParameters(
2020-02-17 16:33:01 +00:00
query=query,
header=request.headers,
cookie=request.cookies,
path=path,
)
return OpenAPIRequest(
2020-02-17 16:33:01 +00:00
full_url_pattern=request.url,
method=method,
parameters=parameters,
2020-02-17 16:33:01 +00:00
body=body,
mimetype=mimetype,
)