Move value getters out of schema models

This commit is contained in:
Artur Maciag 2020-02-03 11:29:01 +00:00
parent faa8164f9a
commit 7b87cf9019
5 changed files with 30 additions and 41 deletions

View file

@ -72,22 +72,6 @@ class Parameter(object):
deserializer = self.get_dererializer()
return deserializer(value)
def get_raw_value(self, request):
location = request.parameters[self.location.value]
if self.name not in location:
if self.required:
raise MissingRequiredParameter(self.name)
raise MissingParameter(self.name)
if self.aslist and self.explode:
if hasattr(location, 'getall'):
return location.getall(self.name)
return location.getlist(self.name)
return location[self.name]
def deserialise(self, value):
if self.deprecated:
warnings.warn(
@ -103,12 +87,3 @@ class Parameter(object):
return self.deserialize(value)
except (ValueError, AttributeError) as exc:
raise InvalidParameterValue(self.name, exc)
def cast(self, value):
if not self.schema:
return value
try:
return self.schema.cast(value)
except CastError as exc:
raise InvalidParameterValue(self.name, exc)

View file

@ -2,7 +2,6 @@
from openapi_core.schema.content.exceptions import MimeTypeNotFound
from openapi_core.schema.content.models import Content
from openapi_core.schema.media_types.exceptions import InvalidContentType
from openapi_core.schema.request_bodies.exceptions import MissingRequestBody
class RequestBody(object):
@ -17,8 +16,3 @@ class RequestBody(object):
return self.content[mimetype]
except MimeTypeNotFound:
raise InvalidContentType(mimetype)
def get_value(self, request):
if not request.body and self.required:
raise MissingRequestBody(request)
return request.body

View file

@ -2,7 +2,6 @@
from openapi_core.schema.content.exceptions import MimeTypeNotFound
from openapi_core.schema.content.models import Content
from openapi_core.schema.media_types.exceptions import InvalidContentType
from openapi_core.schema.responses.exceptions import MissingResponseContent
class Response(object):
@ -24,9 +23,3 @@ class Response(object):
return self.content[mimetype]
except MimeTypeNotFound:
raise InvalidContentType(mimetype)
def get_value(self, response):
if not response.data:
raise MissingResponseContent(response)
return response.data

View file

@ -100,7 +100,7 @@ class RequestValidator(object):
continue
seen.add((param_name, param.location.value))
try:
raw_value = param.get_raw_value(request)
raw_value = self._get_parameter_value(param, request)
except MissingRequiredParameter as exc:
errors.append(exc)
continue
@ -141,7 +141,7 @@ class RequestValidator(object):
return None, [exc, ]
try:
raw_body = operation.request_body.get_value(request)
raw_body = self._get_body_value(operation.request_body, request)
except MissingRequestBody as exc:
return None, [exc, ]
@ -162,6 +162,27 @@ class RequestValidator(object):
return body, []
def _get_parameter_value(self, param, request):
location = request.parameters[param.location.value]
if param.name not in location:
if param.required:
raise MissingRequiredParameter(param.name)
raise MissingParameter(param.name)
if param.aslist and param.explode:
if hasattr(location, 'getall'):
return location.getall(param.name)
return location.getlist(param.name)
return location[param.name]
def _get_body_value(self, request_body, request):
if not request.body and request_body.required:
raise MissingRequestBody(request)
return request.body
def _deserialise(self, param_or_media_type, value):
return param_or_media_type.deserialise(value)

View file

@ -74,7 +74,7 @@ class ResponseValidator(object):
return None, [exc, ]
try:
raw_data = operation_response.get_value(response)
raw_data = self._get_data_value(response)
except MissingResponseContent as exc:
return None, [exc, ]
@ -103,6 +103,12 @@ class ResponseValidator(object):
return headers, errors
def _get_data_value(self, response):
if not response.data:
raise MissingResponseContent(response)
return response.data
def _deserialise(self, param_or_media_type, value):
return param_or_media_type.deserialise(value)