diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PythonClientCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PythonClientCodegen.java index 7896bf66878..590e17f6326 100755 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PythonClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PythonClientCodegen.java @@ -71,6 +71,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig supportingFiles.add(new SupportingFile("swagger.mustache", invokerPackage, "swagger.py")); supportingFiles.add(new SupportingFile("rest.mustache", invokerPackage, "rest.py")); supportingFiles.add(new SupportingFile("util.mustache", invokerPackage, "util.py")); + supportingFiles.add(new SupportingFile("config.mustache", invokerPackage, "config.py")); supportingFiles.add(new SupportingFile("__init__package.mustache", invokerPackage, "__init__.py")); supportingFiles.add(new SupportingFile("__init__model.mustache", modelPackage.replace('.', File.separatorChar), "__init__.py")); supportingFiles.add(new SupportingFile("__init__api.mustache", apiPackage.replace('.', File.separatorChar), "__init__.py")); diff --git a/modules/swagger-codegen/src/main/resources/python/api.mustache b/modules/swagger-codegen/src/main/resources/python/api.mustache index fc779a66653..2ec09d92681 100644 --- a/modules/swagger-codegen/src/main/resources/python/api.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api.mustache @@ -31,11 +31,16 @@ from ..util import remove_none from ..swagger import ApiClient +from .. import config + {{#operations}} class {{classname}}(object): - def __init__(self, api_client): - self.api_client = api_client + def __init__(self, api_client=None): + if api_client: + self.api_client = api_client + else: + self.api_client = config.api_client {{#operation}} def {{nickname}}(self, {{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}**kwargs): """ diff --git a/modules/swagger-codegen/src/main/resources/python/config.mustache b/modules/swagger-codegen/src/main/resources/python/config.mustache new file mode 100644 index 00000000000..e8c36aee77b --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/python/config.mustache @@ -0,0 +1,8 @@ +from __future__ import absolute_import + +from .swagger import ApiClient + +# Configuration variables + +api_client = ApiClient("{{basePath}}") + 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 a599aeaabc1..83b878f8f3f 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 @@ -31,10 +31,15 @@ from ..util import remove_none from ..swagger import ApiClient +from .. import config + class PetApi(object): - def __init__(self, api_client): - self.api_client = api_client + def __init__(self, api_client=None): + if api_client: + self.api_client = api_client + else: + self.api_client = config.api_client def update_pet(self, **kwargs): """ 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 7f9b852f7ca..ee3ed53b032 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 @@ -31,10 +31,15 @@ from ..util import remove_none from ..swagger import ApiClient +from .. import config + class StoreApi(object): - def __init__(self, api_client): - self.api_client = api_client + def __init__(self, api_client=None): + if api_client: + self.api_client = api_client + else: + self.api_client = config.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 2471970c436..3fffde5dfc1 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 @@ -31,10 +31,15 @@ from ..util import remove_none from ..swagger import ApiClient +from .. import config + class UserApi(object): - def __init__(self, api_client): - self.api_client = api_client + def __init__(self, api_client=None): + if api_client: + self.api_client = api_client + else: + self.api_client = config.api_client def create_user(self, **kwargs): """ diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/config.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/config.py new file mode 100644 index 00000000000..6e158eedd70 --- /dev/null +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/config.py @@ -0,0 +1,8 @@ +from __future__ import absolute_import + +from .swagger import ApiClient + +# Configuration variables + +api_client = ApiClient("http://petstore.swagger.io/v2") + 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 f0b8ab03d81..ee8c5dde14e 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 @@ -13,6 +13,7 @@ import unittest import SwaggerPetstore from SwaggerPetstore.rest import ErrorResponse +from SwaggerPetstore import config HOST = 'http://petstore.swagger.io/v2' @@ -49,6 +50,26 @@ class PetApiTests(unittest.TestCase): self.test_file_dir = os.path.realpath(self.test_file_dir) self.foo = os.path.join(self.test_file_dir, "foo.png") + def test_create_api_instance(self): + pet_api = SwaggerPetstore.PetApi() + pet_api2 = SwaggerPetstore.PetApi() + api_client3 = SwaggerPetstore.ApiClient() + api_client3.user_agent = 'api client 3' + api_client4 = SwaggerPetstore.ApiClient() + api_client4.user_agent = 'api client 4' + pet_api3 = SwaggerPetstore.PetApi(api_client3) + + # 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) + # 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) + # customized pet api not using the old pet api's api client + self.assertNotEqual(pet_api3.api_client, pet_api2.api_client) + def test_add_pet_and_get_pet_by_id(self): self.pet_api.add_pet(body=self.pet)