Updated python client.

* Rename `ResponseError` exception to `ApiException`
* Use `File.separatorChar` to build file path in
  PythonClientCodegen.java
* Rename `config.py` module to `configuration.py`
* Rename `swagger.py` module to `api_client.py`
This commit is contained in:
geekerzp 2015-06-02 18:14:46 +08:00
parent 51887c64b5
commit ab6f327f35
15 changed files with 35 additions and 37 deletions

View File

@ -28,16 +28,15 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
eggPackage = module + "-python"; eggPackage = module + "-python";
invokerPackage = eggPackage + "." + module; invokerPackage = eggPackage + File.separatorChar + module;
invokerPackage = invokerPackage.replace('.', File.separatorChar);
outputFolder = "generated-code/python"; outputFolder = "generated-code" + File.separatorChar + "python";
modelTemplateFiles.put("model.mustache", ".py"); modelTemplateFiles.put("model.mustache", ".py");
apiTemplateFiles.put("api.mustache", ".py"); apiTemplateFiles.put("api.mustache", ".py");
templateDir = "python"; templateDir = "python";
apiPackage = invokerPackage + ".apis"; apiPackage = invokerPackage + File.separatorChar + "apis";
modelPackage = invokerPackage + ".models"; modelPackage = invokerPackage + File.separatorChar + "models";
languageSpecificPrimitives.clear(); languageSpecificPrimitives.clear();
languageSpecificPrimitives.add("int"); languageSpecificPrimitives.add("int");
@ -70,13 +69,13 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
supportingFiles.add(new SupportingFile("README.mustache", eggPackage, "README.md")); supportingFiles.add(new SupportingFile("README.mustache", eggPackage, "README.md"));
supportingFiles.add(new SupportingFile("setup.mustache", eggPackage, "setup.py")); supportingFiles.add(new SupportingFile("setup.mustache", eggPackage, "setup.py"));
supportingFiles.add(new SupportingFile("swagger.mustache", invokerPackage, "swagger.py")); supportingFiles.add(new SupportingFile("api_client.mustache", invokerPackage, "api_client.py"));
supportingFiles.add(new SupportingFile("rest.mustache", invokerPackage, "rest.py")); supportingFiles.add(new SupportingFile("rest.mustache", invokerPackage, "rest.py"));
supportingFiles.add(new SupportingFile("util.mustache", invokerPackage, "util.py")); supportingFiles.add(new SupportingFile("util.mustache", invokerPackage, "util.py"));
supportingFiles.add(new SupportingFile("config.mustache", invokerPackage, "config.py")); supportingFiles.add(new SupportingFile("configuration.mustache", invokerPackage, "configuration.py"));
supportingFiles.add(new SupportingFile("__init__package.mustache", invokerPackage, "__init__.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__model.mustache", modelPackage, "__init__.py"));
supportingFiles.add(new SupportingFile("__init__api.mustache", apiPackage.replace('.', File.separatorChar), "__init__.py")); supportingFiles.add(new SupportingFile("__init__api.mustache", apiPackage, "__init__.py"));
} }
@Override @Override

View File

