11import requests
2- import six # noqa: F401
3-
2+ import json
3+ import six
44import sys
55import platform
66from .version import __version__
@@ -60,10 +60,11 @@ def do(self, token, request="?", post_data=None, domain="slack.com", timeout=Non
6060
6161 url = 'https://{0}/api/{1}' .format (domain , request )
6262
63- # Override token header if token is passed in form params
63+ # Override token header if ` token` is passed in post_data
6464 if post_data is not None and "token" in post_data :
6565 token = post_data ['token' ]
6666
67+ # Set user-agent and auth headers
6768 headers = {
6869 'user-agent' : self .get_user_agent (),
6970 'Authorization' : 'Bearer {}' .format (token )
@@ -74,18 +75,26 @@ def do(self, token, request="?", post_data=None, domain="slack.com", timeout=Non
7475 # use the 'file' argument to point to a File ID.
7576 post_data = post_data or {}
7677
77- # Check for plural fields and convert them to comma-separated strings if needed
78- for field in {'channels' , 'users' , 'types' } & set (post_data .keys ()):
79- if isinstance (post_data [field ], list ):
80- post_data [field ] = "," .join (post_data [field ])
81-
8278 # Move singular file objects into `files`
8379 upload_requests = ['files.upload' ]
8480
81+ # Move file content into requests' `files` param
8582 files = None
8683 if request in upload_requests :
8784 files = {'file' : post_data .pop ('file' )} if 'file' in post_data else None
8885
86+ # Check for plural fields and convert them to comma-separated strings if needed
87+ for field in {'channels' , 'users' , 'types' } & set (post_data .keys ()):
88+ if isinstance (post_data [field ], list ):
89+ post_data [field ] = "," .join (post_data [field ])
90+
91+ # Convert any params which aren't integer, string or bool to JSON
92+ # Example: `attachments` is a dict
93+ for k , v in six .iteritems (post_data ):
94+ if not isinstance (v , (six .string_types , six .integer_types , bool )):
95+ post_data [k ] = json .dumps (v )
96+
97+ # Submit the request
8998 return requests .post (
9099 url ,
91100 headers = headers ,
0 commit comments