1212from builtins import object
1313from six import string_types
1414
15+ from requests_toolbelt import MultipartEncoder
16+
1517from 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
1720from ciscosparkapi .restsession import RestSession
1821from ciscosparkapi .sparkdata import SparkData
1922
@@ -196,7 +199,11 @@ def create(self, roomId=None, toPersonId=None, toPersonEmail=None,
196199 specified this parameter may be optionally used to provide
197200 alternate text forUI clients that do not support rich text.
198201 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.
200207
201208 Returns:
202209 Message: With the details of the created message.
@@ -216,6 +223,7 @@ def create(self, roomId=None, toPersonId=None, toPersonEmail=None,
216223 assert markdown is None or isinstance (markdown , string_types )
217224 assert files is None or isinstance (files , list )
218225 post_data = {}
226+ # Where is message to be posted?
219227 if roomId :
220228 post_data ['roomId' ] = roomId
221229 elif toPersonId :
@@ -227,18 +235,45 @@ def create(self, roomId=None, toPersonId=None, toPersonEmail=None,
227235 "toPersonEmail to which you want to post a new " \
228236 "message."
229237 raise ciscosparkapiException (error_message )
238+ # Ensure some message 'content' is provided.
230239 if not text and not markdown and not files :
231240 error_message = "You must supply some message content (text, " \
232241 "markdown, files) when posting a message."
233242 raise ciscosparkapiException (error_message )
243+ # Process the content.
234244 if text :
235245 post_data ['text' ] = text
236246 if markdown :
237247 post_data ['markdown' ] = markdown
248+ upload_local_file = False
238249 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 )
240265 # 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 )
242277 # Return a Message object created from the response JSON data
243278 return Message (json_obj )
244279
0 commit comments