From 19a75ac13d2990402e0b7f226478268aa572073f Mon Sep 17 00:00:00 2001 From: Bartosz Kryza Date: Tue, 6 Sep 2016 10:20:45 +0200 Subject: [PATCH 1/3] Extended request generation logic with support for serialized body content types other than Json --- .../src/main/resources/python/rest.mustache | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/python/rest.mustache b/modules/swagger-codegen/src/main/resources/python/rest.mustache index d5133ac84f4..86e2e4a9b7e 100644 --- a/modules/swagger-codegen/src/main/resources/python/rest.mustache +++ b/modules/swagger-codegen/src/main/resources/python/rest.mustache @@ -122,12 +122,12 @@ class RESTClientObject(object): r = self.pool_manager.request(method, url, body=request_body, headers=headers) - if headers['Content-Type'] == 'application/x-www-form-urlencoded': + elif headers['Content-Type'] == 'application/x-www-form-urlencoded': r = self.pool_manager.request(method, url, fields=post_params, encode_multipart=False, headers=headers) - if headers['Content-Type'] == 'multipart/form-data': + elif headers['Content-Type'] == 'multipart/form-data': # must del headers['Content-Type'], or the correct Content-Type # which generated by urllib3 will be overwritten. del headers['Content-Type'] @@ -135,6 +135,19 @@ class RESTClientObject(object): fields=post_params, encode_multipart=True, headers=headers) + # Pass a `string` parameter directly in the body to support + # other content types than Json when `body` argument is provided + # in serialized form + elif isinstance(body, basestring): + request_body = body + r = self.pool_manager.request(method, url, + body=request_body, + headers=headers) + else: + # Cannot generate the request from given parameters + msg = """Cannot prepare a request message for provided arguments. + Please check that your arguments match declared content type.""" + raise ApiException(status=0, reason=msg) # For `GET`, `HEAD` else: r = self.pool_manager.request(method, url, From 0bf7de669c6b920e56487d99705a5d482242a7b4 Mon Sep 17 00:00:00 2001 From: Bartosz Kryza Date: Tue, 6 Sep 2016 10:22:28 +0200 Subject: [PATCH 2/3] Updated the Petstore Python client tests --- .../client/petstore/python/petstore_api/rest.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/samples/client/petstore/python/petstore_api/rest.py b/samples/client/petstore/python/petstore_api/rest.py index e5bfae61aa1..2a9e97d5490 100644 --- a/samples/client/petstore/python/petstore_api/rest.py +++ b/samples/client/petstore/python/petstore_api/rest.py @@ -142,12 +142,12 @@ def request(self, method, url, query_params=None, headers=None, r = self.pool_manager.request(method, url, body=request_body, headers=headers) - if headers['Content-Type'] == 'application/x-www-form-urlencoded': + elif headers['Content-Type'] == 'application/x-www-form-urlencoded': r = self.pool_manager.request(method, url, fields=post_params, encode_multipart=False, headers=headers) - if headers['Content-Type'] == 'multipart/form-data': + elif headers['Content-Type'] == 'multipart/form-data': # must del headers['Content-Type'], or the correct Content-Type # which generated by urllib3 will be overwritten. del headers['Content-Type'] @@ -155,6 +155,19 @@ def request(self, method, url, query_params=None, headers=None, fields=post_params, encode_multipart=True, headers=headers) + # Pass a `string` parameter directly in the body to support + # other content types than Json when `body` argument is provided + # in serialized form + elif isinstance(body, basestring): + request_body = body + r = self.pool_manager.request(method, url, + body=request_body, + headers=headers) + else: + # Cannot generate the request from given parameters + msg = """Cannot prepare a request message for provided arguments. + Please check that your arguments match declared content type.""" + raise ApiException(status=0, reason=msg) # For `GET`, `HEAD` else: r = self.pool_manager.request(method, url, From 3516614fc1bad8f0ac69f83ed92e40dba57f9eb6 Mon Sep 17 00:00:00 2001 From: Bartosz Kryza Date: Fri, 9 Sep 2016 14:01:08 +0200 Subject: [PATCH 3/3] Fixed body content type identification for strings --- modules/swagger-codegen/src/main/resources/python/rest.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/swagger-codegen/src/main/resources/python/rest.mustache b/modules/swagger-codegen/src/main/resources/python/rest.mustache index 86e2e4a9b7e..9bdddc91904 100644 --- a/modules/swagger-codegen/src/main/resources/python/rest.mustache +++ b/modules/swagger-codegen/src/main/resources/python/rest.mustache @@ -138,7 +138,7 @@ class RESTClientObject(object): # Pass a `string` parameter directly in the body to support # other content types than Json when `body` argument is provided # in serialized form - elif isinstance(body, basestring): + elif isinstance(body, str): request_body = body r = self.pool_manager.request(method, url, body=request_body,