rpc.py 文件源码

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

项目:stig 作者: rndusr 项目源码 文件源码
def __getattr__(self, method):
        """Return asyncio coroutine that sends RPC request and returns response

        method: Any method from the RPC specs with every '-' replaced with '_'.
                For arguments see the RPC specs.

        Example:
        >>> stats = await client.session_stats()
        >>> torrents = await client.torrent_get(ids=(1,2,3), fields=('status','name'))

        Raises RPCError, ConnectionError, AuthError
        """
        async def request(arguments={}, autoconnect=True, **kwargs):
            async with self.__request_lock:
                if not self.connected:
                    if autoconnect:
                        log.debug('Autoconnecting for %r', method)
                        await self.connect()
                    else:
                        log.debug('Not connected and autoconnect=%r - %r returns None',
                                  autoconnect, method)
                        return None

                arguments.update(**kwargs)
                rpc_request = json.dumps({'method'    : method.replace('_', '-'),
                                          'arguments' : arguments})

                try:
                    return await self.__send_request(rpc_request)
                except ClientError as e:
                    log.debug('Caught ClientError in %r request: %r', method, e)

                    # RPCError does not mean host is unreachable, there was just a
                    # misunderstanding, so we're still connected.
                    if not isinstance(e, RPCError) and self.connected:
                        await self.disconnect(str(e))

                    self.__on_error.send(self.__url, error=e)
                    raise

        request.__name__ = method
        request.__qualname__ = method
        return request
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号