forked from loafle/openapi-generator-original
Add multiple servers support to Python client (#1969)
* add multiple server support to python client * various fixes * minor fixes, add tests * test oas2 python first * fix tests * fix issues reported by flake8 * update code format * add python petstore to ensure up-to-date * rearrange test * fix E501 * fix tests * add new files * fix script permission * fix index check * update samples
This commit is contained in:
@@ -258,3 +258,54 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
|
||||
"Version of the API: 1.0.0\n"\
|
||||
"SDK Package Version: 1.0.0".\
|
||||
format(env=sys.platform, pyversion=sys.version)
|
||||
|
||||
def get_host_settings(self):
|
||||
"""Gets an array of host settings
|
||||
|
||||
:return: An array of host settings
|
||||
"""
|
||||
return [
|
||||
{
|
||||
'url': "http://petstore.swagger.io:80/v2",
|
||||
'description': "No description provided",
|
||||
}
|
||||
]
|
||||
|
||||
def get_host_from_settings(self, index, variables={}):
|
||||
"""Gets host URL based on the index and variables
|
||||
:param index: array index of the host settings
|
||||
:param variables: hash of variable and the corresponding value
|
||||
:return: URL based on host settings
|
||||
"""
|
||||
|
||||
servers = self.get_host_settings()
|
||||
|
||||
# check array index out of bound
|
||||
if index < 0 or index >= len(servers):
|
||||
raise ValueError(
|
||||
"Invalid index {} when selecting the host settings. Must be less than {}" # noqa: E501
|
||||
.format(index, len(servers)))
|
||||
|
||||
server = servers[index]
|
||||
url = server['url']
|
||||
|
||||
# go through variable and assign a value
|
||||
for variable_name in server['variables']:
|
||||
if variable_name in variables:
|
||||
if variables[variable_name] in server['variables'][
|
||||
variable_name]['enum_values']:
|
||||
url = url.replace("{" + variable_name + "}",
|
||||
variables[variable_name])
|
||||
else:
|
||||
raise ValueError(
|
||||
"The variable `{}` in the host URL has invalid value {}. Must be {}." # noqa: E501
|
||||
.format(
|
||||
variable_name, variables[variable_name],
|
||||
server['variables'][variable_name]['enum_values']))
|
||||
else:
|
||||
# use default value
|
||||
url = url.replace(
|
||||
"{" + variable_name + "}",
|
||||
server['variables'][variable_name]['default_value'])
|
||||
|
||||
return url
|
||||
|
||||
Reference in New Issue
Block a user