forked from loafle/openapi-generator-original
MultipartForm: getting the file uploading working
This commit is contained in:
parent
63bb20238b
commit
e955c9dcfd
@ -59,7 +59,7 @@ class {{classname}}(object):
|
|||||||
queryParams = {}
|
queryParams = {}
|
||||||
headerParams = {}
|
headerParams = {}
|
||||||
formParams = {}
|
formParams = {}
|
||||||
files = []
|
files = {}
|
||||||
bodyParam = None
|
bodyParam = None
|
||||||
|
|
||||||
headerParams['Content-type'] = '{{#consumes}}{{mediaType}}{{#hasMore}},{{/hasMore}}{{/consumes}}'
|
headerParams['Content-type'] = '{{#consumes}}{{mediaType}}{{#hasMore}},{{/hasMore}}{{/consumes}}'
|
||||||
@ -87,9 +87,9 @@ class {{classname}}(object):
|
|||||||
formParams['{{paramName}}'] = params['{{paramName}}']
|
formParams['{{paramName}}'] = params['{{paramName}}']
|
||||||
{{/notFile}}
|
{{/notFile}}
|
||||||
{{#isFile}}
|
{{#isFile}}
|
||||||
files.append(('{{paramName}}', params['{{paramName}}']))
|
files['{{paramName}}'] = params['{{paramName}}']
|
||||||
{{/isFile}}
|
{{/isFile}}
|
||||||
{{newline}}
|
{{#hasMore}}{{newline}}{{/hasMore}}
|
||||||
{{/formParams}}
|
{{/formParams}}
|
||||||
|
|
||||||
{{#bodyParam}}
|
{{#bodyParam}}
|
||||||
|
@ -12,9 +12,9 @@ import urllib2
|
|||||||
import httplib
|
import httplib
|
||||||
import json
|
import json
|
||||||
import datetime
|
import datetime
|
||||||
import mimetools
|
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import itertools
|
import random
|
||||||
|
import string
|
||||||
|
|
||||||
from models import *
|
from models import *
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ class ApiClient:
|
|||||||
self.apiKey = apiKey
|
self.apiKey = apiKey
|
||||||
self.apiServer = apiServer
|
self.apiServer = apiServer
|
||||||
self.cookie = None
|
self.cookie = None
|
||||||
self.boundary = mimetools.choose_boundary()
|
self.boundary = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(30))
|
||||||
|
|
||||||
def callAPI(self, resourcePath, method, queryParams, postData,
|
def callAPI(self, resourcePath, method, queryParams, postData,
|
||||||
headerParams=None, files=None):
|
headerParams=None, files=None):
|
||||||
@ -40,7 +40,6 @@ class ApiClient:
|
|||||||
for param, value in headerParams.iteritems():
|
for param, value in headerParams.iteritems():
|
||||||
headers[param] = value
|
headers[param] = value
|
||||||
|
|
||||||
#headers['Content-type'] = 'application/json'
|
|
||||||
headers['api_key'] = self.apiKey
|
headers['api_key'] = self.apiKey
|
||||||
|
|
||||||
if self.cookie:
|
if self.cookie:
|
||||||
@ -68,9 +67,10 @@ class ApiClient:
|
|||||||
if 'Content-type' not in headers:
|
if 'Content-type' not in headers:
|
||||||
headers['Content-type'] = 'application/json'
|
headers['Content-type'] = 'application/json'
|
||||||
data = json.dumps(postData)
|
data = json.dumps(postData)
|
||||||
elif headers['Content-type'] == 'multipart/form':
|
elif headers['Content-type'] == 'multipart/form-data':
|
||||||
headers['Content-type'] = 'multipart/form; boundry=%s' % self.boundary
|
|
||||||
data = self.buildMultipartFormData(postData, files)
|
data = self.buildMultipartFormData(postData, files)
|
||||||
|
headers['Content-type'] = 'multipart/form-data; boundary={0}'.format(self.boundary)
|
||||||
|
headers['Content-length'] = str(len(data))
|
||||||
else:
|
else:
|
||||||
data = urllib.urlencode(postData)
|
data = urllib.urlencode(postData)
|
||||||
|
|
||||||
@ -135,29 +135,36 @@ class ApiClient:
|
|||||||
data = json.dumps(postData.__dict__)
|
data = json.dumps(postData.__dict__)
|
||||||
|
|
||||||
def buildMultipartFormData(self, postData, files):
|
def buildMultipartFormData(self, postData, files):
|
||||||
parts = []
|
def escape_quotes(s):
|
||||||
part_boundary = '--' + self.boundary
|
return s.replace('"', '\\"')
|
||||||
parts.extend(
|
|
||||||
[part_boundary, 'Content-Disposition: form-data; name="%s"' % name, '', value]
|
|
||||||
for name, value in postData.items()
|
|
||||||
)
|
|
||||||
|
|
||||||
file_tuples = []
|
lines = []
|
||||||
for fieldname, file_path in files:
|
|
||||||
filename = file_path.split('/')[-1]
|
for name, value in postData.items():
|
||||||
f = open(file_path, 'r')
|
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'
|
mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
|
||||||
body = f.read()
|
lines.extend((
|
||||||
file_tuples.append((fieldname, filename, mimetype, body))
|
'--{0}'.format(self.boundary),
|
||||||
parts.extend(
|
'Content-Disposition: form-data; name="{0}"; filename="{1}"'.format(escape_quotes(name), escape_quotes(filename)),
|
||||||
[part_boundary, 'Content-Disposition file; name="%s"; filename="%s"' % (fieldname, filename), 'Content-Type: %s' % content_type, '', body]
|
'Content-Type: {0}'.format(mimetype),
|
||||||
for fieldname, filename, content_type, body in file_tuples
|
'',
|
||||||
)
|
f.read()
|
||||||
|
))
|
||||||
|
|
||||||
flattened = list(itertools.chain(*parts))
|
lines.extend((
|
||||||
flattened.append('--' + self.boundary + '--')
|
'--{0}--'.format(self.boundary),
|
||||||
flattened.append('')
|
''
|
||||||
return '\r\n'.join(flattened)
|
))
|
||||||
|
return '\r\n'.join(lines)
|
||||||
|
|
||||||
def deserialize(self, obj, objClass):
|
def deserialize(self, obj, objClass):
|
||||||
"""Derialize a JSON string into an object.
|
"""Derialize a JSON string into an object.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user