openapi-core/openapi_core/request_bodies.py

41 lines
1.3 KiB
Python
Raw Normal View History

2017-09-21 11:51:37 +00:00
"""OpenAPI core requestBodies module"""
2017-09-22 08:54:37 +00:00
from functools import lru_cache
2017-11-03 11:18:48 +00:00
from openapi_core.exceptions import InvalidContentType
2017-09-21 11:51:37 +00:00
from openapi_core.media_types import MediaTypeGenerator
class RequestBody(object):
"""Represents an OpenAPI RequestBody."""
def __init__(self, content, required=False):
self.content = dict(content)
self.required = required
2017-10-09 14:57:07 +00:00
def __getitem__(self, mimetype):
2017-11-02 16:05:25 +00:00
try:
return self.content[mimetype]
except KeyError:
2017-11-03 11:18:48 +00:00
raise InvalidContentType(
2017-11-02 16:05:25 +00:00
"Invalid mime type `{0}`".format(mimetype))
2017-09-21 11:51:37 +00:00
class RequestBodyFactory(object):
2017-09-22 08:54:37 +00:00
def __init__(self, dereferencer, schemas_registry):
2017-09-21 11:51:37 +00:00
self.dereferencer = dereferencer
2017-09-22 08:54:37 +00:00
self.schemas_registry = schemas_registry
2017-09-21 11:51:37 +00:00
def create(self, request_body_spec):
request_body_deref = self.dereferencer.dereference(
request_body_spec)
content = request_body_deref['content']
2017-09-22 08:54:37 +00:00
media_types = self.media_types_generator.generate(content)
2017-09-21 11:51:37 +00:00
required = request_body_deref.get('required', False)
return RequestBody(media_types, required=required)
2017-09-22 08:54:37 +00:00
@property
@lru_cache()
def media_types_generator(self):
return MediaTypeGenerator(self.dereferencer, self.schemas_registry)