mirror of
https://github.com/correl/openapi-core.git
synced 2024-11-29 19:19:56 +00:00
6bdd1a6756
Main motivation behind this change is to be able to catch exceptions as per raise_for_errors() helpers, but to inspect state of exceptions instead of just getting a rendered string. This allows rendering exceptions into JSON, for example.
56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""OpenAPI core specs models module"""
|
|
import logging
|
|
|
|
from openapi_core.compat import partialmethod
|
|
from openapi_core.schema.operations.exceptions import InvalidOperation
|
|
from openapi_core.schema.servers.exceptions import InvalidServer
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
class Spec(object):
|
|
"""Represents an OpenAPI Specification for a service."""
|
|
|
|
def __init__(self, info, paths, servers=None, components=None):
|
|
self.info = info
|
|
self.paths = paths and dict(paths)
|
|
self.servers = servers or []
|
|
self.components = components
|
|
|
|
def __getitem__(self, path_name):
|
|
return self.paths[path_name]
|
|
|
|
@property
|
|
def default_url(self):
|
|
return self.servers[0].default_url
|
|
|
|
def get_server(self, full_url_pattern):
|
|
for spec_server in self.servers:
|
|
if spec_server.default_url in full_url_pattern:
|
|
return spec_server
|
|
|
|
raise InvalidServer(full_url_pattern)
|
|
|
|
def get_server_url(self, index=0):
|
|
return self.servers[index].default_url
|
|
|
|
def get_operation(self, path_pattern, http_method):
|
|
try:
|
|
return self.paths[path_pattern].operations[http_method]
|
|
except KeyError:
|
|
raise InvalidOperation(path_pattern, http_method)
|
|
|
|
def get_schema(self, name):
|
|
return self.components.schemas[name]
|
|
|
|
# operations shortcuts
|
|
|
|
get = partialmethod(get_operation, http_method='get')
|
|
put = partialmethod(get_operation, http_method='put')
|
|
post = partialmethod(get_operation, http_method='post')
|
|
delete = partialmethod(get_operation, http_method='delete')
|
|
options = partialmethod(get_operation, http_method='options')
|
|
head = partialmethod(get_operation, http_method='head')
|
|
patch = partialmethod(get_operation, http_method='patch')
|