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
|
|
|
|
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
|
2020-02-17 16:33:01 +00:00
|
|
|
def create(cls, request):
|
2019-11-21 22:50:57 +00:00
|
|
|
"""
|
|
|
|
Create OpenAPIRequest from falcon Request and route params.
|
|
|
|
"""
|
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 = {}
|
|
|
|
|
|
|
|
# Support falcon-jsonify.
|
|
|
|
body = (
|
|
|
|
dumps(request.json) if getattr(request, "json", None)
|
2020-04-06 20:10:23 +00:00
|
|
|
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]
|
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
|
|
|
)
|