2019-10-19 11:01:50 +00:00
|
|
|
"""OpenAPI core contrib flask requests module"""
|
2019-06-11 22:34:48 +00:00
|
|
|
import re
|
|
|
|
|
2019-10-19 11:01:50 +00:00
|
|
|
from openapi_core.wrappers.base import BaseOpenAPIRequest
|
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
|
|
|
|
|
|
|
class FlaskOpenAPIRequest(BaseOpenAPIRequest):
|
|
|
|
|
2019-06-11 22:34:48 +00:00
|
|
|
path_regex = re.compile(PATH_PARAMETER_PATTERN)
|
|
|
|
|
2018-04-17 14:37:52 +00:00
|
|
|
def __init__(self, request):
|
|
|
|
self.request = request
|
|
|
|
|
|
|
|
@property
|
|
|
|
def host_url(self):
|
|
|
|
return self.request.host_url
|
|
|
|
|
|
|
|
@property
|
|
|
|
def path(self):
|
|
|
|
return self.request.path
|
|
|
|
|
|
|
|
@property
|
|
|
|
def method(self):
|
|
|
|
return self.request.method.lower()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def path_pattern(self):
|
|
|
|
if self.request.url_rule is None:
|
|
|
|
return self.path
|
|
|
|
|
2019-06-11 22:34:48 +00:00
|
|
|
return self.path_regex.sub(r'{\1}', self.request.url_rule.rule)
|
2018-04-17 14:37:52 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def parameters(self):
|
|
|
|
return {
|
|
|
|
'path': self.request.view_args,
|
|
|
|
'query': self.request.args,
|
2018-07-09 11:10:05 +00:00
|
|
|
'header': self.request.headers,
|
|
|
|
'cookie': self.request.cookies,
|
2018-04-17 14:37:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def body(self):
|
|
|
|
return self.request.data
|
|
|
|
|
|
|
|
@property
|
|
|
|
def mimetype(self):
|
|
|
|
return self.request.mimetype
|