openapi-core/openapi_core/schema/responses/models.py

31 lines
1.1 KiB
Python
Raw Normal View History

2018-04-17 12:18:40 +00:00
"""OpenAPI core responses models module"""
2018-05-30 13:46:54 +00:00
from openapi_core.schema.content.exceptions import MimeTypeNotFound
from openapi_core.schema.content.models import Content
2018-04-18 10:39:03 +00:00
from openapi_core.schema.media_types.exceptions import InvalidContentType
from openapi_core.schema.responses.exceptions import MissingResponseContent
2018-04-17 12:18:40 +00:00
class Response(object):
def __init__(
self, http_status, description, headers=None, content=None,
links=None):
self.http_status = http_status
self.description = description
self.headers = headers and dict(headers) or {}
2018-05-30 13:46:54 +00:00
self.content = content and Content(content) or Content()
2018-04-17 12:18:40 +00:00
self.links = links and dict(links) or {}
def __getitem__(self, mimetype):
try:
return self.content[mimetype]
2018-05-30 13:46:54 +00:00
except MimeTypeNotFound:
2018-04-17 12:18:40 +00:00
raise InvalidContentType(
"Invalid mime type `{0}`".format(mimetype))
2018-04-18 10:39:03 +00:00
def get_value(self, response):
if not response.data:
raise MissingResponseContent("Missing response content")
return response.data