From 58b98205c79159da1f4790fa764c3951c3597331 Mon Sep 17 00:00:00 2001 From: Lars de Ridder Date: Thu, 21 Nov 2019 23:50:57 +0100 Subject: [PATCH] Falcon OpenAPI request and response factories --- openapi_core/contrib/falcon/__init__.py | 0 openapi_core/contrib/falcon/requests.py | 27 ++++++++++++++++++++++++ openapi_core/contrib/falcon/responses.py | 12 +++++++++++ 3 files changed, 39 insertions(+) create mode 100644 openapi_core/contrib/falcon/__init__.py create mode 100644 openapi_core/contrib/falcon/requests.py create mode 100644 openapi_core/contrib/falcon/responses.py diff --git a/openapi_core/contrib/falcon/__init__.py b/openapi_core/contrib/falcon/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/openapi_core/contrib/falcon/requests.py b/openapi_core/contrib/falcon/requests.py new file mode 100644 index 0000000..74630d6 --- /dev/null +++ b/openapi_core/contrib/falcon/requests.py @@ -0,0 +1,27 @@ +"""OpenAPI core contrib falcon responses module""" +from openapi_core.validation.request.datatypes import OpenAPIRequest, RequestParameters + + +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( + path=route_params, query=req.params, header=headers, cookie=req.cookies + ) + return OpenAPIRequest( + host_url=req.host, + path=req.path, + path_pattern=req.uri_template, + method=method, + parameters=parameters, + body=req.bounded_stream.read(), + mimetype=req.content_type.partition(";")[0] if req.content_type else "", + ) diff --git a/openapi_core/contrib/falcon/responses.py b/openapi_core/contrib/falcon/responses.py new file mode 100644 index 0000000..8855c40 --- /dev/null +++ b/openapi_core/contrib/falcon/responses.py @@ -0,0 +1,12 @@ +"""OpenAPI core contrib falcon responses module""" +from openapi_core.validation.response.datatypes import OpenAPIResponse + + +class FalconOpenAPIResponseFactory(object): + @classmethod + def create(cls, resp): + return OpenAPIResponse( + data=resp.body, + status_code=resp.status[:3], + mimetype=resp.content_type.partition(";")[0] if resp.content_type else '', + )