commands: "notify" cmd: stop watching addr if called with empty URL
closes #5881
This commit is contained in:
@@ -898,11 +898,16 @@ class Commands:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
@command('n')
|
@command('n')
|
||||||
async def notify(self, address: str, URL: str):
|
async def notify(self, address: str, URL: Optional[str]):
|
||||||
"""Watch an address. Every time the address changes, a http POST is sent to the URL."""
|
"""Watch an address. Every time the address changes, a http POST is sent to the URL.
|
||||||
|
Call with an empty URL to stop watching an address.
|
||||||
|
"""
|
||||||
if not hasattr(self, "_notifier"):
|
if not hasattr(self, "_notifier"):
|
||||||
self._notifier = Notifier(self.network)
|
self._notifier = Notifier(self.network)
|
||||||
await self._notifier.start_watching_queue.put((address, URL))
|
if URL:
|
||||||
|
await self._notifier.start_watching_addr(address, URL)
|
||||||
|
else:
|
||||||
|
await self._notifier.stop_watching_addr(address)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@command('wn')
|
@command('wn')
|
||||||
|
|||||||
@@ -263,7 +263,7 @@ class Notifier(SynchronizerBase):
|
|||||||
def __init__(self, network):
|
def __init__(self, network):
|
||||||
SynchronizerBase.__init__(self, network)
|
SynchronizerBase.__init__(self, network)
|
||||||
self.watched_addresses = defaultdict(list) # type: Dict[str, List[str]]
|
self.watched_addresses = defaultdict(list) # type: Dict[str, List[str]]
|
||||||
self.start_watching_queue = asyncio.Queue()
|
self._start_watching_queue = asyncio.Queue() # type: asyncio.Queue[Tuple[str, str]]
|
||||||
|
|
||||||
async def main(self):
|
async def main(self):
|
||||||
# resend existing subscriptions if we were restarted
|
# resend existing subscriptions if we were restarted
|
||||||
@@ -271,11 +271,20 @@ class Notifier(SynchronizerBase):
|
|||||||
await self._add_address(addr)
|
await self._add_address(addr)
|
||||||
# main loop
|
# main loop
|
||||||
while True:
|
while True:
|
||||||
addr, url = await self.start_watching_queue.get()
|
addr, url = await self._start_watching_queue.get()
|
||||||
self.watched_addresses[addr].append(url)
|
self.watched_addresses[addr].append(url)
|
||||||
await self._add_address(addr)
|
await self._add_address(addr)
|
||||||
|
|
||||||
|
async def start_watching_addr(self, addr: str, url: str):
|
||||||
|
await self._start_watching_queue.put((addr, url))
|
||||||
|
|
||||||
|
async def stop_watching_addr(self, addr: str):
|
||||||
|
self.watched_addresses.pop(addr, None)
|
||||||
|
# TODO blockchain.scripthash.unsubscribe
|
||||||
|
|
||||||
async def _on_address_status(self, addr, status):
|
async def _on_address_status(self, addr, status):
|
||||||
|
if addr not in self.watched_addresses:
|
||||||
|
return
|
||||||
self.logger.info(f'new status for addr {addr}')
|
self.logger.info(f'new status for addr {addr}')
|
||||||
headers = {'content-type': 'application/json'}
|
headers = {'content-type': 'application/json'}
|
||||||
data = {'address': addr, 'status': status}
|
data = {'address': addr, 'status': status}
|
||||||
|
|||||||
Reference in New Issue
Block a user