2019-11-21 22:50:57 +00:00
|
|
|
"""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
|
2019-11-21 22:50:57 +00:00
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
2019-11-21 22:50:57 +00:00
|
|
|
|
|
|
|
class FalconOpenAPIRequestFactory:
|
2020-02-17 16:33:01 +00:00
|
|
|
|
2019-11-21 22:50:57 +00:00
|
|
|
@classmethod
|
2021-04-21 16:04:05 +00:00
|
|
|
def create(cls, request, default_when_empty={}):
|
2019-11-21 22:50:57 +00:00
|
|
|
"""
|
|
|
|
Create OpenAPIRequest from falcon Request and route params.
|
|
|
|
"""
|
2021-04-21 16:04:05 +00:00
|
|
|
default = default_when_empty
|
2020-02-17 16:33:01 +00:00
|
|
|
method = request.method.lower()
|
2019-11-21 22:50:57 +00:00
|
|
|
|
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 = (
|
2021-04-21 16:04:05 +00:00
|
|
|
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]
|
2019-11-21 22:50:57 +00:00
|
|
|
|
2020-02-17 16:33:01 +00:00
|
|
|
query = ImmutableMultiDict(request.params.items())
|
2019-11-21 22:50:57 +00:00
|
|
|
parameters = RequestParameters(
|
2020-02-17 16:33:01 +00:00
|
|
|
query=query,
|
|
|
|
header=request.headers,
|
|
|
|
cookie=request.cookies,
|
|
|
|
path=path,
|
2019-11-21 22:50:57 +00:00
|
|
|
)
|
2020-04-06 20:10:23 +00:00
|
|
|
url_pattern = request.prefix + request.path
|
2019-11-21 22:50:57 +00:00
|
|
|
return OpenAPIRequest(
|
2020-04-06 20:10:23 +00:00
|
|
|
full_url_pattern=url_pattern,
|
2019-11-21 22:50:57 +00:00
|
|
|
method=method,
|
|
|
|
parameters=parameters,
|
2020-02-17 16:33:01 +00:00
|
|
|
body=body,
|
|
|
|
mimetype=mimetype,
|
2019-11-21 22:50:57 +00:00
|
|
|
)
|