diff --git a/src/main/resources/php/Swagger.mustache b/src/main/resources/php/Swagger.mustache index 962cc53455f..a176b68aa0d 100644 --- a/src/main/resources/php/Swagger.mustache +++ b/src/main/resources/php/Swagger.mustache @@ -77,7 +77,8 @@ class APIClient { $headers[] = "api_key: " . $this->apiKey; } - if (is_object($postData) or is_array($postData)) { + + if (strpos($headers['Content-Type'], "multipart/form-data") < 0 and (is_object($postData) or is_array($postData))) { $postData = json_encode($this->sanitizeForSerialization($postData)); } diff --git a/src/main/resources/php/api.mustache b/src/main/resources/php/api.mustache index 30d34f556e7..43ffbaa60a5 100644 --- a/src/main/resources/php/api.mustache +++ b/src/main/resources/php/api.mustache @@ -72,7 +72,12 @@ class {{classname}} { } {{#formParams}} if(${{paramName}} != null) { + {{#notFile}} $body['{{paramName}}'] = ${{paramName}}; + {{/notFile}} + {{#isFile}} + $body['{{paramName}}'] = '@' . ${{paramName}}; + {{/isFile}} } {{/formParams}} if (empty($body)) { diff --git a/src/main/resources/python/api.mustache b/src/main/resources/python/api.mustache index 1b304e81d96..30214af6d06 100644 --- a/src/main/resources/python/api.mustache +++ b/src/main/resources/python/api.mustache @@ -59,8 +59,11 @@ class {{classname}}(object): queryParams = {} headerParams = {} formParams = {} + files = {} bodyParam = None + headerParams['Content-type'] = '{{#consumes}}{{mediaType}}{{#hasMore}},{{/hasMore}}{{/consumes}}' + {{#queryParams}} if ('{{paramName}}' in params): queryParams['{{paramName}}'] = self.apiClient.toPathValue(params['{{paramName}}']) @@ -80,10 +83,9 @@ class {{classname}}(object): {{#formParams}} if ('{{paramName}}' in params): - formParams['{{paramName}}'] = params['{{paramName}}'] + {{#notFile}}formParams['{{paramName}}'] = params['{{paramName}}']{{/notFile}}{{#isFile}}files['{{paramName}}'] = params['{{paramName}}']{{/isFile}} + {{newline}} {{/formParams}} - if formParams: - headerParams['Content-type'] = 'application/x-www-form-urlencoded' {{#bodyParam}} if ('{{paramName}}' in params): @@ -93,7 +95,7 @@ class {{classname}}(object): postData = (formParams if formParams else bodyParam) response = self.apiClient.callAPI(resourcePath, method, queryParams, - postData, headerParams) + postData, headerParams, files=files) {{#returnType}} if not response: diff --git a/src/main/resources/python/swagger.mustache b/src/main/resources/python/swagger.mustache index b4e1fab3595..59b6d38fbd6 100644 --- a/src/main/resources/python/swagger.mustache +++ b/src/main/resources/python/swagger.mustache @@ -12,6 +12,9 @@ import urllib2 import httplib import json import datetime +import mimetypes +import random +import string from models import * @@ -26,9 +29,10 @@ class ApiClient: self.apiKey = apiKey self.apiServer = apiServer self.cookie = None + self.boundary = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(30)) def callAPI(self, resourcePath, method, queryParams, postData, - headerParams=None): + headerParams=None, files=None): url = self.apiServer + resourcePath headers = {} @@ -36,7 +40,6 @@ class ApiClient: for param, value in headerParams.iteritems(): headers[param] = value - #headers['Content-type'] = 'application/json' headers['api_key'] = self.apiKey if self.cookie: @@ -60,12 +63,16 @@ class ApiClient: elif method in ['POST', 'PUT', 'DELETE']: if postData: - data = self.sanitizeForSerialization(postData) + postData = self.sanitizeForSerialization(postData) if 'Content-type' not in headers: headers['Content-type'] = 'application/json' - data = json.dumps(data) + data = json.dumps(postData) + elif headers['Content-type'] == 'multipart/form-data': + data = self.buildMultipartFormData(postData, files) + headers['Content-type'] = 'multipart/form-data; boundary={0}'.format(self.boundary) + headers['Content-length'] = str(len(data)) else: - data = urllib.urlencode(data) + data = urllib.urlencode(postData) else: raise Exception('Method ' + method + ' is not recognized.') @@ -127,6 +134,38 @@ class ApiClient: elif type(postData) not in safeToDump: data = json.dumps(postData.__dict__) + def buildMultipartFormData(self, postData, files): + def escape_quotes(s): + return s.replace('"', '\\"') + + lines = [] + + for name, value in postData.items(): + lines.extend(( + '--{0}'.format(self.boundary), + 'Content-Disposition: form-data; name="{0}"'.format(escape_quotes(name)), + '', + str(value), + )) + + for name, filepath in files.items(): + f = open(filepath, 'r') + filename = filepath.split('/')[-1] + mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream' + lines.extend(( + '--{0}'.format(self.boundary), + 'Content-Disposition: form-data; name="{0}"; filename="{1}"'.format(escape_quotes(name), escape_quotes(filename)), + 'Content-Type: {0}'.format(mimetype), + '', + f.read() + )) + + lines.extend(( + '--{0}--'.format(self.boundary), + '' + )) + return '\r\n'.join(lines) + def deserialize(self, obj, objClass): """Derialize a JSON string into an object.