api.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:votebot 作者: greut 项目源码 文件源码
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()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号