openapi-core/openapi_core/operations.py

99 lines
3.4 KiB
Python
Raw Normal View History

2017-09-21 11:51:37 +00:00
# -*- coding: utf-8 -*-
"""OpenAPI core operations module"""
import logging
2017-09-22 08:54:37 +00:00
from functools import lru_cache
2017-09-21 11:51:37 +00:00
from six import iteritems
from openapi_spec_validator.validators import PathItemValidator
2017-09-21 11:51:37 +00:00
2017-11-06 11:00:13 +00:00
from openapi_core.exceptions import InvalidResponse
2017-09-21 11:51:37 +00:00
from openapi_core.parameters import ParametersGenerator
from openapi_core.request_bodies import RequestBodyFactory
2017-11-06 11:00:13 +00:00
from openapi_core.responses import ResponsesGenerator
2017-09-21 11:51:37 +00:00
log = logging.getLogger(__name__)
class Operation(object):
"""Represents an OpenAPI Operation."""
def __init__(
2017-11-06 11:00:13 +00:00
self, http_method, path_name, responses, parameters,
request_body=None, deprecated=False, operation_id=None):
2017-09-21 11:51:37 +00:00
self.http_method = http_method
self.path_name = path_name
2017-11-06 11:00:13 +00:00
self.responses = dict(responses)
2017-09-21 11:51:37 +00:00
self.parameters = dict(parameters)
self.request_body = request_body
self.deprecated = deprecated
self.operation_id = operation_id
def __getitem__(self, name):
return self.parameters[name]
2017-11-06 11:00:13 +00:00
def get_response(self, http_status='default'):
try:
return self.responses[http_status]
except KeyError:
2017-11-06 11:19:18 +00:00
# try range
http_status_range = '{0}XX'.format(http_status[0])
if http_status_range in self.responses:
return self.responses[http_status_range]
2017-11-06 11:00:13 +00:00
if 'default' not in self.responses:
raise InvalidResponse(
"Unknown response http status {0}".format(http_status))
return self.responses['default']
2017-09-21 11:51:37 +00:00
class OperationsGenerator(object):
"""Represents an OpenAPI Operation in a service."""
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 generate(self, path_name, path):
path_deref = self.dereferencer.dereference(path)
for http_method, operation in iteritems(path_deref):
if http_method not in PathItemValidator.OPERATIONS:
2017-09-21 11:51:37 +00:00
continue
operation_deref = self.dereferencer.dereference(operation)
2017-11-06 11:00:13 +00:00
responses_spec = operation_deref['responses']
responses = self.responses_generator.generate(responses_spec)
2017-09-21 11:51:37 +00:00
deprecated = operation_deref.get('deprecated', False)
parameters_list = operation_deref.get('parameters', [])
2017-11-06 11:00:13 +00:00
parameters = self.parameters_generator.generate_from_list(
parameters_list)
2017-09-21 11:51:37 +00:00
request_body = None
if 'requestBody' in operation_deref:
request_body_spec = operation_deref.get('requestBody')
2017-09-22 08:54:37 +00:00
request_body = self.request_body_factory.create(
request_body_spec)
2017-09-21 11:51:37 +00:00
yield (
http_method,
Operation(
2017-11-06 11:00:13 +00:00
http_method, path_name, responses, list(parameters),
2017-09-21 11:51:37 +00:00
request_body=request_body, deprecated=deprecated,
),
)
2017-11-06 11:00:13 +00:00
@property
@lru_cache()
def responses_generator(self):
return ResponsesGenerator(self.dereferencer, self.schemas_registry)
2017-09-22 08:54:37 +00:00
@property
@lru_cache()
def parameters_generator(self):
return ParametersGenerator(self.dereferencer, self.schemas_registry)
2017-09-21 11:51:37 +00:00
2017-09-22 08:54:37 +00:00
@property
@lru_cache()
def request_body_factory(self):
return RequestBodyFactory(self.dereferencer, self.schemas_registry)