diff --git a/modules/swagger-codegen/src/main/resources/python/api.mustache b/modules/swagger-codegen/src/main/resources/python/api.mustache index a91797d71b6..7153ee2b040 100644 --- a/modules/swagger-codegen/src/main/resources/python/api.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api.mustache @@ -27,6 +27,7 @@ import os # python 2 and python 3 compatibility library from six import iteritems +from .. import configuration from ..api_client import ApiClient {{#operations}} @@ -36,7 +37,10 @@ class {{classname}}(object): if api_client: self.api_client = api_client else: - self.api_client = config.api_client + if not configuration.api_client: + configuration.api_client = ApiClient('{{basePath}}') + self.api_client = configuration.api_client + {{#operation}} def {{nickname}}(self, {{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}**kwargs): """ diff --git a/modules/swagger-codegen/src/main/resources/python/api_client.mustache b/modules/swagger-codegen/src/main/resources/python/api_client.mustache index 75c6e336a07..b8cc4cc2a84 100644 --- a/modules/swagger-codegen/src/main/resources/python/api_client.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api_client.mustache @@ -67,7 +67,7 @@ class ApiClient(object): if self.cookie: header_params['Cookie'] = self.cookie if header_params: - header_params = ApiClient.sanitize_for_serialization(header_params) + header_params = self.sanitize_for_serialization(header_params) # path parameters if path_params: @@ -301,6 +301,3 @@ class ApiClient(object): querys[auth_setting['key']] = auth_setting['value'] else: raise ValueError('Authentication token must be in `query` or `header`') - - - diff --git a/modules/swagger-codegen/src/main/resources/python/configuration.mustache b/modules/swagger-codegen/src/main/resources/python/configuration.mustache index 928aac21edf..d3a7093a02a 100644 --- a/modules/swagger-codegen/src/main/resources/python/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/python/configuration.mustache @@ -1,7 +1,7 @@ +from __future__ import absolute_import import base64 import urllib3 - def get_api_key_with_prefix(key): global api_key global api_key_prefix @@ -35,11 +35,12 @@ def auth_settings(): {{/isBasic}}{{/authMethods}} } - - # Default Base url host = "{{basePath}}" +# Default api client +api_client = None + # Authentication settings api_key = {} diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py index 75c6e336a07..b8cc4cc2a84 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py @@ -67,7 +67,7 @@ class ApiClient(object): if self.cookie: header_params['Cookie'] = self.cookie if header_params: - header_params = ApiClient.sanitize_for_serialization(header_params) + header_params = self.sanitize_for_serialization(header_params) # path parameters if path_params: @@ -301,6 +301,3 @@ class ApiClient(object): querys[auth_setting['key']] = auth_setting['value'] else: raise ValueError('Authentication token must be in `query` or `header`') - - - diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/pet_api.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/pet_api.py index ec0a8c0f5fd..549b7fe956f 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/pet_api.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/pet_api.py @@ -27,6 +27,7 @@ import os # python 2 and python 3 compatibility library from six import iteritems +from .. import configuration from ..api_client import ApiClient class PetApi(object): @@ -35,7 +36,10 @@ class PetApi(object): if api_client: self.api_client = api_client else: - self.api_client = config.api_client + if not configuration.api_client: + configuration.api_client = ApiClient('http://petstore.swagger.io/v2') + self.api_client = configuration.api_client + def update_pet(self, **kwargs): """ @@ -496,6 +500,3 @@ class PetApi(object): - - - diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/store_api.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/store_api.py index 03f0b2ec170..bd2fd9b8080 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/store_api.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/store_api.py @@ -27,6 +27,7 @@ import os # python 2 and python 3 compatibility library from six import iteritems +from .. import configuration from ..api_client import ApiClient class StoreApi(object): @@ -35,7 +36,10 @@ class StoreApi(object): if api_client: self.api_client = api_client else: - self.api_client = config.api_client + if not configuration.api_client: + configuration.api_client = ApiClient('http://petstore.swagger.io/v2') + self.api_client = configuration.api_client + def get_inventory(self, **kwargs): """ diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/user_api.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/user_api.py index 223566b8684..5aca93818d0 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/user_api.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/user_api.py @@ -27,6 +27,7 @@ import os # python 2 and python 3 compatibility library from six import iteritems +from .. import configuration from ..api_client import ApiClient class UserApi(object): @@ -35,7 +36,10 @@ class UserApi(object): if api_client: self.api_client = api_client else: - self.api_client = config.api_client + if not configuration.api_client: + configuration.api_client = ApiClient('http://petstore.swagger.io/v2') + self.api_client = configuration.api_client + def create_user(self, **kwargs): """ diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/configuration.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/configuration.py index 46e60ccf417..b5bd0a64e0a 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/configuration.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/configuration.py @@ -1,7 +1,7 @@ +from __future__ import absolute_import import base64 import urllib3 - def get_api_key_with_prefix(key): global api_key global api_key_prefix @@ -28,14 +28,17 @@ def auth_settings(): } - - # Default Base url host = "http://petstore.swagger.io/v2" +# Default api client +api_client = None + # Authentication settings api_key = {} api_key_prefix = {} username = '' password = '' + + diff --git a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_pet_api.py b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_pet_api.py index 2c50ac861a1..3bf18607729 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_pet_api.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_pet_api.py @@ -61,11 +61,11 @@ class PetApiTests(unittest.TestCase): # same default api client self.assertEqual(pet_api.api_client, pet_api2.api_client) # confirm using the default api client in the config module - self.assertEqual(pet_api.api_client, config.api_client) + self.assertEqual(pet_api.api_client, SwaggerPetstore.configuration.api_client) # 2 different api clients are not the same self.assertNotEqual(api_client3, api_client4) # customized pet api not using the default api client - self.assertNotEqual(pet_api3.api_client, config.api_client) + self.assertNotEqual(pet_api3.api_client, SwaggerPetstore.configuration.api_client) # customized pet api not using the old pet api's api client self.assertNotEqual(pet_api3.api_client, pet_api2.api_client)