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

49 lines
1.3 KiB
Python
Raw Normal View History

2018-04-17 12:18:40 +00:00
"""OpenAPI core servers models module"""
from six import iteritems
2020-02-21 16:33:45 +00:00
from six.moves.urllib.parse import urljoin
2018-04-17 12:18:40 +00:00
class Server(object):
2020-03-03 13:01:45 +00:00
def __init__(self, url, variables=None, description=None, extensions=None):
2018-04-17 12:18:40 +00:00
self.url = url
self.variables = variables and dict(variables) or {}
2019-10-22 17:21:40 +00:00
self.description = description
2018-04-17 12:18:40 +00:00
2020-03-03 13:01:45 +00:00
self.extensions = extensions and dict(extensions) or {}
2018-04-17 12:18:40 +00:00
@property
def default_url(self):
return self.get_url()
@property
def default_variables(self):
defaults = {}
for name, variable in iteritems(self.variables):
defaults[name] = variable.default
return defaults
def get_url(self, **variables):
if not variables:
variables = self.default_variables
return self.url.format(**variables)
2020-02-21 16:33:45 +00:00
@staticmethod
def is_absolute(url):
return url.startswith('//') or '://' in url
def get_absolute_url(self, base_url=None):
if base_url is not None and not self.is_absolute(self.url):
return urljoin(base_url, self.url)
return self.url
2018-04-17 12:18:40 +00:00
class ServerVariable(object):
2020-03-03 13:01:45 +00:00
def __init__(self, name, default, enum=None, extensions=None):
2018-04-17 12:18:40 +00:00
self.name = name
self.default = default
self.enum = enum and list(enum) or []
2020-03-03 13:01:45 +00:00
self.extensions = extensions and dict(extensions) or {}