@@ -12,6 +12,9 @@ import urllib2
12
12
import httplib
13
13
import json
14
14
import datetime
15
+ import mimetypes
16
+ import random
17
+ import string
15
18
16
19
from models import *
17
20
@@ -26,17 +29,17 @@ class ApiClient:
26
29
self .apiKey = apiKey
27
30
self .apiServer = apiServer
28
31
self .cookie = None
32
+ self .boundary = '' .join (random .choice (string .ascii_letters + string .digits ) for _ in range (30 ))
29
33
30
34
def callAPI (self , resourcePath , method , queryParams , postData ,
31
- headerParams = None ):
35
+ headerParams = None , files = None ):
32
36
33
37
url = self .apiServer + resourcePath
34
38
headers = {}
35
39
if headerParams :
36
40
for param , value in headerParams .iteritems ():
37
41
headers [param ] = value
38
42
39
- #headers['Content-type'] = 'application/json'
40
43
headers ['api_key' ] = self .apiKey
41
44
42
45
if self .cookie :
@@ -60,12 +63,16 @@ class ApiClient:
60
63
elif method in ['POST' , 'PUT' , 'DELETE' ]:
61
64
62
65
if postData :
63
- data = self .sanitizeForSerialization (postData )
66
+ postData = self .sanitizeForSerialization (postData )
64
67
if 'Content-type' not in headers :
65
68
headers ['Content-type' ] = 'application/json'
66
- data = json .dumps (data )
69
+ data = json .dumps (postData )
70
+ elif headers ['Content-type' ] == 'multipart/form-data' :
71
+ data = self .buildMultipartFormData (postData , files )
72
+ headers ['Content-type' ] = 'multipart/form-data; boundary={0}' .format (self .boundary )
73
+ headers ['Content-length' ] = str (len (data ))
67
74
else :
68
- data = urllib .urlencode (data )
75
+ data = urllib .urlencode (postData )
69
76
70
77
else :
71
78
raise Exception ('Method ' + method + ' is not recognized.' )
@@ -127,6 +134,38 @@ class ApiClient:
127
134
elif type (postData ) not in safeToDump :
128
135
data = json .dumps (postData .__dict__ )
129
136
137
+ def buildMultipartFormData (self , postData , files ):
138
+ def escape_quotes (s ):
139
+ return s .replace ('"' , '\\ "' )
140
+
141
+ lines = []
142
+
143
+ for name , value in postData .items ():
144
+ lines .extend ((
145
+ '--{0}' .format (self .boundary ),
146
+ 'Content-Disposition: form-data; name="{0}"' .format (escape_quotes (name )),
147
+ '' ,
148
+ str (value ),
149
+ ))
150
+
151
+ for name , filepath in files .items ():
152
+ f = open (filepath , 'r' )
153
+ filename = filepath .split ('/' )[- 1 ]
154
+ mimetype = mimetypes .guess_type (filename )[0 ] or 'application/octet-stream'
155
+ lines .extend ((
156
+ '--{0}' .format (self .boundary ),
157
+ 'Content-Disposition: form-data; name="{0}"; filename="{1}"' .format (escape_quotes (name ), escape_quotes (filename )),
158
+ 'Content-Type: {0}' .format (mimetype ),
159
+ '' ,
160
+ f .read ()
161
+ ))
162
+
163
+ lines .extend ((
164
+ '--{0}--' .format (self .boundary ),
165
+ ''
166
+ ))
167
+ return '\r \n ' .join (lines )
168
+
130
169
def deserialize (self , obj , objClass ):
131
170
"""Derialize a JSON string into an object.
132
171
0 commit comments