Skip to content

Commit b6d206a

Browse files
committed
Merge pull request #774 from geekerzp/develop_2.0_python_contenttype
Minor improvement to Python API Client 'accept' and 'content-type' header
2 parents 5527932 + 8d0efd8 commit b6d206a

File tree

7 files changed

+252
-86
lines changed

7 files changed

+252
-86
lines changed

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

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

3030
from ..util import remove_none
3131

32+
from ..swagger import ApiClient
33+
3234
{{#operations}}
3335
class {{classname}}(object):
3436

@@ -68,11 +70,13 @@ class {{classname}}(object):
6870
files = remove_none(dict({{#formParams}}{{#isFile}}{{baseName}}=params.get('{{paramName}}'){{#hasMore}}, {{/hasMore}}{{/isFile}}{{/formParams}}))
6971
body_params = {{#bodyParam}}params.get('{{paramName}}'){{/bodyParam}}{{^bodyParam}}None{{/bodyParam}}
7072

71-
accepts = [{{#produces}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/produces}}]
72-
header_params['Accept'] = ', '.join(accepts)
73+
# HTTP header `Accept`
74+
header_params['Accept'] = ApiClient.select_header_accept([{{#produces}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/produces}}])
75+
if not header_params['Accept']:
76+
del header_params['Accept']
7377

74-
content_types = [{{#consumes}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}]
75-
header_params['Content-Type'] = content_types[0] if len(content_types) > 0 else 'application/json'
78+
# HTTP header `Content-Type`
79+
header_params['Content-Type'] = ApiClient.select_header_content_type([{{#consumes}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}])
7680

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

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class ApiClient(object):
105105
def to_path_value(self, obj):
106106
"""
107107
Convert a string or object to a path-friendly value
108-
108+
109109
:param obj: object or string value
110110
111111
:return string: quoted value
@@ -253,7 +253,32 @@ class ApiClient(object):
253253

254254
return params
255255

256+
@staticmethod
257+
def select_header_accept(accepts):
258+
"""
259+
Return `Accept` based on an array of accepts provided
260+
"""
261+
if not accepts:
262+
return
263+
264+
accepts = list(map(lambda x: x.lower(), accepts))
256265

266+
if 'application/json' in accepts:
267+
return 'application/json'
268+
else:
269+
return ', '.join(accepts)
257270

271+
@staticmethod
272+
def select_header_content_type(content_types):
273+
"""
274+
Return `Content-Type` baseed on an array of content_types provided
275+
"""
276+
if not content_types:
277+
return 'application/json'
258278

279+
content_types = list(map(lambda x: x.lower(), content_types))
259280

281+
if 'application/json' in content_types:
282+
return 'application/json'
283+
else:
284+
return content_types[0]

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

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
from ..util import remove_none
3131

32+
from ..swagger import ApiClient
33+
3234
class PetApi(object):
3335

3436
def __init__(self, api_client):
@@ -63,11 +65,13 @@ def update_pet(self, **kwargs):
6365
files = remove_none(dict())
6466
body_params = params.get('body')
6567

66-
accepts = ['application/json', 'application/xml']
67-
header_params['Accept'] = ', '.join(accepts)
68+
# HTTP header `Accept`
69+
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
70+
if not header_params['Accept']:
71+
del header_params['Accept']
6872

69-
content_types = ['application/json', 'application/xml']
70-
header_params['Content-Type'] = content_types[0] if len(content_types) > 0 else 'application/json'
73+
# HTTP header `Content-Type`
74+
header_params['Content-Type'] = ApiClient.select_header_content_type(['application/json', 'application/xml'])
7175

7276
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
7377
body=body_params, post_params=form_params, files=files,
@@ -102,11 +106,13 @@ def add_pet(self, **kwargs):
102106
files = remove_none(dict())
103107
body_params = params.get('body')
104108

105-
accepts = ['application/json', 'application/xml']
106-
header_params['Accept'] = ', '.join(accepts)
109+
# HTTP header `Accept`
110+
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
111+
if not header_params['Accept']:
112+
del header_params['Accept']
107113

108-
content_types = ['application/json', 'application/xml']
109-
header_params['Content-Type'] = content_types[0] if len(content_types) > 0 else 'application/json'
114+
# HTTP header `Content-Type`
115+
header_params['Content-Type'] = ApiClient.select_header_content_type(['application/json', 'application/xml'])
110116

111117
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
112118
body=body_params, post_params=form_params, files=files,
@@ -141,11 +147,13 @@ def find_pets_by_status(self, **kwargs):
141147
files = remove_none(dict())
142148
body_params = None
143149

144-
accepts = ['application/json', 'application/xml']
145-
header_params['Accept'] = ', '.join(accepts)
150+
# HTTP header `Accept`
151+
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
152+
if not header_params['Accept']:
153+
del header_params['Accept']
146154

147-
content_types = []
148-
header_params['Content-Type'] = content_types[0] if len(content_types) > 0 else 'application/json'
155+
# HTTP header `Content-Type`
156+
header_params['Content-Type'] = ApiClient.select_header_content_type([])
149157

150158
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
151159
body=body_params, post_params=form_params, files=files,
@@ -182,11 +190,13 @@ def find_pets_by_tags(self, **kwargs):
182190
files = remove_none(dict())
183191
body_params = None
184192

185-
accepts = ['application/json', 'application/xml']
186-
header_params['Accept'] = ', '.join(accepts)
193+
# HTTP header `Accept`
194+
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
195+
if not header_params['Accept']:
196+
del header_params['Accept']
187197

188-
content_types = []
189-
header_params['Content-Type'] = content_types[0] if len(content_types) > 0 else 'application/json'
198+
# HTTP header `Content-Type`
199+
header_params['Content-Type'] = ApiClient.select_header_content_type([])
190200

191201
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
192202
body=body_params, post_params=form_params, files=files,
@@ -227,11 +237,13 @@ def get_pet_by_id(self, pet_id, **kwargs):
227237
files = remove_none(dict())
228238
body_params = None
229239

230-
accepts = ['application/json', 'application/xml']
231-
header_params['Accept'] = ', '.join(accepts)
240+
# HTTP header `Accept`
241+
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
242+
if not header_params['Accept']:
243+
del header_params['Accept']
232244

233-
content_types = []
234-
header_params['Content-Type'] = content_types[0] if len(content_types) > 0 else 'application/json'
245+
# HTTP header `Content-Type`
246+
header_params['Content-Type'] = ApiClient.select_header_content_type([])
235247

236248
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
237249
body=body_params, post_params=form_params, files=files,
@@ -274,11 +286,13 @@ def update_pet_with_form(self, pet_id, **kwargs):
274286
files = remove_none(dict())
275287
body_params = None
276288

277-
accepts = ['application/json', 'application/xml']
278-
header_params['Accept'] = ', '.join(accepts)
289+
# HTTP header `Accept`
290+
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
291+
if not header_params['Accept']:
292+
del header_params['Accept']
279293

280-
content_types = ['application/x-www-form-urlencoded']
281-
header_params['Content-Type'] = content_types[0] if len(content_types) > 0 else 'application/json'
294+
# HTTP header `Content-Type`
295+
header_params['Content-Type'] = ApiClient.select_header_content_type(['application/x-www-form-urlencoded'])
282296

283297
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
284298
body=body_params, post_params=form_params, files=files,
@@ -318,11 +332,13 @@ def delete_pet(self, pet_id, **kwargs):
318332
files = remove_none(dict())
319333
body_params = None
320334

321-
accepts = ['application/json', 'application/xml']
322-
header_params['Accept'] = ', '.join(accepts)
335+
# HTTP header `Accept`
336+
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
337+
if not header_params['Accept']:
338+
del header_params['Accept']
323339

324-
content_types = []
325-
header_params['Content-Type'] = content_types[0] if len(content_types) > 0 else 'application/json'
340+
# HTTP header `Content-Type`
341+
header_params['Content-Type'] = ApiClient.select_header_content_type([])
326342

327343
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
328344
body=body_params, post_params=form_params, files=files,
@@ -363,11 +379,13 @@ def upload_file(self, pet_id, **kwargs):
363379
files = remove_none(dict(file=params.get('file')))
364380
body_params = None
365381

366-
accepts = ['application/json', 'application/xml']
367-
header_params['Accept'] = ', '.join(accepts)
382+
# HTTP header `Accept`
383+
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
384+
if not header_params['Accept']:
385+
del header_params['Accept']
368386

369-
content_types = ['multipart/form-data']
370-
header_params['Content-Type'] = content_types[0] if len(content_types) > 0 else 'application/json'
387+
# HTTP header `Content-Type`
388+
header_params['Content-Type'] = ApiClient.select_header_content_type(['multipart/form-data'])
371389

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

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

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
from ..util import remove_none
3131

32+
from ..swagger import ApiClient
33+
3234
class StoreApi(object):
3335

3436
def __init__(self, api_client):
@@ -62,11 +64,13 @@ def get_inventory(self, **kwargs):
6264
files = remove_none(dict())
6365
body_params = None
6466

65-
accepts = ['application/json', 'application/xml']
66-
header_params['Accept'] = ', '.join(accepts)
67+
# HTTP header `Accept`
68+
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
69+
if not header_params['Accept']:
70+
del header_params['Accept']
6771

68-
content_types = []
69-
header_params['Content-Type'] = content_types[0] if len(content_types) > 0 else 'application/json'
72+
# HTTP header `Content-Type`
73+
header_params['Content-Type'] = ApiClient.select_header_content_type([])
7074

7175
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
7276
body=body_params, post_params=form_params, files=files,
@@ -103,11 +107,13 @@ def place_order(self, **kwargs):
103107
files = remove_none(dict())
104108
body_params = params.get('body')
105109

106-
accepts = ['application/json', 'application/xml']
107-
header_params['Accept'] = ', '.join(accepts)
110+
# HTTP header `Accept`
111+
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
112+
if not header_params['Accept']:
113+
del header_params['Accept']
108114

109-
content_types = []
110-
header_params['Content-Type'] = content_types[0] if len(content_types) > 0 else 'application/json'
115+
# HTTP header `Content-Type`
116+
header_params['Content-Type'] = ApiClient.select_header_content_type([])
111117

112118
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
113119
body=body_params, post_params=form_params, files=files,
@@ -148,11 +154,13 @@ def get_order_by_id(self, order_id, **kwargs):
148154
files = remove_none(dict())
149155
body_params = None
150156

151-
accepts = ['application/json', 'application/xml']
152-
header_params['Accept'] = ', '.join(accepts)
157+
# HTTP header `Accept`
158+
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
159+
if not header_params['Accept']:
160+
del header_params['Accept']
153161

154-
content_types = []
155-
header_params['Content-Type'] = content_types[0] if len(content_types) > 0 else 'application/json'
162+
# HTTP header `Content-Type`
163+
header_params['Content-Type'] = ApiClient.select_header_content_type([])
156164

157165
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
158166
body=body_params, post_params=form_params, files=files,
@@ -193,11 +201,13 @@ def delete_order(self, order_id, **kwargs):
193201
files = remove_none(dict())
194202
body_params = None
195203

196-
accepts = ['application/json', 'application/xml']
197-
header_params['Accept'] = ', '.join(accepts)
204+
# HTTP header `Accept`
205+
header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml'])
206+
if not header_params['Accept']:
207+
del header_params['Accept']
198208

199-
content_types = []
200-
header_params['Content-Type'] = content_types[0] if len(content_types) > 0 else 'application/json'
209+
# HTTP header `Content-Type`
210+
header_params['Content-Type'] = ApiClient.select_header_content_type([])
201211

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

0 commit comments

Comments
 (0)