@@ -223,11 +223,12 @@ def send_request(self, method, path, data=None):
223
223
raise ConnectionError (json .dumps (self ._verify_response (None , method , full_path , None )))
224
224
return self ._verify_response (response , method , full_path , rdata )
225
225
226
- def send_file_request (self , method , path , file = None , data = None , remote_path = None , file_key = "file" ):
226
+ def send_file_request (self , method , path , file = None , data = None , remote_path = None , file_key = "file" , file_ext = None ):
227
227
"""This method handles file download and upload operations
228
228
:arg method (str): Method can be GET or POST
229
229
:arg path (str): Path should be the resource path
230
230
:arg file (str): The absolute file path of the target file
231
+ :arg file_ext (str): The file extension, with leading dot, to be used for the file. If file has already an extension, it will be replaced
231
232
:arg data (dict): Data should be the dictionary object
232
233
:arg remote_path (str): Remote directory path to download/upload the file object
233
234
@@ -255,6 +256,14 @@ def send_file_request(self, method, path, file=None, data=None, remote_path=None
255
256
if method is not None :
256
257
self .method = method
257
258
259
+ # If file_ext is provided, replace the file extension (if present) or add it
260
+ if file_ext is not None :
261
+ if not file_ext .startswith ("." ) or file_ext not in set (mimetypes .types_map .keys ()):
262
+ raise ValueError ("Invalid file extension provided. Please provide a valid file extension, with leading dot" )
263
+ filename = os .path .splitext (os .path .basename (file ))[0 ] + file_ext
264
+ else :
265
+ filename = os .path .basename (file )
266
+
258
267
try :
259
268
# create data field
260
269
data ["uploadedFileName" ] = os .path .basename (file )
@@ -267,11 +276,11 @@ def send_file_request(self, method, path, file=None, data=None, remote_path=None
267
276
try :
268
277
# create fields for MultipartEncoder
269
278
if remote_path :
270
- fields = dict (rdir = remote_path , name = (os . path . basename ( file ) , open (file , "rb" ), mimetypes .guess_type (file )))
279
+ fields = dict (rdir = remote_path , name = (filename , open (file , "rb" ), mimetypes .guess_type (filename )))
271
280
elif file_key == "importfile" :
272
- fields = dict (spec = (json .dumps (data )), importfile = (os . path . basename ( file ) , open (file , "rb" ), mimetypes .guess_type (file )))
281
+ fields = dict (spec = (json .dumps (data )), importfile = (filename , open (file , "rb" ), mimetypes .guess_type (filename )))
273
282
else :
274
- fields = dict (data = ("data.json" , data_str , "application/json" ), file = (os . path . basename ( file ) , open (file , "rb" ), mimetypes .guess_type (file )))
283
+ fields = dict (data = ("data.json" , data_str , "application/json" ), file = (filename , open (file , "rb" ), mimetypes .guess_type (filename )))
275
284
276
285
if not HAS_MULTIPART_ENCODER :
277
286
if sys .version_info .major == 2 :
0 commit comments