async def call( self, namespace: typing.Optional[str], name: str, *args: typing.Any, **kwargs: typing.Any ): if not self.running: raise RuntimeError('endpoint not running, please call [start]') if args and kwargs: raise ValueError( 'request must either have positional or named arguments ' + 'but not both' ) id = str(uuid.uuid4()) if namespace: name = namespace + self.namespace_seperator + name await self.stream.dispatch_entity( protocol.RpcRequest(id, name, args or kwargs) ) res = self._requests[id] = asyncio.Future() if self._timeout: self.loop.create_task(self.kill_timeout(res)) res = await res del self._requests[id] return res
def test_batch_with_malformed_to_entity(utf8: JsonSerializer): r = [{ "jsonrpc": "2.0", "method": "sum", "params": [1, 2, 4], "id": "1" }, { "jsonrpc": "2.0", "method": "notify_hello", "params": [7] }, { "jsonrpc": "2.0", "method": "a", "params": [42, 23], "id": "2" }, { "foo": "boo" }, { "jsonrpc": "2.0", "method": "b", "params": { "x": "y" }, "id": "5" }, { "jsonrpc": "2.0", "method": "get_data", "id": "9" }] reality = utf8.bytes_to_entity(json.dumps(r).encode()) expectation = pro.RpcBatch(entities=[ pro.RpcRequest(id='1', method='sum', params=[1, 2, 4], jsonrpc='2.0'), pro.RpcNotification(method='notify_hello', params=[7], jsonrpc='2.0'), pro.RpcRequest(id='2', method='a', params=[42, 23], jsonrpc='2.0'), pro.RpcRequest(id='5', method='b', params={'x': 'y'}, jsonrpc='2.0'), pro.RpcRequest(id='9', method='get_data', params=None, jsonrpc='2.0') ]) assert isinstance(reality, pro.RpcBatch) # test if all entities are present except the malformed one # because exceptions are sadly not equatable matches = len([x for x in expectation.entities if x in reality.entities]) assert matches == len(expectation.entities) x = [x for x in reality.entities if isinstance(x, pro.RpcMalformed)] assert len(x) == 1 assert not x[0].id
async def test_dispatch_request(): expectation = protocol.RpcRequest(0, 'yeet', None) buffer = MockWriter() dispatch_stream = create_stream('', buffer) await dispatch_stream.dispatch_entity(expectation) fetch_stream = create_stream(buffer.buffer.decode('utf-8'), MockWriter) assert expectation == await fetch_stream.fetch_entity()
async def test_handle_request(): class Kek: @dispatcher.request async def yeet(self): return 'kektop' e = JsonRpcEndpoint(None) e.attach_dispatcher(Kek()) r = await e._handle_request(pro.RpcRequest(0, 'Kek/yeet', None)) assert r == pro.RpcResult(id=0, result='kektop', jsonrpc='2.0')
def test_request_params_array(): reality = pro.RpcRequest(0, 'topkek', [1, 'hi']).to_dict() expectation = { 'id': 0, 'method': 'topkek', 'jsonrpc': '2.0', 'params': [1, 'hi'] } assert expectation == reality
async def test_handle_request_params(): class Yeet: @dispatcher.request async def yeet(self, a: str): return a e = JsonRpcEndpoint(None) e.attach_dispatcher(Yeet()) r = await e._handle_request( pro.RpcRequest(0, 'Yeet/yeet', ['salad']) ) assert r == pro.RpcResult(id=0, result='salad', jsonrpc='2.0')
def test_request_params_dict(): reality = pro.RpcRequest(0, 'topkek', {'kek': True}).to_dict() expectation = { 'id': 0, 'method': 'topkek', 'jsonrpc': '2.0', 'params': { 'kek': True } } assert expectation == reality
def handle_request(self, data: dict) -> protocol.RpcRequest: return protocol.RpcRequest(data['id'], data['method'], data.get('params'), jsonrpc=self.version)
def test_request_noparams(): reality = pro.RpcRequest(0, 'topkek', None).to_dict() expectation = {'id': 0, 'method': 'topkek', 'jsonrpc': '2.0'} assert expectation == reality
def test_bytes_to_request(utf8: JsonSerializer): r = pro.RpcRequest(0, 'kek', None) expectation = r.to_dict() reality = utf8.bytes_to_entity(utf8.entity_to_bytes(r)).to_dict() assert expectation == reality
def test_request_to_bytes(utf8: JsonSerializer): r = pro.RpcRequest(0, 'kek', None) data = {'jsonrpc': "2.0", 'method': 'kek', 'id': 0} serialized = json.loads(utf8.entity_to_bytes(r).decode('utf-8')) assert serialized == data