lbry-sdk/lbrynet/extras/daemon/client.py

34 lines
1 KiB
Python
Raw Normal View History

2019-01-22 23:44:17 +01:00
import aiohttp
import logging
2019-01-23 16:41:34 +01:00
from lbrynet.conf import Config
2019-01-22 23:44:17 +01:00
log = logging.getLogger(__name__)
USER_AGENT = "AuthServiceProxy/0.1"
TWISTED_SECURE_SESSION = "TWISTED_SECURE_SESSION"
TWISTED_SESSION = "TWISTED_SESSION"
LBRY_SECRET = "LBRY_SECRET"
HTTP_TIMEOUT = 30
class JSONRPCException(Exception):
def __init__(self, rpc_error):
super().__init__()
self.error = rpc_error
2019-01-23 16:41:34 +01:00
class LBRYAPIClient:
def __init__(self, conf: Config):
self._conf = conf
2019-01-22 23:44:17 +01:00
def __getattr__(self, method):
async def f(*args, **kwargs):
2019-01-23 16:41:34 +01:00
async with aiohttp.ClientSession() as session:
message = {'method': method, 'params': [args, kwargs]}
async with session.get(self._conf.api_connection_url, json=message) as resp:
data = await resp.json()
if 'result' in data:
return data['result']
elif 'error' in data:
raise JSONRPCException(data['error'])
2019-01-22 23:44:17 +01:00
return f