2019-10-19 11:01:50 +00:00
|
|
|
"""OpenAPI core contrib flask requests module"""
|
2019-06-11 22:34:48 +00:00
|
|
|
import re
|
|
|
|
|
2020-02-11 14:54:36 +00:00
|
|
|
from six.moves.urllib.parse import urljoin
|
|
|
|
|
2019-10-19 12:35:48 +00:00
|
|
|
from openapi_core.validation.request.datatypes import (
|
|
|
|
RequestParameters, OpenAPIRequest,
|
|
|
|
)
|
2018-04-17 14:37:52 +00:00
|
|
|
|
2019-06-11 22:34:48 +00:00
|
|
|
# http://flask.pocoo.org/docs/1.0/quickstart/#variable-rules
|
|
|
|
PATH_PARAMETER_PATTERN = r'<(?:(?:string|int|float|path|uuid):)?(\w+)>'
|
|
|
|
|
2018-04-17 14:37:52 +00:00
|
|
|
|
2019-10-19 12:35:48 +00:00
|
|
|
class FlaskOpenAPIRequestFactory(object):
|
2018-04-17 14:37:52 +00:00
|
|
|
|
2019-06-11 22:34:48 +00:00
|
|
|
path_regex = re.compile(PATH_PARAMETER_PATTERN)
|
|
|
|
|
2019-10-19 12:35:48 +00:00
|
|
|
@classmethod
|
|
|
|
def create(cls, request):
|
|
|
|
method = request.method.lower()
|
|
|
|
|
|
|
|
if request.url_rule is None:
|
|
|
|
path_pattern = request.path
|
|
|
|
else:
|
|
|
|
path_pattern = cls.path_regex.sub(r'{\1}', request.url_rule.rule)
|
|
|
|
|
|
|
|
parameters = RequestParameters(
|
|
|
|
path=request.view_args,
|
|
|
|
query=request.args,
|
|
|
|
header=request.headers,
|
|
|
|
cookie=request.cookies,
|
|
|
|
)
|
2020-02-11 14:54:36 +00:00
|
|
|
full_url_pattern = urljoin(request.host_url, path_pattern)
|
2019-10-19 12:35:48 +00:00
|
|
|
return OpenAPIRequest(
|
2020-02-11 14:54:36 +00:00
|
|
|
full_url_pattern=full_url_pattern,
|
2019-10-19 12:35:48 +00:00
|
|
|
method=method,
|
|
|
|
parameters=parameters,
|
|
|
|
body=request.data,
|
|
|
|
mimetype=request.mimetype,
|
|
|
|
)
|