forked from loafle/openapi-generator-original
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:
parent
51887c64b5
commit
ab6f327f35
@ -28,16 +28,15 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
|
||||
eggPackage = module + "-python";
|
||||
|
||||
invokerPackage = eggPackage + "." + module;
|
||||
invokerPackage = invokerPackage.replace('.', File.separatorChar);
|
||||
invokerPackage = eggPackage + File.separatorChar + module;
|
||||
|
||||
outputFolder = "generated-code/python";
|
||||
outputFolder = "generated-code" + File.separatorChar + "python";
|
||||
modelTemplateFiles.put("model.mustache", ".py");
|
||||
apiTemplateFiles.put("api.mustache", ".py");
|
||||
templateDir = "python";
|
||||
|
||||
apiPackage = invokerPackage + ".apis";
|
||||
modelPackage = invokerPackage + ".models";
|
||||
apiPackage = invokerPackage + File.separatorChar + "apis";
|
||||
modelPackage = invokerPackage + File.separatorChar + "models";
|
||||
|
||||
languageSpecificPrimitives.clear();
|
||||
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("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("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__model.mustache", modelPackage.replace('.', File.separatorChar), "__init__.py"));
|
||||
supportingFiles.add(new SupportingFile("__init__api.mustache", apiPackage.replace('.', File.separatorChar), "__init__.py"));
|
||||
supportingFiles.add(new SupportingFile("__init__model.mustache", modelPackage, "__init__.py"));
|
||||
supportingFiles.add(new SupportingFile("__init__api.mustache", apiPackage, "__init__.py"));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -7,4 +7,4 @@ from __future__ import absolute_import
|
||||
{{#apiInfo}}{{#apis}}from .apis.{{classVarName}} import {{classname}}
|
||||
{{/apis}}{{/apiInfo}}
|
||||
# import ApiClient
|
||||
from .swagger import ApiClient
|
||||
from .api_client import ApiClient
|
||||
|
@ -29,7 +29,7 @@ from six import iteritems
|
||||
|
||||
from ..util import remove_none
|
||||
|
||||
from .. import config
|
||||
from ..api_client import ApiClient
|
||||
|
||||
{{#operations}}
|
||||
class {{classname}}(object):
|
||||
|
@ -28,7 +28,7 @@ except ImportError:
|
||||
# for python2
|
||||
from urllib import quote
|
||||
|
||||
from . import config
|
||||
from . import configuration
|
||||
|
||||
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_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 = {}
|
||||
if header_name is not None:
|
||||
self.default_headers[header_name] = header_value
|
||||
@ -292,7 +292,7 @@ class ApiClient(object):
|
||||
return
|
||||
|
||||
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['in'] == 'header':
|
||||
headers[auth_setting['key']] = auth_setting['value']
|
@ -120,7 +120,7 @@ class RESTClientObject(object):
|
||||
r = RESTResponse(r)
|
||||
|
||||
if r.status not in range(200, 206):
|
||||
raise ErrorResponse(r)
|
||||
raise ApiException(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)
|
||||
|
||||
|
||||
class ErrorResponse(Exception):
|
||||
class ApiException(Exception):
|
||||
"""
|
||||
Non-2xx HTTP response
|
||||
"""
|
||||
|
@ -13,4 +13,4 @@ from .apis.pet_api import PetApi
|
||||
from .apis.store_api import StoreApi
|
||||
|
||||
# import ApiClient
|
||||
from .swagger import ApiClient
|
||||
from .api_client import ApiClient
|
||||
|
@ -28,7 +28,7 @@ except ImportError:
|
||||
# for python2
|
||||
from urllib import quote
|
||||
|
||||
from . import config
|
||||
from . import configuration
|
||||
|
||||
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_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 = {}
|
||||
if header_name is not None:
|
||||
self.default_headers[header_name] = header_value
|
||||
@ -292,7 +292,7 @@ class ApiClient(object):
|
||||
return
|
||||
|
||||
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['in'] == 'header':
|
||||
headers[auth_setting['key']] = auth_setting['value']
|
@ -29,7 +29,7 @@ from six import iteritems
|
||||
|
||||
from ..util import remove_none
|
||||
|
||||
from .. import config
|
||||
from ..api_client import ApiClient
|
||||
|
||||
class PetApi(object):
|
||||
|
||||
|
@ -29,7 +29,7 @@ from six import iteritems
|
||||
|
||||
from ..util import remove_none
|
||||
|
||||
from .. import config
|
||||
from ..api_client import ApiClient
|
||||
|
||||
class StoreApi(object):
|
||||
|
||||
|
@ -29,7 +29,7 @@ from six import iteritems
|
||||
|
||||
from ..util import remove_none
|
||||
|
||||
from .. import config
|
||||
from ..api_client import ApiClient
|
||||
|
||||
class UserApi(object):
|
||||
|
||||
|
@ -120,7 +120,7 @@ class RESTClientObject(object):
|
||||
r = RESTResponse(r)
|
||||
|
||||
if r.status not in range(200, 206):
|
||||
raise ErrorResponse(r)
|
||||
raise ApiException(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)
|
||||
|
||||
|
||||
class ErrorResponse(Exception):
|
||||
class ApiException(Exception):
|
||||
"""
|
||||
Non-2xx HTTP response
|
||||
"""
|
||||
|
@ -12,7 +12,7 @@ import time
|
||||
import unittest
|
||||
|
||||
import SwaggerPetstore
|
||||
import SwaggerPetstore.config
|
||||
import SwaggerPetstore.configuration
|
||||
|
||||
HOST = 'http://petstore.swagger.io/v2'
|
||||
|
||||
@ -23,17 +23,17 @@ class ApiClientTests(unittest.TestCase):
|
||||
self.api_client = SwaggerPetstore.ApiClient(HOST)
|
||||
|
||||
def test_configuratjion(self):
|
||||
SwaggerPetstore.config.api_key['api_key'] = '123456'
|
||||
SwaggerPetstore.config.api_key_prefix['api_key'] = 'PREFIX'
|
||||
SwaggerPetstore.config.username = 'test_username'
|
||||
SwaggerPetstore.config.password = 'test_password'
|
||||
SwaggerPetstore.configuration.api_key['api_key'] = '123456'
|
||||
SwaggerPetstore.configuration.api_key_prefix['api_key'] = 'PREFIX'
|
||||
SwaggerPetstore.configuration.username = 'test_username'
|
||||
SwaggerPetstore.configuration.password = 'test_password'
|
||||
|
||||
header_params = {'test1': 'value1'}
|
||||
query_params = {'test2': 'value2'}
|
||||
auth_settings = ['api_key', 'unknown']
|
||||
|
||||
# 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
|
||||
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')
|
||||
|
||||
# test basic auth
|
||||
self.assertEqual('test_username', SwaggerPetstore.config.username)
|
||||
self.assertEqual('test_password', SwaggerPetstore.config.password)
|
||||
self.assertEqual('test_username', SwaggerPetstore.configuration.username)
|
||||
self.assertEqual('test_password', SwaggerPetstore.configuration.password)
|
||||
|
||||
def test_select_header_accept(self):
|
||||
accepts = ['APPLICATION/JSON', 'APPLICATION/XML']
|
||||
|
@ -12,8 +12,7 @@ import time
|
||||
import unittest
|
||||
|
||||
import SwaggerPetstore
|
||||
from SwaggerPetstore.rest import ErrorResponse
|
||||
from SwaggerPetstore import config
|
||||
from SwaggerPetstore.rest import ApiException
|
||||
|
||||
HOST = 'http://petstore.swagger.io/v2'
|
||||
|
||||
@ -126,7 +125,7 @@ class PetApiTests(unittest.TestCase):
|
||||
additional_metadata=additional_metadata,
|
||||
file=self.foo
|
||||
)
|
||||
except ErrorResponse as e:
|
||||
except ApiException as e:
|
||||
self.fail("upload_file() raised {0} unexpectedly".format(type(e)))
|
||||
|
||||
def test_delete_pet(self):
|
||||
@ -136,7 +135,7 @@ class PetApiTests(unittest.TestCase):
|
||||
try:
|
||||
self.pet_api.get_pet_by_id(pet_id=self.pet.id)
|
||||
raise "expected an error"
|
||||
except ErrorResponse as e:
|
||||
except ApiException as e:
|
||||
self.assertEqual(404, e.status)
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
Loading…
x
Reference in New Issue
Block a user