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
评论列表
文章目录