openapi-core/openapi_core/contrib/flask/requests.py

40 lines
1.1 KiB
Python
Raw Normal View History

2019-10-19 11:01:50 +00:00
"""OpenAPI core contrib flask requests module"""
import re
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
# 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
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,
)
return OpenAPIRequest(
host_url=request.host_url,
path=request.path,
path_pattern=path_pattern,
method=method,
parameters=parameters,
body=request.data,
mimetype=request.mimetype,
)