mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-09-01 01:35:20 +00:00
Remove dependencies: jsonrpcserver, jsonrpcclient
This commit is contained in:
parent
947af92126
commit
30f5be26ac
4 changed files with 52 additions and 29 deletions
|
@ -9,6 +9,4 @@ aiohttp>=3.3.0,<4.0.0
|
||||||
aiohttp_socks>=0.3
|
aiohttp_socks>=0.3
|
||||||
certifi
|
certifi
|
||||||
bitstring
|
bitstring
|
||||||
jsonrpcserver
|
|
||||||
jsonrpcclient
|
|
||||||
attrs
|
attrs
|
||||||
|
|
|
@ -37,11 +37,8 @@ from concurrent import futures
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from aiohttp import web, client_exceptions
|
from aiohttp import web, client_exceptions
|
||||||
import jsonrpcclient
|
|
||||||
import jsonrpcserver
|
|
||||||
from jsonrpcserver import response
|
|
||||||
from jsonrpcclient.clients.aiohttp_client import AiohttpClient
|
|
||||||
from aiorpcx import TaskGroup
|
from aiorpcx import TaskGroup
|
||||||
|
import json
|
||||||
|
|
||||||
from . import util
|
from . import util
|
||||||
from .network import Network
|
from .network import Network
|
||||||
|
@ -107,10 +104,8 @@ def request(config: SimpleConfig, endpoint, args=(), timeout=60):
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
async def request_coroutine():
|
async def request_coroutine():
|
||||||
async with aiohttp.ClientSession(auth=auth) as session:
|
async with aiohttp.ClientSession(auth=auth) as session:
|
||||||
server = AiohttpClient(session, server_url, timeout=timeout)
|
c = util.myAiohttpClient(session, server_url)
|
||||||
f = getattr(server, endpoint)
|
return await c.request(endpoint, *args)
|
||||||
response = await f(*args)
|
|
||||||
return response.data.result
|
|
||||||
try:
|
try:
|
||||||
fut = asyncio.run_coroutine_threadsafe(request_coroutine(), loop)
|
fut = asyncio.run_coroutine_threadsafe(request_coroutine(), loop)
|
||||||
return fut.result(timeout=timeout)
|
return fut.result(timeout=timeout)
|
||||||
|
@ -184,16 +179,22 @@ class AuthenticatedServer(Logger):
|
||||||
text='Unauthorized', status=401)
|
text='Unauthorized', status=401)
|
||||||
except AuthenticationCredentialsInvalid:
|
except AuthenticationCredentialsInvalid:
|
||||||
return web.Response(text='Forbidden', status=403)
|
return web.Response(text='Forbidden', status=403)
|
||||||
request = await request.text()
|
try:
|
||||||
response = await jsonrpcserver.async_dispatch(request, methods=self.methods)
|
request = await request.text()
|
||||||
if isinstance(response, jsonrpcserver.response.ExceptionResponse):
|
request = json.loads(request)
|
||||||
self.logger.error(f"error handling request: {request}", exc_info=response.exc)
|
method = request['method']
|
||||||
# this exposes the error message to the client
|
_id = request['id']
|
||||||
response.message = str(response.exc)
|
params = request.get('params', [])
|
||||||
if response.wanted:
|
f = getattr(self, method)
|
||||||
return web.json_response(response.deserialized(), status=response.http_status)
|
assert f in self.methods
|
||||||
else:
|
except:
|
||||||
return web.Response()
|
return web.Response(text='Invalid Request', status=500)
|
||||||
|
response = {'id':_id}
|
||||||
|
try:
|
||||||
|
response['result'] = await f(*params)
|
||||||
|
except BaseException as e:
|
||||||
|
response['error'] = str(e)
|
||||||
|
return web.json_response(response)
|
||||||
|
|
||||||
|
|
||||||
class CommandsServer(AuthenticatedServer):
|
class CommandsServer(AuthenticatedServer):
|
||||||
|
@ -208,7 +209,7 @@ class CommandsServer(AuthenticatedServer):
|
||||||
self.port = self.config.get('rpcport', 0)
|
self.port = self.config.get('rpcport', 0)
|
||||||
self.app = web.Application()
|
self.app = web.Application()
|
||||||
self.app.router.add_post("/", self.handle)
|
self.app.router.add_post("/", self.handle)
|
||||||
self.methods = jsonrpcserver.methods.Methods()
|
self.methods = set()
|
||||||
self.methods.add(self.ping)
|
self.methods.add(self.ping)
|
||||||
self.methods.add(self.gui)
|
self.methods.add(self.gui)
|
||||||
self.cmd_runner = Commands(config=self.config, network=self.daemon.network, daemon=self.daemon)
|
self.cmd_runner = Commands(config=self.config, network=self.daemon.network, daemon=self.daemon)
|
||||||
|
@ -276,7 +277,7 @@ class WatchTowerServer(AuthenticatedServer):
|
||||||
self.lnwatcher = network.local_watchtower
|
self.lnwatcher = network.local_watchtower
|
||||||
self.app = web.Application()
|
self.app = web.Application()
|
||||||
self.app.router.add_post("/", self.handle)
|
self.app.router.add_post("/", self.handle)
|
||||||
self.methods = jsonrpcserver.methods.Methods()
|
self.methods = set()
|
||||||
self.methods.add(self.get_ctn)
|
self.methods.add(self.get_ctn)
|
||||||
self.methods.add(self.add_sweep_tx)
|
self.methods.add(self.add_sweep_tx)
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ import time
|
||||||
from typing import Optional, Sequence, Tuple, List, Dict, TYPE_CHECKING, NamedTuple, Union, Mapping
|
from typing import Optional, Sequence, Tuple, List, Dict, TYPE_CHECKING, NamedTuple, Union, Mapping
|
||||||
import threading
|
import threading
|
||||||
import socket
|
import socket
|
||||||
|
import aiohttp
|
||||||
import json
|
import json
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
@ -25,7 +26,7 @@ from . import constants, util
|
||||||
from . import keystore
|
from . import keystore
|
||||||
from .util import profiler
|
from .util import profiler
|
||||||
from .invoices import PR_TYPE_LN, PR_UNPAID, PR_EXPIRED, PR_PAID, PR_INFLIGHT, PR_FAILED, PR_ROUTING, LNInvoice, LN_EXPIRY_NEVER
|
from .invoices import PR_TYPE_LN, PR_UNPAID, PR_EXPIRED, PR_PAID, PR_INFLIGHT, PR_FAILED, PR_ROUTING, LNInvoice, LN_EXPIRY_NEVER
|
||||||
from .util import NetworkRetryManager
|
from .util import NetworkRetryManager, myAiohttpClient
|
||||||
from .lnutil import LN_MAX_FUNDING_SAT
|
from .lnutil import LN_MAX_FUNDING_SAT
|
||||||
from .keystore import BIP32_KeyStore
|
from .keystore import BIP32_KeyStore
|
||||||
from .bitcoin import COIN
|
from .bitcoin import COIN
|
||||||
|
@ -525,12 +526,6 @@ class LNWallet(LNWorker):
|
||||||
@ignore_exceptions
|
@ignore_exceptions
|
||||||
@log_exceptions
|
@log_exceptions
|
||||||
async def sync_with_remote_watchtower(self):
|
async def sync_with_remote_watchtower(self):
|
||||||
import aiohttp
|
|
||||||
from jsonrpcclient.clients.aiohttp_client import AiohttpClient
|
|
||||||
class myAiohttpClient(AiohttpClient):
|
|
||||||
async def request(self, *args, **kwargs):
|
|
||||||
r = await super().request(*args, **kwargs)
|
|
||||||
return r.data.result
|
|
||||||
while True:
|
while True:
|
||||||
await asyncio.sleep(5)
|
await asyncio.sleep(5)
|
||||||
watchtower_url = self.config.get('watchtower_url')
|
watchtower_url = self.config.get('watchtower_url')
|
||||||
|
@ -539,6 +534,8 @@ class LNWallet(LNWorker):
|
||||||
try:
|
try:
|
||||||
async with make_aiohttp_session(proxy=self.network.proxy) as session:
|
async with make_aiohttp_session(proxy=self.network.proxy) as session:
|
||||||
watchtower = myAiohttpClient(session, watchtower_url)
|
watchtower = myAiohttpClient(session, watchtower_url)
|
||||||
|
watchtower.add_method('get_ctn')
|
||||||
|
watchtower.add_method('add_sweep_tx')
|
||||||
for chan in self.channels.values():
|
for chan in self.channels.values():
|
||||||
await self.sync_channel_with_watchtower(chan, watchtower)
|
await self.sync_channel_with_watchtower(chan, watchtower)
|
||||||
except aiohttp.client_exceptions.ClientConnectorError:
|
except aiohttp.client_exceptions.ClientConnectorError:
|
||||||
|
|
|
@ -1368,3 +1368,30 @@ class MySocksProxy(aiorpcx.SOCKSProxy):
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError # http proxy not available with aiorpcx
|
raise NotImplementedError # http proxy not available with aiorpcx
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
class myAiohttpClient:
|
||||||
|
|
||||||
|
def __init__(self, session, url):
|
||||||
|
self.session = session
|
||||||
|
self.url = url
|
||||||
|
|
||||||
|
async def request(self, endpoint, *args):
|
||||||
|
data = '{"jsonrpc": "2.0", "id":"0", "method":"%s", "params": %s }' %(endpoint, json.dumps(args))
|
||||||
|
async with self.session.post(self.url, data=data) as resp:
|
||||||
|
if resp.status == 200:
|
||||||
|
r = await resp.json()
|
||||||
|
result = r.get('result')
|
||||||
|
error = r.get('error')
|
||||||
|
if error:
|
||||||
|
return 'Error: ' + str(error)
|
||||||
|
else:
|
||||||
|
return result
|
||||||
|
else:
|
||||||
|
text = await resp.text()
|
||||||
|
return 'Error: ' + str(text)
|
||||||
|
|
||||||
|
def add_method(self, endpoint):
|
||||||
|
async def coro(*args):
|
||||||
|
return await self.request(endpoint, *args)
|
||||||
|
setattr(self, endpoint, coro)
|
||||||
|
|
Loading…
Add table
Reference in a new issue