-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
83 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import asyncio | ||
import websockets | ||
import logging | ||
import json | ||
|
||
from .rpc import Rpc | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class Websocket(Rpc): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.ws = None | ||
self.loop = kwargs.get('loop') | ||
|
||
async def connect(self): | ||
self.ws = await websockets.connect(self.url, ssl=True, loop=self.loop) | ||
|
||
async def disconnect(self): | ||
await self.ws.close() | ||
|
||
async def rpcexec(self, payload): | ||
if not self.ws: | ||
await self.connect() | ||
|
||
log.debug(json.dumps(payload)) | ||
|
||
await self.ws.send(json.dumps(payload)) | ||
|
||
return await self.ws.recv() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import pytest | ||
import asyncio | ||
import logging | ||
|
||
from grapheneasync.websocket import Websocket | ||
|
||
logger = logging.getLogger('websockets') | ||
logger.setLevel(logging.DEBUG) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_loop(event_loop): | ||
await asyncio.sleep(1) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_rpc(event_loop): | ||
ws = Websocket('wss://eu.nodes.bitshares.ws') | ||
props = await ws.get_dynamic_global_properties() | ||
await ws.disconnect() | ||
logger.info(props) | ||
assert isinstance(props, dict) | ||
assert props['head_block_number'] > 0 |