当前位置: 首页>>代码示例>>Python>>正文


Python Agent.addCallback方法代码示例

本文整理汇总了Python中twisted.web.client.Agent.addCallback方法的典型用法代码示例。如果您正苦于以下问题:Python Agent.addCallback方法的具体用法?Python Agent.addCallback怎么用?Python Agent.addCallback使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在twisted.web.client.Agent的用法示例。


在下文中一共展示了Agent.addCallback方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: http_patch

# 需要导入模块: from twisted.web.client import Agent [as 别名]
# 或者: from twisted.web.client.Agent import addCallback [as 别名]
def http_patch(endpoint, token, data, reactor=None):
    if reactor is None:
        from twisted.internet import reactor
    assert endpoint is not None
    assert data is not None
    headers = {
        'authorization': [token],
        'content-type': ['application/json'],
        'User-Agent': [__user_agent__]
    }
    payload = json.dumps(data)

    d = Agent(reactor).request(
        method='PATCH',
        uri=endpoint,
        headers=Headers(headers),
        bodyProducer=StringProducer(payload))

    def cbResponse(body, response):
        if response.code == 501:
            raise RateLimitError('Rate limited')
        elif response.code == 400:
            raise LoginError('Unable to peform operation')
        elif response.code != 200:
            raise HTTPError('Unexpected response from server ({response.code})'.format(response=response))
        return body

    def cbWriteBody(response):
        d = defer.Deferred()
        response.deliverBody(SimpleReceiver(d))
        d.addCallback(cbResponse, response)
        return d

    d.addCallback(cbWriteBody)
    return d
开发者ID:maxpowa,项目名称:chord,代码行数:37,代码来源:util.py

示例2: invalidate_token

# 需要导入模块: from twisted.web.client import Agent [as 别名]
# 或者: from twisted.web.client.Agent import addCallback [as 别名]
def invalidate_token(token, reactor=None):
    if reactor is None:
        from twisted.internet import reactor
    headers = {
        'authorization': [token],
        'content-type': ['application/json'],
        'User-Agent': [__user_agent__]
    }
    payload = {
        'token': token
    }
    payload = json.dumps(payload)

    d = Agent(reactor).request(
        method='POST',
        uri='https://discordapp.com/api/auth/logout',
        headers=Headers(headers),
        bodyProducer=StringProducer(payload))

    def cbResponse(body, response):
        if response.code != 200 and response.code != 204:
            raise HTTPError('Unexpected response from server ({response.code})'.format(response=response))

    def cbWriteBody(response):
        d = defer.Deferred()
        response.deliverBody(SimpleReceiver(d))
        d.addCallback(cbResponse, response)
        return d

    d.addCallback(cbWriteBody)
    return d
开发者ID:maxpowa,项目名称:chord,代码行数:33,代码来源:util.py

示例3: get_user_for_token

# 需要导入模块: from twisted.web.client import Agent [as 别名]
# 或者: from twisted.web.client.Agent import addCallback [as 别名]
def get_user_for_token(token, reactor=None):
    if reactor is None:
        from twisted.internet import reactor
    headers = {
        'authorization': [token],
        'content-type': ['application/json'],
        'User-Agent': [__user_agent__]
    }

    d = Agent(reactor).request(
        method='GET',
        uri='https://discordapp.com/api/users/@me',
        headers=Headers(headers),
        bodyProducer=None)

    def cbResponse(response):
        if response.code != 200:
            raise LoginError('Did not receive expected response from @me endpoint. ({response.code})'.format(response=response))
        d = readBody(response)
        d.addCallback(cbParseJson)
        return d

    def cbParseJson(body):
        return json.loads(body)

    d.addCallback(cbResponse)
    return d
开发者ID:maxpowa,项目名称:chord,代码行数:29,代码来源:util.py


注:本文中的twisted.web.client.Agent.addCallback方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。