Skip to content

Commit 13e1465

Browse files
committed
Merge pull request #787 from geekerzp/develop_2.0_python_apiclient
[Python] Make API client more pluggable
2 parents b41e88e + 2d545c7 commit 13e1465

File tree

11 files changed

+134
-92
lines changed

11 files changed

+134
-92
lines changed

modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PythonClientCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public PythonClientCodegen() {
7171
supportingFiles.add(new SupportingFile("swagger.mustache", invokerPackage, "swagger.py"));
7272
supportingFiles.add(new SupportingFile("rest.mustache", invokerPackage, "rest.py"));
7373
supportingFiles.add(new SupportingFile("util.mustache", invokerPackage, "util.py"));
74+
supportingFiles.add(new SupportingFile("config.mustache", invokerPackage, "config.py"));
7475
supportingFiles.add(new SupportingFile("__init__package.mustache", invokerPackage, "__init__.py"));
7576
supportingFiles.add(new SupportingFile("__init__model.mustache", modelPackage.replace('.', File.separatorChar), "__init__.py"));
7677
supportingFiles.add(new SupportingFile("__init__api.mustache", apiPackage.replace('.', File.separatorChar), "__init__.py"));

modules/swagger-codegen/src/main/resources/python/api.mustache

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@ from six import iteritems
2929

3030
from ..util import remove_none
3131

32-
from ..swagger import ApiClient
32+
from .. import config
3333

3434
{{#operations}}
3535
class {{classname}}(object):
3636

37-
def __init__(self, api_client):
38-
self.api_client = api_client
37+
def __init__(self, api_client=None):
38+
if api_client:
39+
self.api_client = api_client
40+
else:
41+
self.api_client = config.api_client
3942
{{#operation}}
4043
def {{nickname}}(self, {{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}**kwargs):
4144
"""
@@ -71,12 +74,12 @@ class {{classname}}(object):
7174
body_params = {{#bodyParam}}params.get('{{paramName}}'){{/bodyParam}}{{^bodyParam}}None{{/bodyParam}}
7275

7376
# HTTP header `Accept`
74-
header_params['Accept'] = ApiClient.select_header_accept([{{#produces}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/produces}}])
77+
header_params['Accept'] = self.api_client.select_header_accept([{{#produces}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/produces}}])
7578
if not header_params['Accept']:
7679
del header_params['Accept']
7780

7881
# HTTP header `Content-Type`
79-
header_params['Content-Type'] = ApiClient.select_header_content_type([{{#consumes}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}])
82+
header_params['Content-Type'] = self.api_client.select_header_content_type([{{#consumes}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}])
8083

8184
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
8285
body=body_params, post_params=form_params, files=files,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from __future__ import absolute_import
2+
3+
from .swagger import ApiClient
4+
5+
# Configuration variables
6+
7+
api_client = ApiClient("{{basePath}}")
8+

modules/swagger-codegen/src/main/resources/python/swagger.mustache

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,28 +66,28 @@ class ApiClient(object):
6666
if self.cookie:
6767
headers['Cookie'] = self.cookie
6868
if headers:
69-
headers = ApiClient.sanitize_for_serialization(headers)
69+
headers = self.sanitize_for_serialization(headers)
7070

7171
# path parameters
7272
if path_params:
73-
path_params = ApiClient.sanitize_for_serialization(path_params)
73+
path_params = self.sanitize_for_serialization(path_params)
7474
for k, v in iteritems(path_params):
7575
replacement = quote(str(self.to_path_value(v)))
7676
resource_path = resource_path.replace('{' + k + '}', replacement)
7777

7878
# query parameters
7979
if query_params:
80-
query_params = ApiClient.sanitize_for_serialization(query_params)
80+
query_params = self.sanitize_for_serialization(query_params)
8181
query_params = {k: self.to_path_value(v) for k, v in iteritems(query_params)}
8282

8383
# post parameters
8484
if post_params:
8585
post_params = self.prepare_post_parameters(post_params, files)
86-
post_params = ApiClient.sanitize_for_serialization(post_params)
86+
post_params = self.sanitize_for_serialization(post_params)
8787

8888
# body
8989
if body:
90-
body = ApiClient.sanitize_for_serialization(body)
90+
body = self.sanitize_for_serialization(body)
9191

9292
# request url
9393
url = self.host + resource_path
@@ -115,8 +115,7 @@ class ApiClient(object):
115115
else:
116116
return str(obj)
117117

118-
@staticmethod
119-
def sanitize_for_serialization(obj):
118+
def sanitize_for_serialization(self, obj):
120119
"""
121120
Sanitize an object for Request.
122121
@@ -132,7 +131,7 @@ class ApiClient(object):
132131
elif isinstance(obj, (str, int, float, bool, tuple)):
133132
return obj
134133
elif isinstance(obj, list):
135-
return [ApiClient.sanitize_for_serialization(sub_obj) for sub_obj in obj]
134+
return [self.sanitize_for_serialization(sub_obj) for sub_obj in obj]
136135
elif isinstance(obj, (datetime.datetime, datetime.date)):
137136
return obj.isoformat()
138137
else:
@@ -145,7 +144,7 @@ class ApiClient(object):
145144
obj_dict = {obj.attribute_map[key]: val
146145
for key, val in iteritems(obj.__dict__)
147146
if key != 'swagger_types' and key != 'attribute_map' and val is not None}
148-
return {key: ApiClient.sanitize_for_serialization(val)
147+
return {key: self.sanitize_for_serialization(val)
149148
for key, val in iteritems(obj_dict)}
150149

151150
def deserialize(self, obj, obj_class):
@@ -253,8 +252,7 @@ class ApiClient(object):
253252

254253
return params
255254

256-
@staticmethod
257-
def select_header_accept(accepts):
255+
def select_header_accept(self, accepts):
258256
"""
259257
Return `Accept` based on an array of accepts provided
260258
"""
@@ -268,8 +266,7 @@ class ApiClient(object):
268266
else:
269267
return ', '.join(accepts)
270268

271-
@staticmethod
272-
def select_header_content_type(content_types):
269+
def select_header_content_type(self, content_types):
273270
"""
274271
Return `Content-Type` baseed on an array of content_types provided
275272
"""

samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/pet_api.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@
2929

3030
from ..util import remove_none
3131

32-
from ..swagger import ApiClient
32+
from .. import config
3333

3434
class PetApi(object):
3535

36-
def __init__(self, api_client):
37-
self.api_client = api_client
36+
def __init__(self, api_client=None):
37+
if api_client:
38+
self.api_client = api_client
39+
else:
40+
self.api_client = config.api_client
3841

3942
def update_pet(self, **kwargs):
4043
"""
@@ -66,12 +69,12 @@ def update_pet(self, **kwargs):
6669
body_params = params.get('body')
6770

6871
# HTTP header `Accept`
69-
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
72+
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
7073
if not header_params['Accept']:
7174
del header_params['Accept']
7275

7376
# HTTP header `Content-Type`
74-
header_params['Content-Type'] = ApiClient.select_header_content_type(['application/json', 'application/xml'])
77+
header_params['Content-Type'] = self.api_client.select_header_content_type(['application/json', 'application/xml'])
7578

7679
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
7780
body=body_params, post_params=form_params, files=files,
@@ -107,12 +110,12 @@ def add_pet(self, **kwargs):
107110
body_params = params.get('body')
108111

109112
# HTTP header `Accept`
110-
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
113+
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
111114
if not header_params['Accept']:
112115
del header_params['Accept']
113116

114117
# HTTP header `Content-Type`
115-
header_params['Content-Type'] = ApiClient.select_header_content_type(['application/json', 'application/xml'])
118+
header_params['Content-Type'] = self.api_client.select_header_content_type(['application/json', 'application/xml'])
116119

117120
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
118121
body=body_params, post_params=form_params, files=files,
@@ -148,12 +151,12 @@ def find_pets_by_status(self, **kwargs):
148151
body_params = None
149152

150153
# HTTP header `Accept`
151-
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
154+
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
152155
if not header_params['Accept']:
153156
del header_params['Accept']
154157

155158
# HTTP header `Content-Type`
156-
header_params['Content-Type'] = ApiClient.select_header_content_type([])
159+
header_params['Content-Type'] = self.api_client.select_header_content_type([])
157160

158161
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
159162
body=body_params, post_params=form_params, files=files,
@@ -191,12 +194,12 @@ def find_pets_by_tags(self, **kwargs):
191194
body_params = None
192195

193196
# HTTP header `Accept`
194-
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
197+
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
195198
if not header_params['Accept']:
196199
del header_params['Accept']
197200

198201
# HTTP header `Content-Type`
199-
header_params['Content-Type'] = ApiClient.select_header_content_type([])
202+
header_params['Content-Type'] = self.api_client.select_header_content_type([])
200203

201204
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
202205
body=body_params, post_params=form_params, files=files,
@@ -238,12 +241,12 @@ def get_pet_by_id(self, pet_id, **kwargs):
238241
body_params = None
239242

240243
# HTTP header `Accept`
241-
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
244+
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
242245
if not header_params['Accept']:
243246
del header_params['Accept']
244247

245248
# HTTP header `Content-Type`
246-
header_params['Content-Type'] = ApiClient.select_header_content_type([])
249+
header_params['Content-Type'] = self.api_client.select_header_content_type([])
247250

248251
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
249252
body=body_params, post_params=form_params, files=files,
@@ -287,12 +290,12 @@ def update_pet_with_form(self, pet_id, **kwargs):
287290
body_params = None
288291

289292
# HTTP header `Accept`
290-
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
293+
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
291294
if not header_params['Accept']:
292295
del header_params['Accept']
293296

294297
# HTTP header `Content-Type`
295-
header_params['Content-Type'] = ApiClient.select_header_content_type(['application/x-www-form-urlencoded'])
298+
header_params['Content-Type'] = self.api_client.select_header_content_type(['application/x-www-form-urlencoded'])
296299

297300
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
298301
body=body_params, post_params=form_params, files=files,
@@ -333,12 +336,12 @@ def delete_pet(self, pet_id, **kwargs):
333336
body_params = None
334337

335338
# HTTP header `Accept`
336-
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
339+
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
337340
if not header_params['Accept']:
338341
del header_params['Accept']
339342

340343
# HTTP header `Content-Type`
341-
header_params['Content-Type'] = ApiClient.select_header_content_type([])
344+
header_params['Content-Type'] = self.api_client.select_header_content_type([])
342345

343346
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
344347
body=body_params, post_params=form_params, files=files,
@@ -380,12 +383,12 @@ def upload_file(self, pet_id, **kwargs):
380383
body_params = None
381384

382385
# HTTP header `Accept`
383-
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
386+
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
384387
if not header_params['Accept']:
385388
del header_params['Accept']
386389

387390
# HTTP header `Content-Type`
388-
header_params['Content-Type'] = ApiClient.select_header_content_type(['multipart/form-data'])
391+
header_params['Content-Type'] = self.api_client.select_header_content_type(['multipart/form-data'])
389392

390393
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
391394
body=body_params, post_params=form_params, files=files,

samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/store_api.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@
2929

3030
from ..util import remove_none
3131

32-
from ..swagger import ApiClient
32+
from .. import config
3333

3434
class StoreApi(object):
3535

36-
def __init__(self, api_client):
37-
self.api_client = api_client
36+
def __init__(self, api_client=None):
37+
if api_client:
38+
self.api_client = api_client
39+
else:
40+
self.api_client = config.api_client
3841

3942
def get_inventory(self, **kwargs):
4043
"""
@@ -65,12 +68,12 @@ def get_inventory(self, **kwargs):
6568
body_params = None
6669

6770
# HTTP header `Accept`
68-
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
71+
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
6972
if not header_params['Accept']:
7073
del header_params['Accept']
7174

7275
# HTTP header `Content-Type`
73-
header_params['Content-Type'] = ApiClient.select_header_content_type([])
76+
header_params['Content-Type'] = self.api_client.select_header_content_type([])
7477

7578
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
7679
body=body_params, post_params=form_params, files=files,
@@ -108,12 +111,12 @@ def place_order(self, **kwargs):
108111
body_params = params.get('body')
109112

110113
# HTTP header `Accept`
111-
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
114+
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
112115
if not header_params['Accept']:
113116
del header_params['Accept']
114117

115118
# HTTP header `Content-Type`
116-
header_params['Content-Type'] = ApiClient.select_header_content_type([])
119+
header_params['Content-Type'] = self.api_client.select_header_content_type([])
117120

118121
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
119122
body=body_params, post_params=form_params, files=files,
@@ -155,12 +158,12 @@ def get_order_by_id(self, order_id, **kwargs):
155158
body_params = None
156159

157160
# HTTP header `Accept`
158-
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
161+
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
159162
if not header_params['Accept']:
160163
del header_params['Accept']
161164

162165
# HTTP header `Content-Type`
163-
header_params['Content-Type'] = ApiClient.select_header_content_type([])
166+
header_params['Content-Type'] = self.api_client.select_header_content_type([])
164167

165168
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
166169
body=body_params, post_params=form_params, files=files,
@@ -202,12 +205,12 @@ def delete_order(self, order_id, **kwargs):
202205
body_params = None
203206

204207
# HTTP header `Accept`
205-
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
208+
header_params['Accept'] = self.api_client.select_header_accept(['application/json', 'application/xml'])
206209
if not header_params['Accept']:
207210
del header_params['Accept']
208211

209212
# HTTP header `Content-Type`
210-
header_params['Content-Type'] = ApiClient.select_header_content_type([])
213+
header_params['Content-Type'] = self.api_client.select_header_content_type([])
211214

212215
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
213216
body=body_params, post_params=form_params, files=files,

0 commit comments

Comments
 (0)