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

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

View File

@@ -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']

View File

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

View File

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

View File

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

View File

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

View File

@@ -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']

View File

@@ -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__':