From 0ca97e4b68444636d0233ffa97bdfa59eff64219 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Wed, 27 May 2015 11:06:52 +0800 Subject: [PATCH 01/14] added authentication for python client. Not completed --- .../src/main/resources/python/config.mustache | 10 ++------ .../main/resources/python/swagger.mustache | 23 ++++++++++++++++++- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/python/config.mustache b/modules/swagger-codegen/src/main/resources/python/config.mustache index e8c36aee77b..3063b4ff026 100644 --- a/modules/swagger-codegen/src/main/resources/python/config.mustache +++ b/modules/swagger-codegen/src/main/resources/python/config.mustache @@ -1,8 +1,2 @@ -from __future__ import absolute_import - -from .swagger import ApiClient - -# Configuration variables - -api_client = ApiClient("{{basePath}}") - +# Default Base url +HOST = "{{basePath}}" diff --git a/modules/swagger-codegen/src/main/resources/python/swagger.mustache b/modules/swagger-codegen/src/main/resources/python/swagger.mustache index 21430c469c8..b97d4ed515a 100644 --- a/modules/swagger-codegen/src/main/resources/python/swagger.mustache +++ b/modules/swagger-codegen/src/main/resources/python/swagger.mustache @@ -28,6 +28,7 @@ except ImportError: # for python2 from urllib import quote +from . import config class ApiClient(object): """ @@ -37,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=None, header_name=None, header_value=None): + def __init__(self, host=config.HOST, header_name=None, header_value=None): self.default_headers = {} if header_name is not None: self.default_headers[header_name] = header_value @@ -279,3 +280,23 @@ class ApiClient(object): return 'application/json' else: return content_types[0] + + def get_api_key_with_prefix(api_key): + """ + Get API key (with prefix if possible) + """ + if config.api_prefix[api_key]: + return config.api_prefix[api_key] + config.api_key[api_key] + else: + return config.api_key[api_key] + + def update_params_for_auth(headers, querys, auths): + """ + Update header and query params based on authentication setting + """ + auths_map = { + {{#authMethods}} + + {{/authMethods}} + } + From b09250ed333857f30c503fc6028d60371712a325 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Wed, 27 May 2015 17:07:43 +0800 Subject: [PATCH 02/14] added authentication for python client. completed. --- .../src/main/resources/python/api.mustache | 5 +- .../src/main/resources/python/config.mustache | 49 ++++++++++++++++++- .../main/resources/python/swagger.mustache | 36 +++++++------- .../SwaggerPetstore/apis/pet_api.py | 40 ++++++++++++--- .../SwaggerPetstore/apis/store_api.py | 20 ++++++-- .../SwaggerPetstore/apis/user_api.py | 40 ++++++++++++--- .../SwaggerPetstore/config.py | 40 +++++++++++++-- .../SwaggerPetstore/swagger.py | 27 +++++++++- .../tests/test_api_client.py | 26 ++++++++++ 9 files changed, 238 insertions(+), 45 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/python/api.mustache b/modules/swagger-codegen/src/main/resources/python/api.mustache index d5e24398f0e..55bfd9e82a4 100644 --- a/modules/swagger-codegen/src/main/resources/python/api.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api.mustache @@ -81,9 +81,12 @@ class {{classname}}(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type([{{#consumes}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}]) + # Authentication setting + auth_settings = [{{#authMethods}}'{{name}}'{{#hasMore}}, {{/hasMore}}{{/authMethods}}] + response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response={{#returnType}}'{{returnType}}'{{/returnType}}{{^returnType}}None{{/returnType}}) + response={{#returnType}}'{{returnType}}'{{/returnType}}{{^returnType}}None{{/returnType}}, auth_settings=auth_settings) {{#returnType}} return response {{/returnType}}{{/operation}} diff --git a/modules/swagger-codegen/src/main/resources/python/config.mustache b/modules/swagger-codegen/src/main/resources/python/config.mustache index 3063b4ff026..a6de7299cba 100644 --- a/modules/swagger-codegen/src/main/resources/python/config.mustache +++ b/modules/swagger-codegen/src/main/resources/python/config.mustache @@ -1,2 +1,49 @@ +import base64 + + +def get_api_key_with_prefix(key): + global api_key + global api_key_prefix + + if api_key_prefix[key]: + return api_key_prefix[key] + ' ' + api_key[key] + else: + return api_key[key] + +def get_basic_auth_token(): + global username + global password + + return base64.base64encode('Basic ' + username + password) + +def auth_settings(): + return { {{#authMethods}}{{#isApiKey}} + '{{name}}': { + 'type': 'api_key', + 'in': {{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}}, + 'key': '{{keyParamName}}', + 'value': get_api_key_with_prefix('{{keyParamName}}') + }, + {{/isApiKey}}{{#isBasic}} + '{{name}}': { + 'type': 'basic', + 'in': 'header', + 'key': 'Authorization', + 'value': get_basic_auth_token() + }, + {{/isBasic}}{{/authMethods}} + } + + + # Default Base url -HOST = "{{basePath}}" +host = "{{basePath}}" + +# Authentication settings + +api_key = {} +api_key_prefix = {} +username = '' +password = '' + + diff --git a/modules/swagger-codegen/src/main/resources/python/swagger.mustache b/modules/swagger-codegen/src/main/resources/python/swagger.mustache index b97d4ed515a..bd478820435 100644 --- a/modules/swagger-codegen/src/main/resources/python/swagger.mustache +++ b/modules/swagger-codegen/src/main/resources/python/swagger.mustache @@ -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=config.host, header_name=None, header_value=None): self.default_headers = {} if header_name is not None: self.default_headers[header_name] = header_value @@ -59,7 +59,7 @@ class ApiClient(object): self.default_headers[header_name] = header_value def call_api(self, resource_path, method, path_params=None, query_params=None, header_params=None, - body=None, post_params=None, files=None, response=None): + body=None, post_params=None, files=None, response=None, auth_settings=None): # headers parameters headers = self.default_headers.copy() @@ -86,6 +86,9 @@ class ApiClient(object): post_params = self.prepare_post_parameters(post_params, files) post_params = self.sanitize_for_serialization(post_params) + # auth setting + self.update_params_for_auth(header_params, query_params, auth_settings) + # body if body: body = self.sanitize_for_serialization(body) @@ -281,22 +284,21 @@ class ApiClient(object): else: return content_types[0] - def get_api_key_with_prefix(api_key): - """ - Get API key (with prefix if possible) - """ - if config.api_prefix[api_key]: - return config.api_prefix[api_key] + config.api_key[api_key] - else: - return config.api_key[api_key] - - def update_params_for_auth(headers, querys, auths): + def update_params_for_auth(self, headers, querys, auth_settings): """ Update header and query params based on authentication setting """ - auths_map = { - {{#authMethods}} - - {{/authMethods}} - } + if not auth_settings: + return + + for auth in auth_settings: + auth_setting = config.auth_settings().get(auth) + if auth_setting: + if auth_setting['in'] == 'header': + headers[auth_setting['key']] = auth_setting['value'] + elif auth_setting['in'] == 'query': + querys[auth_setting['key']] = auth_setting['value'] + else: + raise ValueError('Authentication token must be in `query` or `header`') + 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 ad4bad57412..37da9901597 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 @@ -76,9 +76,12 @@ class PetApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type(['application/json', 'application/xml']) + # Authentication setting + auth_settings = ['petstore_auth'] + response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response=None) + response=None, auth_settings=auth_settings) def add_pet(self, **kwargs): """ @@ -117,9 +120,12 @@ class PetApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type(['application/json', 'application/xml']) + # Authentication setting + auth_settings = ['petstore_auth'] + response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response=None) + response=None, auth_settings=auth_settings) def find_pets_by_status(self, **kwargs): """ @@ -158,9 +164,12 @@ class PetApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type([]) + # Authentication setting + auth_settings = ['petstore_auth'] + response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response='list[Pet]') + response='list[Pet]', auth_settings=auth_settings) return response @@ -201,9 +210,12 @@ class PetApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type([]) + # Authentication setting + auth_settings = ['petstore_auth'] + response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response='list[Pet]') + response='list[Pet]', auth_settings=auth_settings) return response @@ -248,9 +260,12 @@ class PetApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type([]) + # Authentication setting + auth_settings = ['api_key', 'petstore_auth'] + response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response='Pet') + response='Pet', auth_settings=auth_settings) return response @@ -297,9 +312,12 @@ class PetApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type(['application/x-www-form-urlencoded']) + # Authentication setting + auth_settings = ['petstore_auth'] + response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response=None) + response=None, auth_settings=auth_settings) def delete_pet(self, pet_id, **kwargs): """ @@ -343,9 +361,12 @@ class PetApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type([]) + # Authentication setting + auth_settings = ['petstore_auth'] + response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response=None) + response=None, auth_settings=auth_settings) def upload_file(self, pet_id, **kwargs): """ @@ -390,9 +411,12 @@ class PetApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type(['multipart/form-data']) + # Authentication setting + auth_settings = ['petstore_auth'] + response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response=None) + response=None, auth_settings=auth_settings) 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 e973d3a6ba5..649cb5f704d 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 @@ -75,9 +75,12 @@ class StoreApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type([]) + # Authentication setting + auth_settings = ['api_key'] + response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response='map(String, int)') + response='map(String, int)', auth_settings=auth_settings) return response @@ -118,9 +121,12 @@ class StoreApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type([]) + # Authentication setting + auth_settings = [] + response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response='Order') + response='Order', auth_settings=auth_settings) return response @@ -165,9 +171,12 @@ class StoreApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type([]) + # Authentication setting + auth_settings = [] + response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response='Order') + response='Order', auth_settings=auth_settings) return response @@ -212,9 +221,12 @@ class StoreApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type([]) + # Authentication setting + auth_settings = [] + response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response=None) + response=None, auth_settings=auth_settings) 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 82874000d59..3f2b36a85fa 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 @@ -76,9 +76,12 @@ class UserApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type([]) + # Authentication setting + auth_settings = [] + response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response=None) + response=None, auth_settings=auth_settings) def create_users_with_array_input(self, **kwargs): """ @@ -117,9 +120,12 @@ class UserApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type([]) + # Authentication setting + auth_settings = [] + response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response=None) + response=None, auth_settings=auth_settings) def create_users_with_list_input(self, **kwargs): """ @@ -158,9 +164,12 @@ class UserApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type([]) + # Authentication setting + auth_settings = [] + response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response=None) + response=None, auth_settings=auth_settings) def login_user(self, **kwargs): """ @@ -200,9 +209,12 @@ class UserApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type([]) + # Authentication setting + auth_settings = [] + response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response='str') + response='str', auth_settings=auth_settings) return response @@ -242,9 +254,12 @@ class UserApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type([]) + # Authentication setting + auth_settings = [] + response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response=None) + response=None, auth_settings=auth_settings) def get_user_by_name(self, username, **kwargs): """ @@ -287,9 +302,12 @@ class UserApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type([]) + # Authentication setting + auth_settings = [] + response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response='User') + response='User', auth_settings=auth_settings) return response @@ -335,9 +353,12 @@ class UserApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type([]) + # Authentication setting + auth_settings = [] + response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response=None) + response=None, auth_settings=auth_settings) def delete_user(self, username, **kwargs): """ @@ -380,9 +401,12 @@ class UserApi(object): # HTTP header `Content-Type` header_params['Content-Type'] = self.api_client.select_header_content_type([]) + # Authentication setting + auth_settings = [] + response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, - response=None) + response=None, auth_settings=auth_settings) diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/config.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/config.py index 6e158eedd70..edcee6fe90b 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/config.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/config.py @@ -1,8 +1,40 @@ -from __future__ import absolute_import +import base64 -from .swagger import ApiClient -# Configuration variables +def get_api_key_with_prefix(key): + global api_key + global api_key_prefix -api_client = ApiClient("http://petstore.swagger.io/v2") + if api_key_prefix[key]: + return api_key_prefix[key] + ' ' + api_key[key] + else: + return api_key[key] +def get_basic_auth_token(): + global username + global password + + return base64.base64encode('Basic ' + username + password) + +def auth_settings(): + return { + 'api_key': { + 'type': 'api_key', + 'in': 'header', + 'key': 'api_key', + 'value': get_api_key_with_prefix('api_key') + }, + + } + + + +# Default Base url +host = "http://petstore.swagger.io/v2" + +# Authentication settings + +api_key = {} +api_key_prefix = {} +username = '' +password = '' diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py index 21430c469c8..bd478820435 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py @@ -28,6 +28,7 @@ except ImportError: # for python2 from urllib import quote +from . import config class ApiClient(object): """ @@ -37,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=None, header_name=None, header_value=None): + def __init__(self, host=config.host, header_name=None, header_value=None): self.default_headers = {} if header_name is not None: self.default_headers[header_name] = header_value @@ -58,7 +59,7 @@ class ApiClient(object): self.default_headers[header_name] = header_value def call_api(self, resource_path, method, path_params=None, query_params=None, header_params=None, - body=None, post_params=None, files=None, response=None): + body=None, post_params=None, files=None, response=None, auth_settings=None): # headers parameters headers = self.default_headers.copy() @@ -85,6 +86,9 @@ class ApiClient(object): post_params = self.prepare_post_parameters(post_params, files) post_params = self.sanitize_for_serialization(post_params) + # auth setting + self.update_params_for_auth(header_params, query_params, auth_settings) + # body if body: body = self.sanitize_for_serialization(body) @@ -279,3 +283,22 @@ class ApiClient(object): return 'application/json' else: return content_types[0] + + def update_params_for_auth(self, headers, querys, auth_settings): + """ + Update header and query params based on authentication setting + """ + if not auth_settings: + return + + for auth in auth_settings: + auth_setting = config.auth_settings().get(auth) + if auth_setting: + if auth_setting['in'] == 'header': + headers[auth_setting['key']] = auth_setting['value'] + elif auth_setting['in'] == 'query': + querys[auth_setting['key']] = auth_setting['value'] + else: + raise ValueError('Authentication token must be in `query` or `header`') + + diff --git a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py index 5243430a93a..6bc3000f267 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py @@ -12,6 +12,7 @@ import time import unittest import SwaggerPetstore +import SwaggerPetstore.config HOST = 'http://petstore.swagger.io/v2' @@ -21,6 +22,31 @@ class ApiClientTests(unittest.TestCase): def setUp(self): 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' + + 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']) + + # update parameters based on auth setting + self.api_client.update_params_for_auth(header_params, query_params, auth_settings) + + # test api key auth + self.assertEqual(header_params['test1'], 'value1') + self.assertEqual(header_params['api_key'], 'PREFIX 123456') + self.assertEqual(query_params['test2'], 'value2') + + # test basic auth + self.assertEqual('test_username', SwaggerPetstore.config.username) + self.assertEqual('test_password', SwaggerPetstore.config.password) + def test_select_header_accept(self): accepts = ['APPLICATION/JSON', 'APPLICATION/XML'] accept = self.api_client.select_header_accept(accepts) From d7aaaea0df842c9da2d7b3e8aca287c1aadde2c2 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Wed, 27 May 2015 18:37:34 +0800 Subject: [PATCH 03/14] updated config.py of python client. --- .../src/main/resources/python/config.mustache | 8 +++++--- .../src/main/resources/python/swagger.mustache | 2 +- .../SwaggerPetstore-python/SwaggerPetstore/config.py | 8 +++++--- .../SwaggerPetstore-python/SwaggerPetstore/swagger.py | 2 +- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/python/config.mustache b/modules/swagger-codegen/src/main/resources/python/config.mustache index a6de7299cba..5fb2db28329 100644 --- a/modules/swagger-codegen/src/main/resources/python/config.mustache +++ b/modules/swagger-codegen/src/main/resources/python/config.mustache @@ -1,20 +1,22 @@ import base64 +import urllib3 def get_api_key_with_prefix(key): global api_key global api_key_prefix - if api_key_prefix[key]: + if api_key.get(key) and api_key_prefix.get(key): return api_key_prefix[key] + ' ' + api_key[key] - else: + elif api_key.get(key): return api_key[key] def get_basic_auth_token(): global username global password - return base64.base64encode('Basic ' + username + password) + if username and password: + return urllib3.util.make_headers(basic_auth=username + ':' + password).get('authorization') def auth_settings(): return { {{#authMethods}}{{#isApiKey}} diff --git a/modules/swagger-codegen/src/main/resources/python/swagger.mustache b/modules/swagger-codegen/src/main/resources/python/swagger.mustache index bd478820435..f01192dd46b 100644 --- a/modules/swagger-codegen/src/main/resources/python/swagger.mustache +++ b/modules/swagger-codegen/src/main/resources/python/swagger.mustache @@ -87,7 +87,7 @@ class ApiClient(object): post_params = self.sanitize_for_serialization(post_params) # auth setting - self.update_params_for_auth(header_params, query_params, auth_settings) + self.update_params_for_auth(headers, query_params, auth_settings) # body if body: diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/config.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/config.py index edcee6fe90b..ec3dbe8efb4 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/config.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/config.py @@ -1,20 +1,22 @@ import base64 +import urllib3 def get_api_key_with_prefix(key): global api_key global api_key_prefix - if api_key_prefix[key]: + if api_key.get(key) and api_key_prefix.get(key): return api_key_prefix[key] + ' ' + api_key[key] - else: + elif api_key.get(key): return api_key[key] def get_basic_auth_token(): global username global password - return base64.base64encode('Basic ' + username + password) + if username and password: + return urllib3.util.make_headers(basic_auth=username + ':' + password).get('authorization') def auth_settings(): return { diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py index bd478820435..f01192dd46b 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py @@ -87,7 +87,7 @@ class ApiClient(object): post_params = self.sanitize_for_serialization(post_params) # auth setting - self.update_params_for_auth(header_params, query_params, auth_settings) + self.update_params_for_auth(headers, query_params, auth_settings) # body if body: From eac884d85b7053379a451d99e2d0a9bfdb5db9d8 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Wed, 27 May 2015 20:35:59 +0800 Subject: [PATCH 04/14] updated ApiClient of python sdk. --- .../src/main/resources/python/config.mustache | 3 +-- .../main/resources/python/swagger.mustache | 13 +++++---- .../python/SwaggerPetstore-python/.coverage | Bin 0 -> 3297 bytes .../SwaggerPetstore.egg-info/PKG-INFO | 12 +++++++++ .../SwaggerPetstore.egg-info/SOURCES.txt | 25 ++++++++++++++++++ .../dependency_links.txt | 1 + .../SwaggerPetstore.egg-info/requires.txt | 2 ++ .../SwaggerPetstore.egg-info/top_level.txt | 2 ++ .../dev-requirements.txt.log | 5 ++++ 9 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 samples/client/petstore/python/SwaggerPetstore-python/.coverage create mode 100644 samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/PKG-INFO create mode 100644 samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/SOURCES.txt create mode 100644 samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/dependency_links.txt create mode 100644 samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/requires.txt create mode 100644 samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/top_level.txt create mode 100644 samples/client/petstore/python/SwaggerPetstore-python/dev-requirements.txt.log diff --git a/modules/swagger-codegen/src/main/resources/python/config.mustache b/modules/swagger-codegen/src/main/resources/python/config.mustache index 5fb2db28329..928aac21edf 100644 --- a/modules/swagger-codegen/src/main/resources/python/config.mustache +++ b/modules/swagger-codegen/src/main/resources/python/config.mustache @@ -15,8 +15,7 @@ def get_basic_auth_token(): global username global password - if username and password: - return urllib3.util.make_headers(basic_auth=username + ':' + password).get('authorization') + return urllib3.util.make_headers(basic_auth=username + ':' + password).get('authorization') def auth_settings(): return { {{#authMethods}}{{#isApiKey}} diff --git a/modules/swagger-codegen/src/main/resources/python/swagger.mustache b/modules/swagger-codegen/src/main/resources/python/swagger.mustache index f01192dd46b..0483041116d 100644 --- a/modules/swagger-codegen/src/main/resources/python/swagger.mustache +++ b/modules/swagger-codegen/src/main/resources/python/swagger.mustache @@ -62,12 +62,11 @@ class ApiClient(object): body=None, post_params=None, files=None, response=None, auth_settings=None): # headers parameters - headers = self.default_headers.copy() - headers.update(header_params) + headers_params = self.default_headers.copy().update(header_params) if self.cookie: - headers['Cookie'] = self.cookie - if headers: - headers = self.sanitize_for_serialization(headers) + headers_params['Cookie'] = self.cookie + if headers_params: + headers_params = ApiClient.sanitize_for_serialization(headers_params) # path parameters if path_params: @@ -87,7 +86,7 @@ class ApiClient(object): post_params = self.sanitize_for_serialization(post_params) # auth setting - self.update_params_for_auth(headers, query_params, auth_settings) + self.update_params_for_auth(headers_params, query_params, auth_settings) # body if body: @@ -97,7 +96,7 @@ class ApiClient(object): url = self.host + resource_path # perform request and return response - response_data = self.request(method, url, query_params=query_params, headers=headers, + response_data = self.request(method, url, query_params=query_params, headers=headers_params, post_params=post_params, body=body) # deserialize response data diff --git a/samples/client/petstore/python/SwaggerPetstore-python/.coverage b/samples/client/petstore/python/SwaggerPetstore-python/.coverage new file mode 100644 index 0000000000000000000000000000000000000000..50a2ec114f96392ad885009385cd17ebb246e395 GIT binary patch literal 3297 zcmc(iX>e0z6ox&eh3+kT7g|bNXee!B3xa|OZp`TG!BV)Qz$J$CBMqcUZju%X6c7;v zSrt@7(Q(EF_ibF!(NSD+8%G`ShvN@*+(yS;T+jD4wbbeF^j9)>CO2=+ea`!Q*_hVl zkju*QM?xXxPefu)npNPBbf}mwpei~hyQjGuoIzGbC>U08C%wzba9JBYR$Rs6o`6y- zRcuYv6JO;E1XQf&wca0TQ2`b9#C>hikcxZ!p`Z#UJW-X1%Qq-bv@_8f347*uf2=8a zxxW9)o;JB#NN(aws6Zsv>5g_fgKcMs3&Su1MJT~IRH6zKPy-k0;6bCZ)~7b2FB*({ z+GQwSd8Y$r+D?`WBQZ(>7Gn&`Q6V9#CE|Q+z`Z!m8N}0wTS-}61CUi7MNiah+sWxK z>Uas;fXSGG(=ZLEV>)JFCeFev%*NR`2j^oBE(GIZG-00nEE`^24j=qz!Eyu;#0soL z2yF<%K@1(}#9FLFmkeMVuE7rM#BS`twYU!ZaXoIpjW~dta0?FMcHDuxa5oOisP4xT zcnVMB89a;U@H}3?i+Bk~@iJb)t9S#)@DAR?`}hDK;v;;F&+#R`#y9vLKj26Fj9>68 ze#7rLfxqxKr;(og5aLYYEaDvEJmM1KGUBns6~yC+tB9+KClJ>VPb97-o5noBXnRpBFR^n~M+lhA&?5lT>sGK`gdbzvfEF$wi> z<5VNJExx@X996CC(=kWp339nZ_%Y54@aDkAH3=CESRQV?dDzu%U{{<4rGE-ihNe@M0Eh?r* zG|YB}_eyAvveu<`^OAYAc66K(wllIfPLs0M4oDm^i%v7jc1HK6={9sV#v4oP&95`N z?~(1FB6gWBW|=K}FGFlkZR(hco4iG~Q|v+-1_|-q4VG`ZtGl_1<&;rsG_ECHn@ryz z&80g_CqK{F5Ykq2lSXV7CoV9pb(yr*Vl0v7(tcbfhE!t6R`FvSqQ;GJBus~CL#`J; zUV%;6jH|F!x@?y;*{=t6w+WA__ICHb;8cs) literal 0 HcmV?d00001 diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/PKG-INFO b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/PKG-INFO new file mode 100644 index 00000000000..ade0b731202 --- /dev/null +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/PKG-INFO @@ -0,0 +1,12 @@ +Metadata-Version: 1.0 +Name: SwaggerPetstore +Version: 1.0.0 +Summary: Swagger Petstore +Home-page: UNKNOWN +Author: UNKNOWN +Author-email: apiteam@wordnik.com +License: UNKNOWN +Description: This is a sample server Petstore server. You can find out more about Swagger at <a href=\"http://swagger.io\">http://swagger.io</a> or on irc.freenode.net, #swagger. For this sample, you can use the api key \"special-key\" to test the authorization filters + +Keywords: Swagger,Swagger Petstore +Platform: UNKNOWN diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/SOURCES.txt b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/SOURCES.txt new file mode 100644 index 00000000000..94955251c31 --- /dev/null +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/SOURCES.txt @@ -0,0 +1,25 @@ +setup.cfg +setup.py +SwaggerPetstore/__init__.py +SwaggerPetstore/config.py +SwaggerPetstore/rest.py +SwaggerPetstore/swagger.py +SwaggerPetstore/util.py +SwaggerPetstore.egg-info/PKG-INFO +SwaggerPetstore.egg-info/SOURCES.txt +SwaggerPetstore.egg-info/dependency_links.txt +SwaggerPetstore.egg-info/requires.txt +SwaggerPetstore.egg-info/top_level.txt +SwaggerPetstore/apis/__init__.py +SwaggerPetstore/apis/pet_api.py +SwaggerPetstore/apis/store_api.py +SwaggerPetstore/apis/user_api.py +SwaggerPetstore/models/__init__.py +SwaggerPetstore/models/category.py +SwaggerPetstore/models/order.py +SwaggerPetstore/models/pet.py +SwaggerPetstore/models/tag.py +SwaggerPetstore/models/user.py +tests/__init__.py +tests/test_api_client.py +tests/test_pet_api.py \ No newline at end of file diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/dependency_links.txt b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/dependency_links.txt new file mode 100644 index 00000000000..8b137891791 --- /dev/null +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/requires.txt b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/requires.txt new file mode 100644 index 00000000000..cf46870af1e --- /dev/null +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/requires.txt @@ -0,0 +1,2 @@ +urllib3 >= 1.10 +six >= 1.9 \ No newline at end of file diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/top_level.txt b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/top_level.txt new file mode 100644 index 00000000000..58cccc9f2e4 --- /dev/null +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/top_level.txt @@ -0,0 +1,2 @@ +tests +SwaggerPetstore diff --git a/samples/client/petstore/python/SwaggerPetstore-python/dev-requirements.txt.log b/samples/client/petstore/python/SwaggerPetstore-python/dev-requirements.txt.log new file mode 100644 index 00000000000..0549f97e65f --- /dev/null +++ b/samples/client/petstore/python/SwaggerPetstore-python/dev-requirements.txt.log @@ -0,0 +1,5 @@ +Requirement already satisfied (use --upgrade to upgrade): nose in /Users/geekerzp/.virtualenvs/python2/lib/python2.7/site-packages (from -r dev-requirements.txt (line 1)) +Requirement already satisfied (use --upgrade to upgrade): tox in /Users/geekerzp/.virtualenvs/python2/lib/python2.7/site-packages (from -r dev-requirements.txt (line 2)) +Requirement already satisfied (use --upgrade to upgrade): coverage in /Users/geekerzp/.virtualenvs/python2/lib/python2.7/site-packages (from -r dev-requirements.txt (line 3)) +Requirement already satisfied (use --upgrade to upgrade): randomize in /Users/geekerzp/.virtualenvs/python2/lib/python2.7/site-packages (from -r dev-requirements.txt (line 4)) +Cleaning up... From 654a73c5a8163080cb760fc3d8af5964b0d94cc5 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Wed, 27 May 2015 20:50:04 +0800 Subject: [PATCH 05/14] rebuild python client --- .../main/resources/python/swagger.mustache | 14 +++++----- .../python/SwaggerPetstore-python/.coverage | Bin 3297 -> 0 bytes .../SwaggerPetstore.egg-info/PKG-INFO | 12 --------- .../SwaggerPetstore.egg-info/SOURCES.txt | 25 ------------------ .../dependency_links.txt | 1 - .../SwaggerPetstore.egg-info/requires.txt | 2 -- .../SwaggerPetstore.egg-info/top_level.txt | 2 -- .../SwaggerPetstore/config.py | 3 +-- .../SwaggerPetstore/swagger.py | 15 ++++++----- .../dev-requirements.txt.log | 5 ---- 10 files changed, 17 insertions(+), 62 deletions(-) delete mode 100644 samples/client/petstore/python/SwaggerPetstore-python/.coverage delete mode 100644 samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/PKG-INFO delete mode 100644 samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/SOURCES.txt delete mode 100644 samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/dependency_links.txt delete mode 100644 samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/requires.txt delete mode 100644 samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/top_level.txt delete mode 100644 samples/client/petstore/python/SwaggerPetstore-python/dev-requirements.txt.log diff --git a/modules/swagger-codegen/src/main/resources/python/swagger.mustache b/modules/swagger-codegen/src/main/resources/python/swagger.mustache index 0483041116d..d7b896d3c0e 100644 --- a/modules/swagger-codegen/src/main/resources/python/swagger.mustache +++ b/modules/swagger-codegen/src/main/resources/python/swagger.mustache @@ -62,11 +62,12 @@ class ApiClient(object): body=None, post_params=None, files=None, response=None, auth_settings=None): # headers parameters - headers_params = self.default_headers.copy().update(header_params) + header_params = header_params or {} + header_params.update(self.default_headers) if self.cookie: - headers_params['Cookie'] = self.cookie - if headers_params: - headers_params = ApiClient.sanitize_for_serialization(headers_params) + header_params['Cookie'] = self.cookie + if header_params: + header_params = ApiClient.sanitize_for_serialization(header_params) # path parameters if path_params: @@ -86,7 +87,7 @@ class ApiClient(object): post_params = self.sanitize_for_serialization(post_params) # auth setting - self.update_params_for_auth(headers_params, query_params, auth_settings) + self.update_params_for_auth(header_params, query_params, auth_settings) # body if body: @@ -96,7 +97,7 @@ class ApiClient(object): url = self.host + resource_path # perform request and return response - response_data = self.request(method, url, query_params=query_params, headers=headers_params, + response_data = self.request(method, url, query_params=query_params, headers=header_params, post_params=post_params, body=body) # deserialize response data @@ -301,3 +302,4 @@ class ApiClient(object): raise ValueError('Authentication token must be in `query` or `header`') + diff --git a/samples/client/petstore/python/SwaggerPetstore-python/.coverage b/samples/client/petstore/python/SwaggerPetstore-python/.coverage deleted file mode 100644 index 50a2ec114f96392ad885009385cd17ebb246e395..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3297 zcmc(iX>e0z6ox&eh3+kT7g|bNXee!B3xa|OZp`TG!BV)Qz$J$CBMqcUZju%X6c7;v zSrt@7(Q(EF_ibF!(NSD+8%G`ShvN@*+(yS;T+jD4wbbeF^j9)>CO2=+ea`!Q*_hVl zkju*QM?xXxPefu)npNPBbf}mwpei~hyQjGuoIzGbC>U08C%wzba9JBYR$Rs6o`6y- zRcuYv6JO;E1XQf&wca0TQ2`b9#C>hikcxZ!p`Z#UJW-X1%Qq-bv@_8f347*uf2=8a zxxW9)o;JB#NN(aws6Zsv>5g_fgKcMs3&Su1MJT~IRH6zKPy-k0;6bCZ)~7b2FB*({ z+GQwSd8Y$r+D?`WBQZ(>7Gn&`Q6V9#CE|Q+z`Z!m8N}0wTS-}61CUi7MNiah+sWxK z>Uas;fXSGG(=ZLEV>)JFCeFev%*NR`2j^oBE(GIZG-00nEE`^24j=qz!Eyu;#0soL z2yF<%K@1(}#9FLFmkeMVuE7rM#BS`twYU!ZaXoIpjW~dta0?FMcHDuxa5oOisP4xT zcnVMB89a;U@H}3?i+Bk~@iJb)t9S#)@DAR?`}hDK;v;;F&+#R`#y9vLKj26Fj9>68 ze#7rLfxqxKr;(og5aLYYEaDvEJmM1KGUBns6~yC+tB9+KClJ>VPb97-o5noBXnRpBFR^n~M+lhA&?5lT>sGK`gdbzvfEF$wi> z<5VNJExx@X996CC(=kWp339nZ_%Y54@aDkAH3=CESRQV?dDzu%U{{<4rGE-ihNe@M0Eh?r* zG|YB}_eyAvveu<`^OAYAc66K(wllIfPLs0M4oDm^i%v7jc1HK6={9sV#v4oP&95`N z?~(1FB6gWBW|=K}FGFlkZR(hco4iG~Q|v+-1_|-q4VG`ZtGl_1<&;rsG_ECHn@ryz z&80g_CqK{F5Ykq2lSXV7CoV9pb(yr*Vl0v7(tcbfhE!t6R`FvSqQ;GJBus~CL#`J; zUV%;6jH|F!x@?y;*{=t6w+WA__ICHb;8cs) diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/PKG-INFO b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/PKG-INFO deleted file mode 100644 index ade0b731202..00000000000 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/PKG-INFO +++ /dev/null @@ -1,12 +0,0 @@ -Metadata-Version: 1.0 -Name: SwaggerPetstore -Version: 1.0.0 -Summary: Swagger Petstore -Home-page: UNKNOWN -Author: UNKNOWN -Author-email: apiteam@wordnik.com -License: UNKNOWN -Description: This is a sample server Petstore server. You can find out more about Swagger at <a href=\"http://swagger.io\">http://swagger.io</a> or on irc.freenode.net, #swagger. For this sample, you can use the api key \"special-key\" to test the authorization filters - -Keywords: Swagger,Swagger Petstore -Platform: UNKNOWN diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/SOURCES.txt b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/SOURCES.txt deleted file mode 100644 index 94955251c31..00000000000 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/SOURCES.txt +++ /dev/null @@ -1,25 +0,0 @@ -setup.cfg -setup.py -SwaggerPetstore/__init__.py -SwaggerPetstore/config.py -SwaggerPetstore/rest.py -SwaggerPetstore/swagger.py -SwaggerPetstore/util.py -SwaggerPetstore.egg-info/PKG-INFO -SwaggerPetstore.egg-info/SOURCES.txt -SwaggerPetstore.egg-info/dependency_links.txt -SwaggerPetstore.egg-info/requires.txt -SwaggerPetstore.egg-info/top_level.txt -SwaggerPetstore/apis/__init__.py -SwaggerPetstore/apis/pet_api.py -SwaggerPetstore/apis/store_api.py -SwaggerPetstore/apis/user_api.py -SwaggerPetstore/models/__init__.py -SwaggerPetstore/models/category.py -SwaggerPetstore/models/order.py -SwaggerPetstore/models/pet.py -SwaggerPetstore/models/tag.py -SwaggerPetstore/models/user.py -tests/__init__.py -tests/test_api_client.py -tests/test_pet_api.py \ No newline at end of file diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/dependency_links.txt b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/dependency_links.txt deleted file mode 100644 index 8b137891791..00000000000 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/dependency_links.txt +++ /dev/null @@ -1 +0,0 @@ - diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/requires.txt b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/requires.txt deleted file mode 100644 index cf46870af1e..00000000000 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/requires.txt +++ /dev/null @@ -1,2 +0,0 @@ -urllib3 >= 1.10 -six >= 1.9 \ No newline at end of file diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/top_level.txt b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/top_level.txt deleted file mode 100644 index 58cccc9f2e4..00000000000 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore.egg-info/top_level.txt +++ /dev/null @@ -1,2 +0,0 @@ -tests -SwaggerPetstore diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/config.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/config.py index ec3dbe8efb4..46e60ccf417 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/config.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/config.py @@ -15,8 +15,7 @@ def get_basic_auth_token(): global username global password - if username and password: - return urllib3.util.make_headers(basic_auth=username + ':' + password).get('authorization') + return urllib3.util.make_headers(basic_auth=username + ':' + password).get('authorization') def auth_settings(): return { diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py index f01192dd46b..d7b896d3c0e 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py @@ -62,12 +62,12 @@ class ApiClient(object): body=None, post_params=None, files=None, response=None, auth_settings=None): # headers parameters - headers = self.default_headers.copy() - headers.update(header_params) + header_params = header_params or {} + header_params.update(self.default_headers) if self.cookie: - headers['Cookie'] = self.cookie - if headers: - headers = self.sanitize_for_serialization(headers) + header_params['Cookie'] = self.cookie + if header_params: + header_params = ApiClient.sanitize_for_serialization(header_params) # path parameters if path_params: @@ -87,7 +87,7 @@ class ApiClient(object): post_params = self.sanitize_for_serialization(post_params) # auth setting - self.update_params_for_auth(headers, query_params, auth_settings) + self.update_params_for_auth(header_params, query_params, auth_settings) # body if body: @@ -97,7 +97,7 @@ class ApiClient(object): url = self.host + resource_path # perform request and return response - response_data = self.request(method, url, query_params=query_params, headers=headers, + response_data = self.request(method, url, query_params=query_params, headers=header_params, post_params=post_params, body=body) # deserialize response data @@ -302,3 +302,4 @@ class ApiClient(object): raise ValueError('Authentication token must be in `query` or `header`') + diff --git a/samples/client/petstore/python/SwaggerPetstore-python/dev-requirements.txt.log b/samples/client/petstore/python/SwaggerPetstore-python/dev-requirements.txt.log deleted file mode 100644 index 0549f97e65f..00000000000 --- a/samples/client/petstore/python/SwaggerPetstore-python/dev-requirements.txt.log +++ /dev/null @@ -1,5 +0,0 @@ -Requirement already satisfied (use --upgrade to upgrade): nose in /Users/geekerzp/.virtualenvs/python2/lib/python2.7/site-packages (from -r dev-requirements.txt (line 1)) -Requirement already satisfied (use --upgrade to upgrade): tox in /Users/geekerzp/.virtualenvs/python2/lib/python2.7/site-packages (from -r dev-requirements.txt (line 2)) -Requirement already satisfied (use --upgrade to upgrade): coverage in /Users/geekerzp/.virtualenvs/python2/lib/python2.7/site-packages (from -r dev-requirements.txt (line 3)) -Requirement already satisfied (use --upgrade to upgrade): randomize in /Users/geekerzp/.virtualenvs/python2/lib/python2.7/site-packages (from -r dev-requirements.txt (line 4)) -Cleaning up... From 51887c64b518f60c0568e8b07340904830ee44be Mon Sep 17 00:00:00 2001 From: geekerzp Date: Fri, 29 May 2015 10:47:07 +0800 Subject: [PATCH 06/14] Updated python codegen. Use File.separatorChar to create invokerPacakge variable instread of '\' char. --- .../swagger/codegen/languages/PythonClientCodegen.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 590e17f6326..44dfaeefcd4 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 @@ -27,7 +27,9 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig super(); eggPackage = module + "-python"; - invokerPackage = eggPackage + "/" + module; + + invokerPackage = eggPackage + "." + module; + invokerPackage = invokerPackage.replace('.', File.separatorChar); outputFolder = "generated-code/python"; modelTemplateFiles.put("model.mustache", ".py"); From ab6f327f35a2b54a22b7161151262e3bb4dbd026 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Tue, 2 Jun 2015 18:14:46 +0800 Subject: [PATCH 07/14] 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` --- .../codegen/languages/PythonClientCodegen.java | 17 ++++++++--------- .../resources/python/__init__package.mustache | 2 +- .../src/main/resources/python/api.mustache | 2 +- .../{swagger.mustache => api_client.mustache} | 6 +++--- .../{config.mustache => configuration.mustache} | 0 .../src/main/resources/python/rest.mustache | 4 ++-- .../SwaggerPetstore/__init__.py | 2 +- .../{swagger.py => api_client.py} | 6 +++--- .../SwaggerPetstore/apis/pet_api.py | 2 +- .../SwaggerPetstore/apis/store_api.py | 2 +- .../SwaggerPetstore/apis/user_api.py | 2 +- .../{config.py => configuration.py} | 0 .../SwaggerPetstore/rest.py | 4 ++-- .../tests/test_api_client.py | 16 ++++++++-------- .../tests/test_pet_api.py | 7 +++---- 15 files changed, 35 insertions(+), 37 deletions(-) rename modules/swagger-codegen/src/main/resources/python/{swagger.mustache => api_client.mustache} (98%) rename modules/swagger-codegen/src/main/resources/python/{config.mustache => configuration.mustache} (100%) rename samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/{swagger.py => api_client.py} (98%) rename samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/{config.py => configuration.py} (100%) 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 44dfaeefcd4..4cb96497aff 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 @@ -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 diff --git a/modules/swagger-codegen/src/main/resources/python/__init__package.mustache b/modules/swagger-codegen/src/main/resources/python/__init__package.mustache index 7b48136d3a9..921f4dac0eb 100644 --- a/modules/swagger-codegen/src/main/resources/python/__init__package.mustache +++ b/modules/swagger-codegen/src/main/resources/python/__init__package.mustache @@ -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 diff --git a/modules/swagger-codegen/src/main/resources/python/api.mustache b/modules/swagger-codegen/src/main/resources/python/api.mustache index 55bfd9e82a4..69c90491b95 100644 --- a/modules/swagger-codegen/src/main/resources/python/api.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api.mustache @@ -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): diff --git a/modules/swagger-codegen/src/main/resources/python/swagger.mustache b/modules/swagger-codegen/src/main/resources/python/api_client.mustache similarity index 98% rename from modules/swagger-codegen/src/main/resources/python/swagger.mustache rename to modules/swagger-codegen/src/main/resources/python/api_client.mustache index d7b896d3c0e..4ba445e2fa6 100644 --- a/modules/swagger-codegen/src/main/resources/python/swagger.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api_client.mustache @@ -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'] diff --git a/modules/swagger-codegen/src/main/resources/python/config.mustache b/modules/swagger-codegen/src/main/resources/python/configuration.mustache similarity index 100% rename from modules/swagger-codegen/src/main/resources/python/config.mustache rename to modules/swagger-codegen/src/main/resources/python/configuration.mustache diff --git a/modules/swagger-codegen/src/main/resources/python/rest.mustache b/modules/swagger-codegen/src/main/resources/python/rest.mustache index da99abc2493..44a1022906b 100644 --- a/modules/swagger-codegen/src/main/resources/python/rest.mustache +++ b/modules/swagger-codegen/src/main/resources/python/rest.mustache @@ -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 """ diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/__init__.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/__init__.py index 9d4b2db3de5..3e7b51b8467 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/__init__.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/__init__.py @@ -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 diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py similarity index 98% rename from samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py rename to samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py index d7b896d3c0e..4ba445e2fa6 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py @@ -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'] 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 37da9901597..1a738e4c317 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 @@ -29,7 +29,7 @@ from six import iteritems from ..util import remove_none -from .. import config +from ..api_client import ApiClient class PetApi(object): 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 649cb5f704d..aab7d0f11b6 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 @@ -29,7 +29,7 @@ from six import iteritems from ..util import remove_none -from .. import config +from ..api_client import ApiClient class StoreApi(object): 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 3f2b36a85fa..66cb235c3d7 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 @@ -29,7 +29,7 @@ from six import iteritems from ..util import remove_none -from .. import config +from ..api_client import ApiClient class UserApi(object): diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/config.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/configuration.py similarity index 100% rename from samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/config.py rename to samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/configuration.py diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/rest.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/rest.py index da99abc2493..44a1022906b 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/rest.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/rest.py @@ -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 """ diff --git a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py index 6bc3000f267..560d01d0f3d 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py @@ -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'] 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 ee8c5dde14e..2c50ac861a1 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 @@ -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__': From fbe327ac2f8b59854c13971515a6e8b66d861615 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Tue, 2 Jun 2015 18:23:55 +0800 Subject: [PATCH 08/14] Updated PythonClientCodegen.java * updated method apiFileFolder. * udpated method modelFileFolder. --- .../swagger/codegen/languages/PythonClientCodegen.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 4cb96497aff..679b3174c7c 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 @@ -85,11 +85,11 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig @Override public String apiFileFolder() { - return outputFolder + "/" + apiPackage().replace('.', File.separatorChar); + return outputFolder + File.separatorChar + apiPackage().replace('.', File.separatorChar); } public String modelFileFolder() { - return outputFolder + "/" + modelPackage().replace('.', File.separatorChar); + return outputFolder + File.separatorChar + modelPackage().replace('.', File.separatorChar); } @Override From 5fbda0e2069797cdd9b2c515c029ad32678f86fd Mon Sep 17 00:00:00 2001 From: geekerzp Date: Wed, 3 Jun 2015 10:46:32 +0800 Subject: [PATCH 09/14] added test case for 404 response of python client --- .../SwaggerPetstore/rest.py | 6 ++++ .../tests/test_api_exception.py | 31 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_exception.py diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/rest.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/rest.py index 44a1022906b..e7051886dcd 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/rest.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/rest.py @@ -1,4 +1,10 @@ # coding: utf-8 + +""" +Credit: this file (rest.py) is modified based on rest.py in Dropbox Python SDK: +https://www.dropbox.com/developers/core/sdks/python +""" + import sys import io import json diff --git a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_exception.py b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_exception.py new file mode 100644 index 00000000000..6e306753989 --- /dev/null +++ b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_exception.py @@ -0,0 +1,31 @@ +# coding: utf-8 + +""" +Run the tests. +$ pip install nose (optional) +$ cd SwaggerPetstore-python +$ nosetests -v +""" + +import os +import time +import unittest + +import SwaggerPetstore +from SwaggerPetstore.rest import ApiException + + +class ApiExceptionTests(unittest.TestCase): + + def setUp(self): + self.api_client = SwaggerPetstore.ApiClient() + self.pet_api = SwaggerPetstore.PetApi(self.api_client) + + def tearDown(self): + time.sleep(1) + + def test_404_error(self): + self.pet_api.delete_pet(pet_id=1234) + + with self.assertRaisesRegexp(ApiException, "Pet not found"): + self.pet_api.get_pet_by_id(pet_id=1234) From 8ef4c4401e3fac538b77e8242f87548c1783aba4 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Wed, 3 Jun 2015 10:50:32 +0800 Subject: [PATCH 10/14] updated rest.mustache of python client --- .../swagger-codegen/src/main/resources/python/rest.mustache | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/modules/swagger-codegen/src/main/resources/python/rest.mustache b/modules/swagger-codegen/src/main/resources/python/rest.mustache index 44a1022906b..e7051886dcd 100644 --- a/modules/swagger-codegen/src/main/resources/python/rest.mustache +++ b/modules/swagger-codegen/src/main/resources/python/rest.mustache @@ -1,4 +1,10 @@ # coding: utf-8 + +""" +Credit: this file (rest.py) is modified based on rest.py in Dropbox Python SDK: +https://www.dropbox.com/developers/core/sdks/python +""" + import sys import io import json From 813c0119aafe79e8f5be23c7af75bbd4ffc0b875 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Wed, 3 Jun 2015 15:04:36 +0800 Subject: [PATCH 11/14] added test case for 500 response of python client --- .../src/main/resources/python/rest.mustache | 5 +- .../src/main/resources/python/util.mustache | 13 +++-- .../SwaggerPetstore/rest.py | 5 +- .../SwaggerPetstore/util.py | 13 +++-- .../tests/test_api_exception.py | 49 ++++++++++++++++++- 5 files changed, 67 insertions(+), 18 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/python/rest.mustache b/modules/swagger-codegen/src/main/resources/python/rest.mustache index e7051886dcd..508f3d6693a 100644 --- a/modules/swagger-codegen/src/main/resources/python/rest.mustache +++ b/modules/swagger-codegen/src/main/resources/python/rest.mustache @@ -190,7 +190,10 @@ class ApiException(Exception): """ Custom error response messages """ - return "({0})\nReason: {1}\nHeader: {2}\nBody: {3}\n".\ + return "({0})\n"\ + "Reason: {1}\n"\ + "HTTP response headers: {2}\n"\ + "HTTP response body: {3}\n".\ format(self.status, self.reason, self.headers, self.body) class RESTClient(object): diff --git a/modules/swagger-codegen/src/main/resources/python/util.mustache b/modules/swagger-codegen/src/main/resources/python/util.mustache index 1137a5d2d23..92df1e37e3a 100644 --- a/modules/swagger-codegen/src/main/resources/python/util.mustache +++ b/modules/swagger-codegen/src/main/resources/python/util.mustache @@ -1,6 +1,12 @@ +# coding: utf-8 + from six import iteritems def remove_none(obj): + """ + Remove None from `list`, `tuple`, `set`. + Remove None value from `dict`. + """ if isinstance(obj, (list, tuple, set)): return type(obj)(remove_none(x) for x in obj if x is not None) elif isinstance(obj, dict): @@ -8,10 +14,3 @@ def remove_none(obj): for k, v in iteritems(obj) if k is not None and v is not None) else: return obj - - -def inspect_vars(obj): - if not hasattr(obj, '__dict__'): - return obj - else: - return {k: inspect_vars(getattr(obj, k)) for k in dir(obj)} diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/rest.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/rest.py index e7051886dcd..508f3d6693a 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/rest.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/rest.py @@ -190,7 +190,10 @@ class ApiException(Exception): """ Custom error response messages """ - return "({0})\nReason: {1}\nHeader: {2}\nBody: {3}\n".\ + return "({0})\n"\ + "Reason: {1}\n"\ + "HTTP response headers: {2}\n"\ + "HTTP response body: {3}\n".\ format(self.status, self.reason, self.headers, self.body) class RESTClient(object): diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/util.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/util.py index 1137a5d2d23..92df1e37e3a 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/util.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/util.py @@ -1,6 +1,12 @@ +# coding: utf-8 + from six import iteritems def remove_none(obj): + """ + Remove None from `list`, `tuple`, `set`. + Remove None value from `dict`. + """ if isinstance(obj, (list, tuple, set)): return type(obj)(remove_none(x) for x in obj if x is not None) elif isinstance(obj, dict): @@ -8,10 +14,3 @@ def remove_none(obj): for k, v in iteritems(obj) if k is not None and v is not None) else: return obj - - -def inspect_vars(obj): - if not hasattr(obj, '__dict__'): - return obj - else: - return {k: inspect_vars(getattr(obj, k)) for k in dir(obj)} diff --git a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_exception.py b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_exception.py index 6e306753989..d1b786241e8 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_exception.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_exception.py @@ -20,12 +20,57 @@ class ApiExceptionTests(unittest.TestCase): def setUp(self): self.api_client = SwaggerPetstore.ApiClient() self.pet_api = SwaggerPetstore.PetApi(self.api_client) + self.setUpModels() + + def setUpModels(self): + self.category = SwaggerPetstore.Category() + self.category.id = int(time.time()) + self.category.name = "dog" + self.tag = SwaggerPetstore.Tag() + self.tag.id = int(time.time()) + self.tag.name = "blank" + self.pet = SwaggerPetstore.Pet() + self.pet.id = int(time.time()) + self.pet.name = "hello kity" + self.pet.photo_urls = ["http://foo.bar.com/1", "http://foo.bar.com/2"] + self.pet.status = "sold" + self.pet.category = self.category + self.pet.tags = [self.tag] def tearDown(self): time.sleep(1) def test_404_error(self): - self.pet_api.delete_pet(pet_id=1234) + self.pet_api.add_pet(body=self.pet) + self.pet_api.delete_pet(pet_id=self.pet.id) with self.assertRaisesRegexp(ApiException, "Pet not found"): - self.pet_api.get_pet_by_id(pet_id=1234) + self.pet_api.get_pet_by_id(pet_id=self.pet.id) + + try: + self.pet_api.get_pet_by_id(pet_id=self.pet.id) + except ApiException as e: + self.assertEqual(e.status, 404) + self.assertEqual(e.reason, "Not Found") + self.assertDictEqual(e.body, {'message': 'Pet not found', 'code': 1, 'type': 'error'}) + + def test_500_error(self): + self.pet_api.add_pet(body=self.pet) + + with self.assertRaisesRegexp(ApiException, "Internal Server Error"): + self.pet_api.upload_file( + pet_id=self.pet.id, + additional_metadata="special", + file=None + ) + + try: + self.pet_api.upload_file( + pet_id=self.pet.id, + additional_metadata="special", + file=None + ) + except ApiException as e: + self.assertEqual(e.status, 500) + self.assertEqual(e.reason, "Internal Server Error") + self.assertRegexpMatches(e.body, "Error 500 Internal Server Error") From a30f537f5fdb645f5fdbd8baed3b7b9e46364d3c Mon Sep 17 00:00:00 2001 From: geekerzp Date: Wed, 3 Jun 2015 15:07:50 +0800 Subject: [PATCH 12/14] fixed typo --- .../python/SwaggerPetstore-python/tests/test_api_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py index 560d01d0f3d..0ef4314d0f4 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py @@ -22,7 +22,7 @@ class ApiClientTests(unittest.TestCase): def setUp(self): self.api_client = SwaggerPetstore.ApiClient(HOST) - def test_configuratjion(self): + def test_configuration(self): SwaggerPetstore.configuration.api_key['api_key'] = '123456' SwaggerPetstore.configuration.api_key_prefix['api_key'] = 'PREFIX' SwaggerPetstore.configuration.username = 'test_username' From ddc5293e6a43713a8cbbb86de1b0b91014165dae Mon Sep 17 00:00:00 2001 From: geekerzp Date: Wed, 3 Jun 2015 18:26:51 +0800 Subject: [PATCH 13/14] updated api.mustache of python. --- .../languages/PythonClientCodegen.java | 1 - .../src/main/resources/python/api.mustache | 35 +++- .../main/resources/python/api_client.mustache | 11 +- .../src/main/resources/python/util.mustache | 16 -- .../SwaggerPetstore/api_client.py | 11 +- .../SwaggerPetstore/apis/pet_api.py | 176 ++++++++++++------ .../SwaggerPetstore/apis/store_api.py | 77 +++++--- .../SwaggerPetstore/apis/user_api.py | 165 ++++++++++------ .../SwaggerPetstore/util.py | 16 -- 9 files changed, 323 insertions(+), 185 deletions(-) delete mode 100644 modules/swagger-codegen/src/main/resources/python/util.mustache delete mode 100644 samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/util.py 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 679b3174c7c..7f9a77d3132 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,7 +71,6 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig supportingFiles.add(new SupportingFile("setup.mustache", eggPackage, "setup.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("configuration.mustache", invokerPackage, "configuration.py")); supportingFiles.add(new SupportingFile("__init__package.mustache", invokerPackage, "__init__.py")); supportingFiles.add(new SupportingFile("__init__model.mustache", modelPackage, "__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 69c90491b95..a91797d71b6 100644 --- a/modules/swagger-codegen/src/main/resources/python/api.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api.mustache @@ -27,8 +27,6 @@ import os # python 2 and python 3 compatibility library from six import iteritems -from ..util import remove_none - from ..api_client import ApiClient {{#operations}} @@ -66,13 +64,32 @@ class {{classname}}(object): resource_path = '{{path}}'.replace('{format}', 'json') method = '{{httpMethod}}' - path_params = remove_none(dict({{#pathParams}}{{baseName}}=params.get('{{paramName}}'){{#hasMore}}, {{/hasMore}}{{/pathParams}})) - query_params = remove_none(dict({{#queryParams}}{{baseName}}=params.get('{{paramName}}'){{#hasMore}}, {{/hasMore}}{{/queryParams}})) - header_params = remove_none(dict({{#headerParams}}{{baseName}}=params.get('{{paramName}}'){{#hasMore}}, {{/hasMore}}{{/headerParams}})) - form_params = remove_none(dict({{#formParams}}{{^isFile}}{{baseName}}=params.get('{{paramName}}'){{#hasMore}}, {{/hasMore}}{{/isFile}}{{/formParams}})) - files = remove_none(dict({{#formParams}}{{#isFile}}{{baseName}}=params.get('{{paramName}}'){{#hasMore}}, {{/hasMore}}{{/isFile}}{{/formParams}})) - body_params = {{#bodyParam}}params.get('{{paramName}}'){{/bodyParam}}{{^bodyParam}}None{{/bodyParam}} - + path_params = {} + {{#pathParams}} + if '{{paramName}}' in params: + path_params['{{baseName}}'] = params['{{paramName}}'] + {{/pathParams}} + query_params = {} + {{#queryParams}} + if '{{paramName}}' in params: + query_params['{{baseName}}'] = params['{{paramName}}'] + {{/queryParams}} + header_params = {} + {{#headerParams}} + if '{{paramName}}' in params: + header_params['{{baseName}}'] = params['{{paramName}}'] + {{/headerParams}} + form_params = {} + files = {} + {{#formParams}} + if '{{paramName}}' in params: + {{#notFile}}form_params['{{baseName}}'] = params['{{paramName}}']{{/notFile}}{{#isFile}}files['{{baseName}}'] = params['{{paramName}}']{{/isFile}} + {{/formParams}} + body_params = None + {{#bodyParam}} + if '{{paramName}}' in params: + body_params = params['{{paramName}}'] + {{/bodyParam}} # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept([{{#produces}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/produces}}]) if not header_params['Accept']: diff --git a/modules/swagger-codegen/src/main/resources/python/api_client.mustache b/modules/swagger-codegen/src/main/resources/python/api_client.mustache index 4ba445e2fa6..75c6e336a07 100644 --- a/modules/swagger-codegen/src/main/resources/python/api_client.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api_client.mustache @@ -248,11 +248,12 @@ class ApiClient(object): if files: for k, v in iteritems(files): - with open(v, 'rb') as f: - filename = os.path.basename(f.name) - filedata = f.read() - mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream' - params[k] = tuple([filename, filedata, mimetype]) + if v: + with open(v, 'rb') as f: + filename = os.path.basename(f.name) + filedata = f.read() + mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream' + params[k] = tuple([filename, filedata, mimetype]) return params diff --git a/modules/swagger-codegen/src/main/resources/python/util.mustache b/modules/swagger-codegen/src/main/resources/python/util.mustache deleted file mode 100644 index 92df1e37e3a..00000000000 --- a/modules/swagger-codegen/src/main/resources/python/util.mustache +++ /dev/null @@ -1,16 +0,0 @@ -# coding: utf-8 - -from six import iteritems - -def remove_none(obj): - """ - Remove None from `list`, `tuple`, `set`. - Remove None value from `dict`. - """ - if isinstance(obj, (list, tuple, set)): - return type(obj)(remove_none(x) for x in obj if x is not None) - elif isinstance(obj, dict): - return type(obj)((remove_none(k), remove_none(v)) - for k, v in iteritems(obj) if k is not None and v is not None) - else: - return obj diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py index 4ba445e2fa6..75c6e336a07 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py @@ -248,11 +248,12 @@ class ApiClient(object): if files: for k, v in iteritems(files): - with open(v, 'rb') as f: - filename = os.path.basename(f.name) - filedata = f.read() - mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream' - params[k] = tuple([filename, filedata, mimetype]) + if v: + with open(v, 'rb') as f: + filename = os.path.basename(f.name) + filedata = f.read() + mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream' + params[k] = tuple([filename, filedata, mimetype]) return params 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 1a738e4c317..ec0a8c0f5fd 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 @@ -27,8 +27,6 @@ import os # python 2 and python 3 compatibility library from six import iteritems -from ..util import remove_none - from ..api_client import ApiClient class PetApi(object): @@ -61,13 +59,20 @@ class PetApi(object): resource_path = '/pet'.replace('{format}', 'json') method = 'PUT' - path_params = remove_none(dict()) - query_params = remove_none(dict()) - header_params = remove_none(dict()) - form_params = remove_none(dict()) - files = remove_none(dict()) - body_params = params.get('body') - + path_params = {} + + query_params = {} + + header_params = {} + + form_params = {} + files = {} + + body_params = None + + if 'body' in params: + body_params = params['body'] + # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml']) if not header_params['Accept']: @@ -105,13 +110,20 @@ class PetApi(object): resource_path = '/pet'.replace('{format}', 'json') method = 'POST' - path_params = remove_none(dict()) - query_params = remove_none(dict()) - header_params = remove_none(dict()) - form_params = remove_none(dict()) - files = remove_none(dict()) - body_params = params.get('body') - + path_params = {} + + query_params = {} + + header_params = {} + + form_params = {} + files = {} + + body_params = None + + if 'body' in params: + body_params = params['body'] + # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml']) if not header_params['Accept']: @@ -149,13 +161,20 @@ class PetApi(object): resource_path = '/pet/findByStatus'.replace('{format}', 'json') method = 'GET' - path_params = remove_none(dict()) - query_params = remove_none(dict(status=params.get('status'))) - header_params = remove_none(dict()) - form_params = remove_none(dict()) - files = remove_none(dict()) + path_params = {} + + query_params = {} + + if 'status' in params: + query_params['status'] = params['status'] + + header_params = {} + + form_params = {} + files = {} + body_params = None - + # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml']) if not header_params['Accept']: @@ -195,13 +214,20 @@ class PetApi(object): resource_path = '/pet/findByTags'.replace('{format}', 'json') method = 'GET' - path_params = remove_none(dict()) - query_params = remove_none(dict(tags=params.get('tags'))) - header_params = remove_none(dict()) - form_params = remove_none(dict()) - files = remove_none(dict()) + path_params = {} + + query_params = {} + + if 'tags' in params: + query_params['tags'] = params['tags'] + + header_params = {} + + form_params = {} + files = {} + body_params = None - + # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml']) if not header_params['Accept']: @@ -245,13 +271,20 @@ class PetApi(object): resource_path = '/pet/{petId}'.replace('{format}', 'json') method = 'GET' - path_params = remove_none(dict(petId=params.get('pet_id'))) - query_params = remove_none(dict()) - header_params = remove_none(dict()) - form_params = remove_none(dict()) - files = remove_none(dict()) + path_params = {} + + if 'pet_id' in params: + path_params['petId'] = params['pet_id'] + + query_params = {} + + header_params = {} + + form_params = {} + files = {} + body_params = None - + # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml']) if not header_params['Accept']: @@ -297,13 +330,26 @@ class PetApi(object): resource_path = '/pet/{petId}'.replace('{format}', 'json') method = 'POST' - path_params = remove_none(dict(petId=params.get('pet_id'))) - query_params = remove_none(dict()) - header_params = remove_none(dict()) - form_params = remove_none(dict(name=params.get('name'), status=params.get('status'))) - files = remove_none(dict()) + path_params = {} + + if 'pet_id' in params: + path_params['petId'] = params['pet_id'] + + query_params = {} + + header_params = {} + + form_params = {} + files = {} + + if 'name' in params: + form_params['name'] = params['name'] + + if 'status' in params: + form_params['status'] = params['status'] + body_params = None - + # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml']) if not header_params['Accept']: @@ -346,13 +392,23 @@ class PetApi(object): resource_path = '/pet/{petId}'.replace('{format}', 'json') method = 'DELETE' - path_params = remove_none(dict(petId=params.get('pet_id'))) - query_params = remove_none(dict()) - header_params = remove_none(dict(api_key=params.get('api_key'))) - form_params = remove_none(dict()) - files = remove_none(dict()) + path_params = {} + + if 'pet_id' in params: + path_params['petId'] = params['pet_id'] + + query_params = {} + + header_params = {} + + if 'api_key' in params: + header_params['api_key'] = params['api_key'] + + form_params = {} + files = {} + body_params = None - + # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml']) if not header_params['Accept']: @@ -396,13 +452,26 @@ class PetApi(object): resource_path = '/pet/{petId}/uploadImage'.replace('{format}', 'json') method = 'POST' - path_params = remove_none(dict(petId=params.get('pet_id'))) - query_params = remove_none(dict()) - header_params = remove_none(dict()) - form_params = remove_none(dict(additionalMetadata=params.get('additional_metadata'), )) - files = remove_none(dict(file=params.get('file'))) + path_params = {} + + if 'pet_id' in params: + path_params['petId'] = params['pet_id'] + + query_params = {} + + header_params = {} + + form_params = {} + files = {} + + if 'additional_metadata' in params: + form_params['additionalMetadata'] = params['additional_metadata'] + + if 'file' in params: + files['file'] = params['file'] + body_params = None - + # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml']) if not header_params['Accept']: @@ -427,3 +496,6 @@ class PetApi(object): + + + 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 aab7d0f11b6..03f0b2ec170 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 @@ -27,8 +27,6 @@ import os # python 2 and python 3 compatibility library from six import iteritems -from ..util import remove_none - from ..api_client import ApiClient class StoreApi(object): @@ -60,13 +58,17 @@ class StoreApi(object): resource_path = '/store/inventory'.replace('{format}', 'json') method = 'GET' - path_params = remove_none(dict()) - query_params = remove_none(dict()) - header_params = remove_none(dict()) - form_params = remove_none(dict()) - files = remove_none(dict()) + path_params = {} + + query_params = {} + + header_params = {} + + form_params = {} + files = {} + body_params = None - + # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml']) if not header_params['Accept']: @@ -106,13 +108,20 @@ class StoreApi(object): resource_path = '/store/order'.replace('{format}', 'json') method = 'POST' - path_params = remove_none(dict()) - query_params = remove_none(dict()) - header_params = remove_none(dict()) - form_params = remove_none(dict()) - files = remove_none(dict()) - body_params = params.get('body') - + path_params = {} + + query_params = {} + + header_params = {} + + form_params = {} + files = {} + + body_params = None + + if 'body' in params: + body_params = params['body'] + # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml']) if not header_params['Accept']: @@ -156,13 +165,20 @@ class StoreApi(object): resource_path = '/store/order/{orderId}'.replace('{format}', 'json') method = 'GET' - path_params = remove_none(dict(orderId=params.get('order_id'))) - query_params = remove_none(dict()) - header_params = remove_none(dict()) - form_params = remove_none(dict()) - files = remove_none(dict()) + path_params = {} + + if 'order_id' in params: + path_params['orderId'] = params['order_id'] + + query_params = {} + + header_params = {} + + form_params = {} + files = {} + body_params = None - + # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml']) if not header_params['Accept']: @@ -206,13 +222,20 @@ class StoreApi(object): resource_path = '/store/order/{orderId}'.replace('{format}', 'json') method = 'DELETE' - path_params = remove_none(dict(orderId=params.get('order_id'))) - query_params = remove_none(dict()) - header_params = remove_none(dict()) - form_params = remove_none(dict()) - files = remove_none(dict()) + path_params = {} + + if 'order_id' in params: + path_params['orderId'] = params['order_id'] + + query_params = {} + + header_params = {} + + form_params = {} + files = {} + body_params = None - + # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml']) if not header_params['Accept']: 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 66cb235c3d7..223566b8684 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 @@ -27,8 +27,6 @@ import os # python 2 and python 3 compatibility library from six import iteritems -from ..util import remove_none - from ..api_client import ApiClient class UserApi(object): @@ -61,13 +59,20 @@ class UserApi(object): resource_path = '/user'.replace('{format}', 'json') method = 'POST' - path_params = remove_none(dict()) - query_params = remove_none(dict()) - header_params = remove_none(dict()) - form_params = remove_none(dict()) - files = remove_none(dict()) - body_params = params.get('body') - + path_params = {} + + query_params = {} + + header_params = {} + + form_params = {} + files = {} + + body_params = None + + if 'body' in params: + body_params = params['body'] + # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml']) if not header_params['Accept']: @@ -105,13 +110,20 @@ class UserApi(object): resource_path = '/user/createWithArray'.replace('{format}', 'json') method = 'POST' - path_params = remove_none(dict()) - query_params = remove_none(dict()) - header_params = remove_none(dict()) - form_params = remove_none(dict()) - files = remove_none(dict()) - body_params = params.get('body') - + path_params = {} + + query_params = {} + + header_params = {} + + form_params = {} + files = {} + + body_params = None + + if 'body' in params: + body_params = params['body'] + # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml']) if not header_params['Accept']: @@ -149,13 +161,20 @@ class UserApi(object): resource_path = '/user/createWithList'.replace('{format}', 'json') method = 'POST' - path_params = remove_none(dict()) - query_params = remove_none(dict()) - header_params = remove_none(dict()) - form_params = remove_none(dict()) - files = remove_none(dict()) - body_params = params.get('body') - + path_params = {} + + query_params = {} + + header_params = {} + + form_params = {} + files = {} + + body_params = None + + if 'body' in params: + body_params = params['body'] + # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml']) if not header_params['Accept']: @@ -194,13 +213,23 @@ class UserApi(object): resource_path = '/user/login'.replace('{format}', 'json') method = 'GET' - path_params = remove_none(dict()) - query_params = remove_none(dict(username=params.get('username'), password=params.get('password'))) - header_params = remove_none(dict()) - form_params = remove_none(dict()) - files = remove_none(dict()) + path_params = {} + + query_params = {} + + if 'username' in params: + query_params['username'] = params['username'] + + if 'password' in params: + query_params['password'] = params['password'] + + header_params = {} + + form_params = {} + files = {} + body_params = None - + # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml']) if not header_params['Accept']: @@ -239,13 +268,17 @@ class UserApi(object): resource_path = '/user/logout'.replace('{format}', 'json') method = 'GET' - path_params = remove_none(dict()) - query_params = remove_none(dict()) - header_params = remove_none(dict()) - form_params = remove_none(dict()) - files = remove_none(dict()) + path_params = {} + + query_params = {} + + header_params = {} + + form_params = {} + files = {} + body_params = None - + # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml']) if not header_params['Accept']: @@ -287,13 +320,20 @@ class UserApi(object): resource_path = '/user/{username}'.replace('{format}', 'json') method = 'GET' - path_params = remove_none(dict(username=params.get('username'))) - query_params = remove_none(dict()) - header_params = remove_none(dict()) - form_params = remove_none(dict()) - files = remove_none(dict()) + path_params = {} + + if 'username' in params: + path_params['username'] = params['username'] + + query_params = {} + + header_params = {} + + form_params = {} + files = {} + body_params = None - + # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml']) if not header_params['Accept']: @@ -338,13 +378,23 @@ class UserApi(object): resource_path = '/user/{username}'.replace('{format}', 'json') method = 'PUT' - path_params = remove_none(dict(username=params.get('username'))) - query_params = remove_none(dict()) - header_params = remove_none(dict()) - form_params = remove_none(dict()) - files = remove_none(dict()) - body_params = params.get('body') - + path_params = {} + + if 'username' in params: + path_params['username'] = params['username'] + + query_params = {} + + header_params = {} + + form_params = {} + files = {} + + body_params = None + + if 'body' in params: + body_params = params['body'] + # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml']) if not header_params['Accept']: @@ -386,13 +436,20 @@ class UserApi(object): resource_path = '/user/{username}'.replace('{format}', 'json') method = 'DELETE' - path_params = remove_none(dict(username=params.get('username'))) - query_params = remove_none(dict()) - header_params = remove_none(dict()) - form_params = remove_none(dict()) - files = remove_none(dict()) + path_params = {} + + if 'username' in params: + path_params['username'] = params['username'] + + query_params = {} + + header_params = {} + + form_params = {} + files = {} + body_params = None - + # HTTP header `Accept` header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml']) if not header_params['Accept']: diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/util.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/util.py deleted file mode 100644 index 92df1e37e3a..00000000000 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/util.py +++ /dev/null @@ -1,16 +0,0 @@ -# coding: utf-8 - -from six import iteritems - -def remove_none(obj): - """ - Remove None from `list`, `tuple`, `set`. - Remove None value from `dict`. - """ - if isinstance(obj, (list, tuple, set)): - return type(obj)(remove_none(x) for x in obj if x is not None) - elif isinstance(obj, dict): - return type(obj)((remove_none(k), remove_none(v)) - for k, v in iteritems(obj) if k is not None and v is not None) - else: - return obj From eb90b907e7d19a087746f57794103f5c545ee413 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Fri, 5 Jun 2015 17:02:38 +0800 Subject: [PATCH 14/14] rebuild python client --- .../src/main/resources/python/api.mustache | 6 +++++- .../src/main/resources/python/api_client.mustache | 5 +---- .../src/main/resources/python/configuration.mustache | 7 ++++--- .../SwaggerPetstore-python/SwaggerPetstore/api_client.py | 5 +---- .../SwaggerPetstore/apis/pet_api.py | 9 +++++---- .../SwaggerPetstore/apis/store_api.py | 6 +++++- .../SwaggerPetstore/apis/user_api.py | 6 +++++- .../SwaggerPetstore/configuration.py | 9 ++++++--- .../python/SwaggerPetstore-python/tests/test_pet_api.py | 4 ++-- 9 files changed, 34 insertions(+), 23 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/python/api.mustache b/modules/swagger-codegen/src/main/resources/python/api.mustache index a91797d71b6..7153ee2b040 100644 --- a/modules/swagger-codegen/src/main/resources/python/api.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api.mustache @@ -27,6 +27,7 @@ import os # python 2 and python 3 compatibility library from six import iteritems +from .. import configuration from ..api_client import ApiClient {{#operations}} @@ -36,7 +37,10 @@ class {{classname}}(object): if api_client: self.api_client = api_client else: - self.api_client = config.api_client + if not configuration.api_client: + configuration.api_client = ApiClient('{{basePath}}') + self.api_client = configuration.api_client + {{#operation}} def {{nickname}}(self, {{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}**kwargs): """ diff --git a/modules/swagger-codegen/src/main/resources/python/api_client.mustache b/modules/swagger-codegen/src/main/resources/python/api_client.mustache index 75c6e336a07..b8cc4cc2a84 100644 --- a/modules/swagger-codegen/src/main/resources/python/api_client.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api_client.mustache @@ -67,7 +67,7 @@ class ApiClient(object): if self.cookie: header_params['Cookie'] = self.cookie if header_params: - header_params = ApiClient.sanitize_for_serialization(header_params) + header_params = self.sanitize_for_serialization(header_params) # path parameters if path_params: @@ -301,6 +301,3 @@ class ApiClient(object): querys[auth_setting['key']] = auth_setting['value'] else: raise ValueError('Authentication token must be in `query` or `header`') - - - diff --git a/modules/swagger-codegen/src/main/resources/python/configuration.mustache b/modules/swagger-codegen/src/main/resources/python/configuration.mustache index 928aac21edf..d3a7093a02a 100644 --- a/modules/swagger-codegen/src/main/resources/python/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/python/configuration.mustache @@ -1,7 +1,7 @@ +from __future__ import absolute_import import base64 import urllib3 - def get_api_key_with_prefix(key): global api_key global api_key_prefix @@ -35,11 +35,12 @@ def auth_settings(): {{/isBasic}}{{/authMethods}} } - - # Default Base url host = "{{basePath}}" +# Default api client +api_client = None + # Authentication settings api_key = {} diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py index 75c6e336a07..b8cc4cc2a84 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/api_client.py @@ -67,7 +67,7 @@ class ApiClient(object): if self.cookie: header_params['Cookie'] = self.cookie if header_params: - header_params = ApiClient.sanitize_for_serialization(header_params) + header_params = self.sanitize_for_serialization(header_params) # path parameters if path_params: @@ -301,6 +301,3 @@ class ApiClient(object): querys[auth_setting['key']] = auth_setting['value'] else: raise ValueError('Authentication token must be in `query` or `header`') - - - 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 ec0a8c0f5fd..549b7fe956f 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 @@ -27,6 +27,7 @@ import os # python 2 and python 3 compatibility library from six import iteritems +from .. import configuration from ..api_client import ApiClient class PetApi(object): @@ -35,7 +36,10 @@ class PetApi(object): if api_client: self.api_client = api_client else: - self.api_client = config.api_client + if not configuration.api_client: + configuration.api_client = ApiClient('http://petstore.swagger.io/v2') + self.api_client = configuration.api_client + def update_pet(self, **kwargs): """ @@ -496,6 +500,3 @@ class PetApi(object): - - - 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 03f0b2ec170..bd2fd9b8080 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 @@ -27,6 +27,7 @@ import os # python 2 and python 3 compatibility library from six import iteritems +from .. import configuration from ..api_client import ApiClient class StoreApi(object): @@ -35,7 +36,10 @@ class StoreApi(object): if api_client: self.api_client = api_client else: - self.api_client = config.api_client + if not configuration.api_client: + configuration.api_client = ApiClient('http://petstore.swagger.io/v2') + self.api_client = configuration.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 223566b8684..5aca93818d0 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 @@ -27,6 +27,7 @@ import os # python 2 and python 3 compatibility library from six import iteritems +from .. import configuration from ..api_client import ApiClient class UserApi(object): @@ -35,7 +36,10 @@ class UserApi(object): if api_client: self.api_client = api_client else: - self.api_client = config.api_client + if not configuration.api_client: + configuration.api_client = ApiClient('http://petstore.swagger.io/v2') + self.api_client = configuration.api_client + def create_user(self, **kwargs): """ diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/configuration.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/configuration.py index 46e60ccf417..b5bd0a64e0a 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/configuration.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/configuration.py @@ -1,7 +1,7 @@ +from __future__ import absolute_import import base64 import urllib3 - def get_api_key_with_prefix(key): global api_key global api_key_prefix @@ -28,14 +28,17 @@ def auth_settings(): } - - # Default Base url host = "http://petstore.swagger.io/v2" +# Default api client +api_client = None + # Authentication settings api_key = {} api_key_prefix = {} username = '' password = '' + + 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 2c50ac861a1..3bf18607729 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 @@ -61,11 +61,11 @@ class PetApiTests(unittest.TestCase): # 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) + self.assertEqual(pet_api.api_client, SwaggerPetstore.configuration.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) + self.assertNotEqual(pet_api3.api_client, SwaggerPetstore.configuration.api_client) # customized pet api not using the old pet api's api client self.assertNotEqual(pet_api3.api_client, pet_api2.api_client)