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

39 lines
1.3 KiB
Python
Raw Normal View History

2018-04-17 12:18:40 +00:00
# -*- coding: utf-8 -*-
"""OpenAPI core operations models module"""
2018-04-18 10:39:03 +00:00
from openapi_core.schema.responses.exceptions import InvalidResponse
2018-04-17 12:18:40 +00:00
class Operation(object):
"""Represents an OpenAPI Operation."""
def __init__(
self, http_method, path_name, responses, parameters,
2018-08-31 14:57:53 +00:00
request_body=None, deprecated=False, operation_id=None, tags=None):
2018-04-17 12:18:40 +00:00
self.http_method = http_method
self.path_name = path_name
self.responses = dict(responses)
self.parameters = dict(parameters)
self.request_body = request_body
self.deprecated = deprecated
self.operation_id = operation_id
2018-08-31 14:57:53 +00:00
self.tags = tags
2018-04-17 12:18:40 +00:00
def __getitem__(self, name):
return self.parameters[name]
def get_response(self, http_status='default'):
2018-04-18 10:39:03 +00:00
# @todo: move to Responses object
2018-04-17 12:18:40 +00:00
try:
return self.responses[http_status]
except KeyError:
# try range
http_status_range = '{0}XX'.format(http_status[0])
if http_status_range in self.responses:
return self.responses[http_status_range]
if 'default' not in self.responses:
raise InvalidResponse(
"Unknown response http status {0}".format(http_status))
return self.responses['default']