From 983edbd85b6f55919c9286b756c5e1df86b16731 Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Fri, 19 Sep 2014 09:44:08 -0700 Subject: [PATCH 1/2] Adding in the ability to post forms using swagger codegen generate bindings for python --- src/main/resources/python/api.mustache | 14 +++++++++++++- src/main/resources/python/swagger.mustache | 7 +++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/resources/python/api.mustache b/src/main/resources/python/api.mustache index 5db17906c63..05c11a08bda 100644 --- a/src/main/resources/python/api.mustache +++ b/src/main/resources/python/api.mustache @@ -58,6 +58,8 @@ class {{classname}}(object): queryParams = {} headerParams = {} + formParams = {} + bodyParam = None {{#queryParams}} if ('{{paramName}}' in params): @@ -76,7 +78,17 @@ class {{classname}}(object): replacement) {{/pathParams}} - postData = (params['body'] if 'body' in params else None) + {{#formParams}} + headerParams['Content-type'] = 'application/x-www-form-urlencoded' + if ('{{paramName}}' in params): + formParams['{{paramName}}'] = params['{{paramName}}'] + {{/formParams}} + + {{#bodyParam}} + bodyParam = params['body'] + {{/bodyParam}} + + postData = (formParams if formParams else bodyParam) response = self.apiClient.callAPI(resourcePath, method, queryParams, postData, headerParams) diff --git a/src/main/resources/python/swagger.mustache b/src/main/resources/python/swagger.mustache index e62f854c7bf..b4e1fab3595 100644 --- a/src/main/resources/python/swagger.mustache +++ b/src/main/resources/python/swagger.mustache @@ -60,9 +60,12 @@ class ApiClient: elif method in ['POST', 'PUT', 'DELETE']: if postData: - headers['Content-type'] = 'application/json' data = self.sanitizeForSerialization(postData) - data = json.dumps(data) + if 'Content-type' not in headers: + headers['Content-type'] = 'application/json' + data = json.dumps(data) + else: + data = urllib.urlencode(data) else: raise Exception('Method ' + method + ' is not recognized.') From b8f3201dbacfce8bd16562800c39bb975df10afa Mon Sep 17 00:00:00 2001 From: James Ebentier Date: Fri, 19 Sep 2014 10:01:14 -0700 Subject: [PATCH 2/2] fixing a minor generation issue in api.mustashe to make sure that python scripts are as clean as possible for apis --- src/main/resources/python/api.mustache | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/resources/python/api.mustache b/src/main/resources/python/api.mustache index 05c11a08bda..7bfa22b6fc2 100644 --- a/src/main/resources/python/api.mustache +++ b/src/main/resources/python/api.mustache @@ -79,13 +79,15 @@ class {{classname}}(object): {{/pathParams}} {{#formParams}} - headerParams['Content-type'] = 'application/x-www-form-urlencoded' if ('{{paramName}}' in params): formParams['{{paramName}}'] = params['{{paramName}}'] {{/formParams}} + if formParams: + headerParams['Content-type'] = 'application/x-www-form-urlencoded' {{#bodyParam}} - bodyParam = params['body'] + if ('{{paramName}}' in params): + bodyParam = params['{{paramName}}'] {{/bodyParam}} postData = (formParams if formParams else bodyParam)