2017-09-22 08:54:37 +00:00
|
|
|
from functools import lru_cache
|
|
|
|
|
2017-09-22 08:14:07 +00:00
|
|
|
from openapi_core.schemas import SchemasGenerator
|
|
|
|
|
|
|
|
|
|
|
|
class Components(object):
|
|
|
|
"""Represents an OpenAPI Components in a service."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, schemas=None, responses=None, parameters=None,
|
|
|
|
request_bodies=None):
|
|
|
|
self.schemas = schemas and dict(schemas) or {}
|
|
|
|
self.responses = responses and dict(responses) or {}
|
|
|
|
self.parameters = parameters and dict(parameters) or {}
|
|
|
|
self.request_bodies = request_bodies and dict(request_bodies) or {}
|
|
|
|
|
|
|
|
|
|
|
|
class ComponentsFactory(object):
|
|
|
|
|
2017-09-22 08:54:37 +00:00
|
|
|
def __init__(self, dereferencer, schemas_registry):
|
2017-09-22 08:14:07 +00:00
|
|
|
self.dereferencer = dereferencer
|
2017-09-22 08:54:37 +00:00
|
|
|
self.schemas_registry = schemas_registry
|
2017-09-22 08:14:07 +00:00
|
|
|
|
|
|
|
def create(self, components_spec):
|
|
|
|
components_deref = self.dereferencer.dereference(components_spec)
|
|
|
|
|
2018-02-09 16:37:57 +00:00
|
|
|
schemas_spec = components_deref.get('schemas', {})
|
|
|
|
responses_spec = components_deref.get('responses', {})
|
|
|
|
parameters_spec = components_deref.get('parameters', {})
|
|
|
|
request_bodies_spec = components_deref.get('request_bodies', {})
|
2017-09-22 08:14:07 +00:00
|
|
|
|
2017-09-22 08:54:37 +00:00
|
|
|
schemas = self.schemas_generator.generate(schemas_spec)
|
2017-09-22 08:14:07 +00:00
|
|
|
responses = self._generate_response(responses_spec)
|
|
|
|
parameters = self._generate_parameters(parameters_spec)
|
|
|
|
request_bodies = self._generate_request_bodies(request_bodies_spec)
|
|
|
|
return Components(
|
|
|
|
schemas=list(schemas), responses=responses, parameters=parameters,
|
|
|
|
request_bodies=request_bodies,
|
|
|
|
)
|
|
|
|
|
2017-09-22 08:54:37 +00:00
|
|
|
@property
|
|
|
|
@lru_cache()
|
|
|
|
def schemas_generator(self):
|
|
|
|
return SchemasGenerator(self.dereferencer, self.schemas_registry)
|
2017-09-22 08:14:07 +00:00
|
|
|
|
|
|
|
def _generate_response(self, responses_spec):
|
|
|
|
return responses_spec
|
|
|
|
|
|
|
|
def _generate_parameters(self, parameters_spec):
|
|
|
|
return parameters_spec
|
|
|
|
|
|
|
|
def _generate_request_bodies(self, request_bodies_spec):
|
|
|
|
return request_bodies_spec
|