@ -7,4 +7,4 @@ from __future__ import absolute_import
{{#apiInfo}}{{#apis}}from .apis.{{classVarName}} import {{classname}} {{#apiInfo}}{{#apis}}from .apis.{{classVarName}} import {{classname}}
{{/apis}}{{/apiInfo}} {{/apis}}{{/apiInfo}}
# import ApiClient # import ApiClient
from .swagger import ApiClient from .api_client import ApiClient

View File

@ -29,7 +29,7 @@ from six import iteritems
from ..util import remove_none from ..util import remove_none
from .. import config from ..api_client import ApiClient
{{#operations}} {{#operations}}
class {{classname}}(object): class {{classname}}(object):

View File

@ -28,7 +28,7 @@ except ImportError:
# for python2 # for python2
from urllib import quote from urllib import quote
from . import config from . import configuration
class ApiClient(object): class ApiClient(object):
""" """
@ -38,7 +38,7 @@ class ApiClient(object):
:param header_name: a header to pass when making calls to the API :param header_name: a header to pass when making calls to the API
:param header_value: a header value to pass when making calls to the API :param header_value: a header value to pass when making calls to the API
""" """
def __init__(self, host=config.host, header_name=None, header_value=None): def __init__(self, host=configuration.host, header_name=None, header_value=None):
self.default_headers = {} self.default_headers = {}
if header_name is not None: if header_name is not None:
self.default_headers[header_name] = header_value self.default_headers[header_name] = header_value
@ -292,7 +292,7 @@ class ApiClient(object):
return return
for auth in auth_settings: for auth in auth_settings:
auth_setting = config.auth_settings().get(auth) auth_setting = configuration.auth_settings().get(auth)
if auth_setting: if auth_setting:
if auth_setting['in'] == 'header': if auth_setting['in'] == 'header':
headers[auth_setting['key']] = auth_setting['value'] headers[auth_setting['key']] = auth_setting['value']

View File

@ -120,7 +120,7 @@ class RESTClientObject(object):
r = RESTResponse(r) r = RESTResponse(r)
if r.status not in range(200, 206): if r.status not in range(200, 206):
raise ErrorResponse(r) raise ApiException(r)
return self.process_response(r) return self.process_response(r)
@ -157,7 +157,7 @@ class RESTClientObject(object):
return self.request("PATCH", url, headers=headers, post_params=post_params, body=body) return self.request("PATCH", url, headers=headers, post_params=post_params, body=body)
class ErrorResponse(Exception): class ApiException(Exception):
""" """
Non-2xx HTTP response Non-2xx HTTP response
""" """

View File

@ -13,4 +13,4 @@ from .apis.pet_api import PetApi
from .apis.store_api import StoreApi from .apis.store_api import StoreApi
# import ApiClient # import ApiClient
from .swagger import ApiClient from .api_client import ApiClient

View File

@ -28,7 +28,7 @@ except ImportError:
# for python2 # for python2
from urllib import quote from urllib import quote
from . import config from . import configuration
class ApiClient(object): class ApiClient(object):
""" """
@ -38,7 +38,7 @@ class ApiClient(object):
:param header_name: a header to pass when making calls to the API :param header_name: a header to pass when making calls to the API
:param header_value: a header value to pass when making calls to the API :param header_value: a header value to pass when making calls to the API
""" """
def __init__(self, host=config.host, header_name=None, header_value=None): def __init__(self, host=configuration.host, header_name=None, header_value=None):
self.default_headers = {} self.default_headers = {}
if header_name is not None: if header_name is not None:
self.default_headers[header_name] = header_value self.default_headers[header_name] = header_value
@ -292,7 +292,7 @@ class ApiClient(object):
return return
for auth in auth_settings: for auth in auth_settings:
auth_setting = config.auth_settings().get(auth) auth_setting = configuration.auth_settings().get(auth)
if auth_setting: if auth_setting:
if auth_setting['in'] == 'header': if auth_setting['in'] == 'header':
headers[auth_setting['key']] = auth_setting['value'] headers[auth_setting['key']] = auth_setting['value']

View File

@ -29,7 +29,7 @@ from six import iteritems
from ..util import remove_none from ..util import remove_none
from .. import config from ..api_client import ApiClient
class PetApi(object): class PetApi(object):

View File

@ -29,7 +29,7 @@ from six import iteritems
from ..util import remove_none from ..util import remove_none
from .. import config from ..api_client import ApiClient
class StoreApi(object): class StoreApi(object):

View File

@ -29,7 +29,7 @@ from six import iteritems
from ..util import remove_none from ..util import remove_none
from .. import config from ..api_client import ApiClient
class UserApi(object): class UserApi(object):

View File

@ -120,7 +120,7 @@ class RESTClientObject(object):
r = RESTResponse(r) r = RESTResponse(r)
if r.status not in range(200, 206): if r.status not in range(200, 206):
raise ErrorResponse(r) raise ApiException(r)
return self.process_response(r) return self.process_response(r)
@ -157,7 +157,7 @@ class RESTClientObject(object):
return self.request("PATCH", url, headers=headers, post_params=post_params, body=body) return self.request("PATCH", url, headers=headers, post_params=post_params, body=body)
class ErrorResponse(Exception): class ApiException(Exception):
""" """
Non-2xx HTTP response Non-2xx HTTP response
""" """

View File

@ -12,7 +12,7 @@ import time
import unittest import unittest
import SwaggerPetstore import SwaggerPetstore
import SwaggerPetstore.config import SwaggerPetstore.configuration
HOST = 'http://petstore.swagger.io/v2' HOST = 'http://petstore.swagger.io/v2'
@ -23,17 +23,17 @@ class ApiClientTests(unittest.TestCase):
self.api_client = SwaggerPetstore.ApiClient(HOST) self.api_client = SwaggerPetstore.ApiClient(HOST)
def test_configuratjion(self): def test_configuratjion(self):
SwaggerPetstore.config.api_key['api_key'] = '123456' SwaggerPetstore.configuration.api_key['api_key'] = '123456'
SwaggerPetstore.config.api_key_prefix['api_key'] = 'PREFIX' SwaggerPetstore.configuration.api_key_prefix['api_key'] = 'PREFIX'
SwaggerPetstore.config.username = 'test_username' SwaggerPetstore.configuration.username = 'test_username'
SwaggerPetstore.config.password = 'test_password' SwaggerPetstore.configuration.password = 'test_password'
header_params = {'test1': 'value1'} header_params = {'test1': 'value1'}
query_params = {'test2': 'value2'} query_params = {'test2': 'value2'}
auth_settings = ['api_key', 'unknown'] auth_settings = ['api_key', 'unknown']
# test prefix # test prefix
self.assertEqual('PREFIX', SwaggerPetstore.config.api_key_prefix['api_key']) self.assertEqual('PREFIX', SwaggerPetstore.configuration.api_key_prefix['api_key'])
# update parameters based on auth setting # update parameters based on auth setting
self.api_client.update_params_for_auth(header_params, query_params, auth_settings) self.api_client.update_params_for_auth(header_params, query_params, auth_settings)
@ -44,8 +44,8 @@ class ApiClientTests(unittest.TestCase):
self.assertEqual(query_params['test2'], 'value2') self.assertEqual(query_params['test2'], 'value2')
# test basic auth # test basic auth
self.assertEqual('test_username', SwaggerPetstore.config.username) self.assertEqual('test_username', SwaggerPetstore.configuration.username)
self.assertEqual('test_password', SwaggerPetstore.config.password) self.assertEqual('test_password', SwaggerPetstore.configuration.password)
def test_select_header_accept(self): def test_select_header_accept(self):
accepts = ['APPLICATION/JSON', 'APPLICATION/XML'] accepts = ['APPLICATION/JSON', 'APPLICATION/XML']

View File

@ -12,8 +12,7 @@ import time
import unittest import unittest
import SwaggerPetstore import SwaggerPetstore
from SwaggerPetstore.rest import ErrorResponse from SwaggerPetstore.rest import ApiException
from SwaggerPetstore import config
HOST = 'http://petstore.swagger.io/v2' HOST = 'http://petstore.swagger.io/v2'
@ -126,7 +125,7 @@ class PetApiTests(unittest.TestCase):
additional_metadata=additional_metadata, additional_metadata=additional_metadata,
file=self.foo file=self.foo
) )
except ErrorResponse as e: except ApiException as e:
self.fail("upload_file() raised {0} unexpectedly".format(type(e))) self.fail("upload_file() raised {0} unexpectedly".format(type(e)))
def test_delete_pet(self): def test_delete_pet(self):
@ -136,7 +135,7 @@ class PetApiTests(unittest.TestCase):
try: try:
self.pet_api.get_pet_by_id(pet_id=self.pet.id) self.pet_api.get_pet_by_id(pet_id=self.pet.id)
raise "expected an error" raise "expected an error"
except ErrorResponse as e: except ApiException as e:
self.assertEqual(404, e.status) self.assertEqual(404, e.status)
if __name__ == '__main__': if __name__ == '__main__':