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

47 lines
1.3 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 dumps(request.media)
2020-02-17 16:33:01 +00:00
)
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,
)
url_pattern = request.prefix + request.path
return OpenAPIRequest(
full_url_pattern=url_pattern,
method=method,
parameters=parameters,
2020-02-17 16:33:01 +00:00
body=body,
mimetype=mimetype,
)