openapi-core/openapi_core/wrappers.py

143 lines
3.4 KiB
Python
Raw Normal View History

2017-09-21 11:51:37 +00:00
"""OpenAPI core wrappers module"""
2017-11-03 11:18:48 +00:00
import warnings
2017-09-25 11:22:55 +00:00
2017-11-03 11:18:48 +00:00
from six.moves.urllib.parse import urljoin
2017-11-14 16:05:03 +00:00
from werkzeug.datastructures import ImmutableMultiDict
2017-09-25 11:22:55 +00:00
2017-09-21 11:51:37 +00:00
2017-11-03 11:18:48 +00:00
class BaseOpenAPIRequest(object):
2017-09-21 11:51:37 +00:00
2017-11-03 11:18:48 +00:00
host_url = NotImplemented
path = NotImplemented
path_pattern = NotImplemented
method = NotImplemented
2017-11-03 11:18:48 +00:00
parameters = NotImplemented
body = NotImplemented
2017-11-03 11:18:48 +00:00
mimetype = NotImplemented
2017-09-21 11:51:37 +00:00
2017-11-03 11:18:48 +00:00
@property
def full_url_pattern(self):
return urljoin(self.host_url, self.path_pattern)
2017-09-21 11:51:37 +00:00
2017-11-03 11:18:48 +00:00
def get_body(self, spec):
warnings.warn(
"`get_body` method is deprecated. "
"Use RequestValidator instead.",
DeprecationWarning,
)
# backward compatibility
2017-11-03 15:11:50 +00:00
from openapi_core.shortcuts import validate_body
return validate_body(spec, self, wrapper_class=None)
2017-09-21 11:51:37 +00:00
2017-11-03 11:18:48 +00:00
def get_parameters(self, spec):
warnings.warn(
"`get_parameters` method is deprecated. "
"Use RequestValidator instead.",
DeprecationWarning,
)
# backward compatibility
2017-11-03 15:11:50 +00:00
from openapi_core.shortcuts import validate_parameters
return validate_parameters(spec, self, wrapper_class=None)
2017-09-21 11:51:37 +00:00
2017-11-03 11:18:48 +00:00
class MockRequest(BaseOpenAPIRequest):
2017-09-21 11:51:37 +00:00
2017-11-03 11:18:48 +00:00
def __init__(
self, host_url, method, path, path_pattern=None, args=None,
view_args=None, headers=None, cookies=None, data=None,
mimetype='application/json'):
self.host_url = host_url
self.path = path
self.path_pattern = path_pattern or path
self.method = method.lower()
2017-09-21 11:51:37 +00:00
2017-11-03 11:18:48 +00:00
self.parameters = {
'path': view_args or {},
2017-11-14 16:05:03 +00:00
'query': ImmutableMultiDict(args or []),
'header': headers or {},
'cookie': cookies or {},
2017-11-03 11:18:48 +00:00
}
2017-09-25 13:20:23 +00:00
2017-11-03 11:18:48 +00:00
self.body = data or ''
2017-09-21 11:51:37 +00:00
2017-11-03 11:18:48 +00:00
self.mimetype = mimetype
2017-09-21 11:51:37 +00:00
2017-11-03 14:17:38 +00:00
class FlaskOpenAPIRequest(BaseOpenAPIRequest):
2017-09-21 11:51:37 +00:00
2017-11-03 11:18:48 +00:00
def __init__(self, request):
self.request = request
2017-09-21 11:51:37 +00:00
2017-11-03 11:18:48 +00:00
@property
def host_url(self):
return self.request.host_url
2017-09-21 11:51:37 +00:00
2017-11-03 11:18:48 +00:00
@property
def path(self):
return self.request.path
2017-09-21 11:51:37 +00:00
2017-11-03 11:18:48 +00:00
@property
def method(self):
return self.request.method.lower()
2017-09-21 11:51:37 +00:00
2017-09-25 11:22:55 +00:00
@property
2017-11-03 11:18:48 +00:00
def path_pattern(self):
2017-11-03 14:17:38 +00:00
if self.request.url_rule is None:
return self.path
2017-11-03 11:18:48 +00:00
return self.request.url_rule.rule
2017-09-25 11:22:55 +00:00
2017-11-03 11:18:48 +00:00
@property
def parameters(self):
return {
2017-11-03 14:17:38 +00:00
'path': self.request.view_args,
'query': self.request.args,
'headers': self.request.headers,
'cookies': self.request.cookies,
2017-11-03 11:18:48 +00:00
}
2017-09-21 11:51:37 +00:00
2017-11-03 11:18:48 +00:00
@property
def body(self):
return self.request.data
@property
def mimetype(self):
return self.request.mimetype
2017-11-06 13:32:31 +00:00
class BaseOpenAPIResponse(object):
body = NotImplemented
2017-11-06 15:08:21 +00:00
status_code = NotImplemented
2017-11-06 13:32:31 +00:00
mimetype = NotImplemented
class MockResponse(BaseOpenAPIRequest):
2017-11-06 15:08:21 +00:00
def __init__(self, data, status_code=200, mimetype='application/json'):
2017-11-06 14:05:06 +00:00
self.data = data
2017-11-06 13:32:31 +00:00
2017-11-06 15:08:21 +00:00
self.status_code = status_code
2017-11-06 13:32:31 +00:00
self.mimetype = mimetype
class FlaskOpenAPIResponse(BaseOpenAPIResponse):
def __init__(self, response):
self.response = response
@property
2017-11-06 14:05:06 +00:00
def data(self):
return self.response.data
2017-11-06 13:32:31 +00:00
@property
2017-11-06 15:08:21 +00:00
def status_code(self):
return self.response._status_code
2017-11-06 13:32:31 +00:00
@property
def mimetype(self):
return self.response.mimetype