updated ApiClient of python sdk.

When create api instance, if we don't pass the `api_client` parameter,
then use the default api_client in `config` module.
This commit is contained in:
geekerzp 2015-05-25 14:31:55 +08:00
parent 095771d345
commit 3beeb6125e
8 changed files with 66 additions and 8 deletions

View File

@ -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"));

View File

@ -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):
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):
"""

View File

@ -0,0 +1,8 @@
from __future__ import absolute_import
from .swagger import ApiClient
# Configuration variables
api_client = ApiClient("{{basePath}}")

View File

@ -31,10 +31,15 @@ from ..util import remove_none
from ..swagger import ApiClient
from .. import config
class PetApi(object):
def __init__(self, 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):
"""

View File

@ -31,10 +31,15 @@ from ..util import remove_none
from ..swagger import ApiClient
from .. import config
class StoreApi(object):
def __init__(self, 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):
"""

View File

@ -31,10 +31,15 @@ from ..util import remove_none
from ..swagger import ApiClient
from .. import config
class UserApi(object):
def __init__(self, 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):
"""

View File

@ -0,0 +1,8 @@
from __future__ import absolute_import
from .swagger import ApiClient
# Configuration variables
api_client = ApiClient("http://petstore.swagger.io/v2")

View File

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