Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] Support for per-operation servers #6557

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
81e594d
Dynamic server support
jirikuncar Jun 5, 2020
4985f27
regenerated
jirikuncar Jun 5, 2020
1fd2e65
Apply suggestions from code review
jirikuncar Jun 5, 2020
6caf66f
regenerated
jirikuncar Jun 5, 2020
5eec729
Add ParameterizedServer feature to Python experimental
jirikuncar Jun 5, 2020
df1c324
Fix lookup of server variables
jirikuncar Jun 5, 2020
3920645
Add tests and change default value for servers
jirikuncar Jun 8, 2020
7420edb
Fix server variables
jirikuncar Jun 8, 2020
5422cfd
Return base path when index is None
jirikuncar Jun 8, 2020
280837f
Merge remote-tracking branch 'upstream/master' into python-experiment…
jirikuncar Jun 8, 2020
294d164
Use HOST
jirikuncar Jun 8, 2020
f38fc72
Apply suggestions from code review
jirikuncar Jun 11, 2020
8ec2cb7
Apply suggestions from code review
jirikuncar Jun 11, 2020
655ecd0
Merge remote-tracking branch 'upstream/master' into python-experiment…
jirikuncar Jun 11, 2020
6e97360
regenerated
jirikuncar Jun 11, 2020
0a57b4c
Add specific tests for dynamic servers
jirikuncar Jun 12, 2020
5788068
Merge remote-tracking branch 'upstream/master' into python-experiment…
jirikuncar Jun 12, 2020
1d19097
regenerated
jirikuncar Jun 12, 2020
ff976a4
add docstring
jirikuncar Jun 14, 2020
032533d
Merge remote-tracking branch 'upstream/master' into python-experiment…
jirikuncar Jun 14, 2020
3706f4d
regenerated
jirikuncar Jun 15, 2020
3a1d0ca
Merge remote-tracking branch 'upstream/master' into python-experiment…
jirikuncar Jun 23, 2020
66df647
Fix wrong merge resolution
jirikuncar Jun 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/generators/python-experimental.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ sidebar_label: python-experimental
|Examples|✓|OAS2,OAS3
|XMLStructureDefinitions|✗|OAS2,OAS3
|MultiServer|✗|OAS3
|ParameterizedServer||OAS3
|ParameterizedServer||OAS3
|ParameterStyling|✗|OAS3
|Callbacks|✗|OAS3
|LinkObjects|✗|OAS3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public PythonClientExperimentalCodegen() {
SecurityFeature.ApiKey,
SecurityFeature.OAuth2_Implicit
))
.includeGlobalFeatures(
GlobalFeature.ParameterizedServer
)
.excludeGlobalFeatures(
GlobalFeature.XMLStructureDefinitions,
GlobalFeature.Callbacks,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,30 @@ conf = {{{packageName}}}.Configuration(

_default = None

def __init__(self, host="{{{basePath}}}",
def __init__(self, host=None,
api_key=None, api_key_prefix=None,
username=None, password=None,
discard_unknown_keys=False,
disabled_client_side_validations="",
{{#hasHttpSignatureMethods}}
signing_info=None,
{{/hasHttpSignatureMethods}}
server_index=None, server_variables=None,
server_operation_index=None, server_operation_variables=None,
jirikuncar marked this conversation as resolved.
Show resolved Hide resolved
):
"""Constructor
"""
self.host = host
self._base_path = "{{{basePath}}}" if host is None else host
spacether marked this conversation as resolved.
Show resolved Hide resolved
"""Default Base url
"""
self.server_index = 0 if server_index is None and host is None else server_index
self.server_operation_index = server_operation_index or {}
"""Default server index
"""
self.server_variables = server_variables or {}
self.server_operation_variables = server_operation_variables or {}
"""Default server variables
"""
self.temp_folder_path = None
"""Temp file folder for downloading files
"""
Expand Down Expand Up @@ -561,14 +571,18 @@ conf = {{{packageName}}}.Configuration(
{{/servers}}
]

def get_host_from_settings(self, index, variables=None):
def get_host_from_settings(self, index, variables=None, servers=None):
"""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

This comment was marked as resolved.

jirikuncar marked this conversation as resolved.
Show resolved Hide resolved
:param servers: host settings for the endpoint
jirikuncar marked this conversation as resolved.
Show resolved Hide resolved
jirikuncar marked this conversation as resolved.
Show resolved Hide resolved
:return: URL based on host settings
"""
if index is None:
return self._base_path

variables = {} if variables is None else variables
servers = self.get_host_settings()
servers = self.get_host_settings() if servers is None else servers

try:
server = servers[index]
Expand All @@ -580,7 +594,7 @@ conf = {{{packageName}}}.Configuration(
url = server['url']

# go through variables and replace placeholders
for variable_name, variable in server['variables'].items():
for variable_name, variable in server.get('variables', {}).items():
used_value = variables.get(
variable_name, variable['default_value'])

Expand All @@ -595,3 +609,17 @@ conf = {{{packageName}}}.Configuration(
url = url.replace("{" + variable_name + "}", used_value)

return url

@property
def host(self):
"""Return generated host."""
if self.server_index is None:
return self._base_path
jirikuncar marked this conversation as resolved.
Show resolved Hide resolved

jirikuncar marked this conversation as resolved.
Show resolved Hide resolved
return self.get_host_from_settings(self.server_index, variables=self.server_variables)

@host.setter
def host(self, value):
"""Fix base path."""
self._base_path = value
self.server_index = None
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class {{classname}}(object):
Default is True.
_host_index (int): specifies the index of the server
jirikuncar marked this conversation as resolved.
Show resolved Hide resolved
that we want to use.
Default is 0.
Default is read from the configuration.
spacether marked this conversation as resolved.
Show resolved Hide resolved
async_req (bool): execute request asynchronously

Returns:
Expand All @@ -131,7 +131,7 @@ class {{classname}}(object):
kwargs['_check_return_type'] = kwargs.get(
'_check_return_type', True
)
kwargs['_host_index'] = kwargs.get('_host_index', 0)
kwargs['_host_index'] = kwargs.get('_host_index')
{{#requiredParams}}
kwargs['{{paramName}}'] = \
{{paramName}}
Expand Down Expand Up @@ -160,13 +160,37 @@ class {{classname}}(object):
{{#-first}}
'servers': [
{{/-first}}
'{{{url}}}'{{^-last}},{{/-last}}
{
'url': "{{{url}}}",
'description': "{{{description}}}{{^description}}No description provided{{/description}}",
{{#variables}}
{{#-first}}
'variables': {
{{/-first}}
'{{{name}}}': {
'description': "{{{description}}}{{^description}}No description provided{{/description}}",
'default_value': "{{{defaultValue}}}",
{{#enumValues}}
{{#-first}}
'enum_values': [
{{/-first}}
"{{{.}}}"{{^-last}},{{/-last}}
{{#-last}}
]
{{/-last}}
{{/enumValues}}
}{{^-last}},{{/-last}}
{{#-last}}
}
{{/-last}}
{{/variables}}
},
{{#-last}}
]
{{/-last}}
{{/servers}}
{{^servers}}
'servers': [],
'servers': None,
{{/servers}}
},
params_map={
Expand Down Expand Up @@ -348,7 +372,7 @@ class Endpoint(object):
self.openapi_types = root_map['openapi_types']
extra_types = {
'async_req': (bool,),
'_host_index': (int,),
'_host_index': (none_type, int),
'_preload_content': (bool,),
'_request_timeout': (none_type, int, (int,), [int]),
'_return_http_data_only': (bool,),
Expand Down Expand Up @@ -447,7 +471,15 @@ class Endpoint(object):
def call_with_http_info(self, **kwargs):

try:
_host = self.settings['servers'][kwargs['_host_index']]
index = self.api_client.configuration.server_operation_index.get(
self.settings['operation_id'], self.api_client.configuration.server_index
) if kwargs['_host_index'] is None else kwargs['_host_index']
server_variables = self.api_client.configuration.server_operation_variables.get(
self.settings['operation_id'], self.api_client.configuration.server_variables
)
_host = self.api_client.configuration.get_host_from_settings(
index, variables=server_variables, servers=self.settings['servers']
)
except IndexError:
if self.settings['servers']:
raise ApiValueError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,27 @@ class Configuration(object):

_default = None

def __init__(self, host="http://petstore.swagger.io:80/v2",
def __init__(self, host=None,
api_key=None, api_key_prefix=None,
username=None, password=None,
discard_unknown_keys=False,
disabled_client_side_validations="",
server_index=None, server_variables=None,
server_operation_index=None, server_operation_variables=None,
):
"""Constructor
"""
self.host = host
self._base_path = "http://petstore.swagger.io:80/v2" if host is None else host
"""Default Base url
"""
self.server_index = 0 if server_index is None and host is None else server_index
self.server_operation_index = server_operation_index or {}
"""Default server index
"""
self.server_variables = server_variables or {}
self.server_operation_variables = server_operation_variables or {}
"""Default server variables
"""
self.temp_folder_path = None
"""Temp file folder for downloading files
"""
Expand Down Expand Up @@ -432,14 +442,18 @@ def get_host_settings(self):
}
]

def get_host_from_settings(self, index, variables=None):
def get_host_from_settings(self, index, variables=None, servers=None):
"""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
:param servers: host settings for the endpoint
:return: URL based on host settings
"""
if index is None:
return self._base_path

variables = {} if variables is None else variables
servers = self.get_host_settings()
servers = self.get_host_settings() if servers is None else servers

try:
server = servers[index]
Expand All @@ -451,7 +465,7 @@ def get_host_from_settings(self, index, variables=None):
url = server['url']

# go through variables and replace placeholders
for variable_name, variable in server['variables'].items():
for variable_name, variable in server.get('variables', {}).items():
used_value = variables.get(
variable_name, variable['default_value'])

Expand All @@ -466,3 +480,17 @@ def get_host_from_settings(self, index, variables=None):
url = url.replace("{" + variable_name + "}", used_value)

return url

@property
def host(self):
"""Return generated host."""
if self.server_index is None:
return self._base_path

return self.get_host_from_settings(self.server_index, variables=self.server_variables)

@host.setter
def host(self, value):
"""Fix base path."""
self._base_path = value
self.server_index = None
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def __call_123_test_special_tags(
Default is True.
_host_index (int): specifies the index of the server
that we want to use.
Default is 0.
Default is read from the configuration.
async_req (bool): execute request asynchronously

Returns:
Expand All @@ -110,7 +110,7 @@ def __call_123_test_special_tags(
kwargs['_check_return_type'] = kwargs.get(
'_check_return_type', True
)
kwargs['_host_index'] = kwargs.get('_host_index', 0)
kwargs['_host_index'] = kwargs.get('_host_index')
kwargs['body'] = \
body
return self.call_with_http_info(**kwargs)
Expand All @@ -122,7 +122,7 @@ def __call_123_test_special_tags(
'endpoint_path': '/another-fake/dummy',
'operation_id': 'call_123_test_special_tags',
'http_method': 'PATCH',
'servers': [],
'servers': None,
},
params_map={
'all': [
Expand Down Expand Up @@ -221,7 +221,7 @@ def __init__(self, settings=None, params_map=None, root_map=None,
self.openapi_types = root_map['openapi_types']
extra_types = {
'async_req': (bool,),
'_host_index': (int,),
'_host_index': (none_type, int),
'_preload_content': (bool,),
'_request_timeout': (none_type, int, (int,), [int]),
'_return_http_data_only': (bool,),
Expand Down Expand Up @@ -320,7 +320,15 @@ def __call__(self, *args, **kwargs):
def call_with_http_info(self, **kwargs):

try:
_host = self.settings['servers'][kwargs['_host_index']]
index = self.api_client.configuration.server_operation_index.get(
self.settings['operation_id'], self.api_client.configuration.server_index
) if kwargs['_host_index'] is None else kwargs['_host_index']
server_variables = self.api_client.configuration.server_operation_variables.get(
self.settings['operation_id'], self.api_client.configuration.server_variables
)
_host = self.api_client.configuration.get_host_from_settings(
index, variables=server_variables, servers=self.settings['servers']
)
except IndexError:
if self.settings['servers']:
raise ApiValueError(
Expand Down
Loading