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:
William Cheng
2019-01-28 11:24:48 +08:00
committed by GitHub
parent 887b688014
commit 7811390b7b
188 changed files with 17669 additions and 73 deletions

View File

@@ -410,9 +410,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint
# Configure HTTP basic authorization: http_basic_test
configuration = petstore_api.Configuration()
# Configure HTTP basic authorization: http_basic_test
configuration.username = 'YOUR_USERNAME'
configuration.password = 'YOUR_PASSWORD'

View File

@@ -23,9 +23,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint
# Configure API key authorization: api_key_query
configuration = petstore_api.Configuration()
# Configure API key authorization: api_key_query
configuration.api_key['api_key_query'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['api_key_query'] = 'Bearer'

View File

@@ -29,9 +29,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint
# Configure OAuth2 access token for authorization: petstore_auth
configuration = petstore_api.Configuration()
# Configure OAuth2 access token for authorization: petstore_auth
configuration.access_token = 'YOUR_ACCESS_TOKEN'
# create an instance of the API class
@@ -80,9 +79,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint
# Configure OAuth2 access token for authorization: petstore_auth
configuration = petstore_api.Configuration()
# Configure OAuth2 access token for authorization: petstore_auth
configuration.access_token = 'YOUR_ACCESS_TOKEN'
# create an instance of the API class
@@ -135,9 +133,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint
# Configure OAuth2 access token for authorization: petstore_auth
configuration = petstore_api.Configuration()
# Configure OAuth2 access token for authorization: petstore_auth
configuration.access_token = 'YOUR_ACCESS_TOKEN'
# create an instance of the API class
@@ -189,9 +186,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint
# Configure OAuth2 access token for authorization: petstore_auth
configuration = petstore_api.Configuration()
# Configure OAuth2 access token for authorization: petstore_auth
configuration.access_token = 'YOUR_ACCESS_TOKEN'
# create an instance of the API class
@@ -243,9 +239,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint
# Configure API key authorization: api_key
configuration = petstore_api.Configuration()
# Configure API key authorization: api_key
configuration.api_key['api_key'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['api_key'] = 'Bearer'
@@ -297,9 +292,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint
# Configure OAuth2 access token for authorization: petstore_auth
configuration = petstore_api.Configuration()
# Configure OAuth2 access token for authorization: petstore_auth
configuration.access_token = 'YOUR_ACCESS_TOKEN'
# create an instance of the API class
@@ -348,9 +342,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint
# Configure OAuth2 access token for authorization: petstore_auth
configuration = petstore_api.Configuration()
# Configure OAuth2 access token for authorization: petstore_auth
configuration.access_token = 'YOUR_ACCESS_TOKEN'
# create an instance of the API class
@@ -403,9 +396,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint
# Configure OAuth2 access token for authorization: petstore_auth
configuration = petstore_api.Configuration()
# Configure OAuth2 access token for authorization: petstore_auth
configuration.access_token = 'YOUR_ACCESS_TOKEN'
# create an instance of the API class
@@ -459,9 +451,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint
# Configure OAuth2 access token for authorization: petstore_auth
configuration = petstore_api.Configuration()
# Configure OAuth2 access token for authorization: petstore_auth
configuration.access_token = 'YOUR_ACCESS_TOKEN'
# create an instance of the API class

View File

@@ -73,9 +73,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint
# Configure API key authorization: api_key
configuration = petstore_api.Configuration()
# Configure API key authorization: api_key
configuration.api_key['api_key'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['api_key'] = 'Bearer'

View File

@@ -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