2019-11-21 22:50:57 +00:00
|
|
|
"""OpenAPI core contrib falcon responses module"""
|
2019-12-02 16:25:40 +00:00
|
|
|
import json
|
|
|
|
|
2019-11-21 22:50:57 +00:00
|
|
|
from openapi_core.validation.request.datatypes import OpenAPIRequest, RequestParameters
|
2019-11-28 15:57:18 +00:00
|
|
|
from werkzeug.datastructures import ImmutableMultiDict
|
2019-11-21 22:50:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FalconOpenAPIRequestFactory:
|
|
|
|
@classmethod
|
|
|
|
def create(cls, req, route_params):
|
|
|
|
"""
|
|
|
|
Create OpenAPIRequest from falcon Request and route params.
|
|
|
|
"""
|
|
|
|
method = req.method.lower()
|
|
|
|
|
|
|
|
# Convert keys to lowercase as that's what the OpenAPIRequest expects.
|
|
|
|
headers = {key.lower(): value for key, value in req.headers.items()}
|
|
|
|
|
|
|
|
parameters = RequestParameters(
|
2019-11-28 15:57:18 +00:00
|
|
|
path=route_params,
|
|
|
|
query=ImmutableMultiDict(req.params.items()),
|
|
|
|
header=headers,
|
|
|
|
cookie=req.cookies,
|
2019-11-21 22:50:57 +00:00
|
|
|
)
|
|
|
|
return OpenAPIRequest(
|
2020-01-28 09:23:02 +00:00
|
|
|
host_url=None,
|
2019-11-21 22:50:57 +00:00
|
|
|
path=req.path,
|
2020-01-28 09:23:02 +00:00
|
|
|
path_pattern=req.uri_template or req.path,
|
2019-11-21 22:50:57 +00:00
|
|
|
method=method,
|
|
|
|
parameters=parameters,
|
2019-12-02 16:25:40 +00:00
|
|
|
# Support falcon-jsonify.
|
|
|
|
body=json.dumps(req.json)
|
|
|
|
if getattr(req, "json", None)
|
|
|
|
else req.bounded_stream.read(),
|
2019-11-21 22:50:57 +00:00
|
|
|
mimetype=req.content_type.partition(";")[0] if req.content_type else "",
|
|
|
|
)
|