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

49 lines
1.4 KiB
Python
Raw Permalink 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
2021-04-30 23:30:55 +00:00
from openapi_core.contrib.falcon.compat import get_request_media
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
def create(cls, request, default_when_empty={}):
"""
Create OpenAPIRequest from falcon Request and route params.
"""
default = default_when_empty
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 = {}
2021-04-30 23:30:55 +00:00
media = get_request_media(request, default=default)
2021-04-21 16:05:53 +00:00
# Support falcon-jsonify.
2020-02-17 16:33:01 +00:00
body = (
dumps(getattr(request, "json", 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,
)