def call(method, file=None, **kwargs):
r"""
Perform an API call to Slack.
:param file: File pointer
:type file: file
:param \**kwargs: see below
:Keyword Arguments:
All the arguments required by the method from the `Slack Web API`_.
:returns: JSON response.
:rtype: dict
"""
# JSON encode any sub-structure...
for k, w in kwargs.items():
# keep str as is.
if not isinstance(w, (bytes, str)):
kwargs[k] = json.dumps(w)
form = FormData(kwargs)
# Handle file upload
if file:
form.add_field('file', file)
logging.debug('POST (m=%s) /api/%s %s', form.is_multipart, method, kwargs)
with ClientSession() as session:
with Timeout(10):
response = yield from session.post('https://{0}/api/{1}'
.format(SLACK_DOMAIN, method),
data=form)
assert 200 == response.status, response
try:
body = yield from response.json()
logging.debug('Response /api/%s %d %s',
method, response.status, body)
return body
finally:
yield from response.release()
评论列表
文章目录