12
12
from builtins import object
13
13
from six import string_types
14
14
15
+ from requests_toolbelt import MultipartEncoder
16
+
15
17
from ciscosparkapi .exceptions import ciscosparkapiException
16
- from ciscosparkapi .helper import generator_container
18
+ from ciscosparkapi .helper import generator_container , is_web_url , \
19
+ is_local_file , open_local_file
17
20
from ciscosparkapi .restsession import RestSession
18
21
from ciscosparkapi .sparkdata import SparkData
19
22
@@ -196,7 +199,11 @@ def create(self, roomId=None, toPersonId=None, toPersonEmail=None,
196
199
specified this parameter may be optionally used to provide
197
200
alternate text forUI clients that do not support rich text.
198
201
markdown(string_types): The message, in markdown format.
199
- files(list): A list of URL references for the message attachments.
202
+ files(list): A list containing local paths or URL references for
203
+ the message attachment(s). The files attribute currently only
204
+ takes a list containing one (1) filename or URL as an input.
205
+ This is a Spark API limitation that may be lifted at a later
206
+ date.
200
207
201
208
Returns:
202
209
Message: With the details of the created message.
@@ -216,6 +223,7 @@ def create(self, roomId=None, toPersonId=None, toPersonEmail=None,
216
223
assert markdown is None or isinstance (markdown , string_types )
217
224
assert files is None or isinstance (files , list )
218
225
post_data = {}
226
+ # Where is message to be posted?
219
227
if roomId :
220
228
post_data ['roomId' ] = roomId
221
229
elif toPersonId :
@@ -227,18 +235,45 @@ def create(self, roomId=None, toPersonId=None, toPersonEmail=None,
227
235
"toPersonEmail to which you want to post a new " \
228
236
"message."
229
237
raise ciscosparkapiException (error_message )
238
+ # Ensure some message 'content' is provided.
230
239
if not text and not markdown and not files :
231
240
error_message = "You must supply some message content (text, " \
232
241
"markdown, files) when posting a message."
233
242
raise ciscosparkapiException (error_message )
243
+ # Process the content.
234
244
if text :
235
245
post_data ['text' ] = text
236
246
if markdown :
237
247
post_data ['markdown' ] = markdown
248
+ upload_local_file = False
238
249
if files :
239
- post_data ['files' ] = files
250
+ if len (files ) > 1 :
251
+ error_message = "The files attribute currently only takes a " \
252
+ "list containing one (1) filename or URL as " \
253
+ "an input. This is a Spark API limitation " \
254
+ "that may be lifted at a later date."
255
+ raise ciscosparkapiException (error_message )
256
+ if is_web_url (files [0 ]):
257
+ post_data ['files' ] = files
258
+ elif is_local_file (files [0 ]):
259
+ upload_local_file = True
260
+ post_data ['files' ] = open_local_file (files [0 ])
261
+ else :
262
+ error_message = "The provided files argument does not " \
263
+ "contain a valid URL or local file path."
264
+ raise ciscosparkapiException (error_message )
240
265
# API request
241
- json_obj = self .session .post ('messages' , json = post_data )
266
+ if upload_local_file :
267
+ try :
268
+ multipart_data = MultipartEncoder (post_data )
269
+ headers = {'Content-type' : multipart_data .content_type }
270
+ json_obj = self .session .post ('messages' ,
271
+ data = multipart_data ,
272
+ headers = headers )
273
+ finally :
274
+ post_data ['files' ].file_object .close ()
275
+ else :
276
+ json_obj = self .session .post ('messages' , json = post_data )
242
277
# Return a Message object created from the response JSON data
243
278
return Message (json_obj )
244
279
0 commit comments