async def test_using_fixture(pjrpc_aiohttp_mocker): client = aiohttp_client.Client('http://localhost/api/v1') pjrpc_aiohttp_mocker.add('http://localhost/api/v1', 'sum', result=2) result = await client.proxy.sum(1, 1) assert result == 2 pjrpc_aiohttp_mocker.replace( 'http://localhost/api/v1', 'sum', error=pjrpc.exc.JsonRpcError(code=1, message='error', data='oops'), ) with pytest.raises(pjrpc.exc.JsonRpcError) as exc_info: await client.proxy.sum(a=1, b=1) assert exc_info.type is pjrpc.exc.JsonRpcError assert exc_info.value.code == 1 assert exc_info.value.message == 'error' assert exc_info.value.data == 'oops' localhost_calls = pjrpc_aiohttp_mocker.calls['http://localhost/api/v1'] assert localhost_calls[('2.0', 'sum')].call_count == 2 assert localhost_calls[('2.0', 'sum')].mock_calls == [ mock.call(1, 1), mock.call(a=1, b=1) ]
async def test_using_resource_manager(): client = aiohttp_client.Client('http://localhost/api/v1') with PjRpcAiohttpMocker() as mocker: mocker.add('http://localhost/api/v1', 'div', result=2) result = await client.proxy.div(4, 2) assert result == 2 localhost_calls = mocker.calls['http://localhost/api/v1'] assert localhost_calls[('2.0', 'div')].mock_calls == [mock.call(4, 2)]
async def test_pjrpc_server(aiohttp_client, http_app, jsonrpc_app): jsonrpc_cli = async_client.Client('/api/v1', session=await aiohttp_client(http_app)) with PjRpcAiohttpMocker(passthrough=True) as mocker: mocker.add('http://127.0.0.2:8000/api/v1', 'div', result=2) result = await jsonrpc_cli.proxy.div(4, 2) assert result == 2 localhost_calls = mocker.calls['http://127.0.0.2:8000/api/v1'] assert localhost_calls[('2.0', 'div')].mock_calls == [mock.call(4, 2)]
async def main(): async with pjrpc_client.Client('http://localhost/api/v1') as client: response = await client.send(pjrpc.Request('sum', params=[1, 2], id=1)) print(f"1 + 2 = {response.result}") result = await client('sum', a=1, b=2) print(f"1 + 2 = {result}") result = await client.proxy.sum(1, 2) print(f"1 + 2 = {result}") await client.notify('tick')
async def main(): async with pjrpc_client.Client('http://localhost:8080/api/v1') as client: batch_response = await client.batch.send( pjrpc.BatchRequest( pjrpc.Request('sum', [2, 2], id=1), pjrpc.Request('sub', [2, 2], id=2), pjrpc.Request('div', [2, 2], id=3), pjrpc.Request('mult', [2, 2], id=4), ), ) print(f"2 + 2 = {batch_response[0].result}") print(f"2 - 2 = {batch_response[1].result}") print(f"2 / 2 = {batch_response[2].result}") print(f"2 * 2 = {batch_response[3].result}") result = await client.batch('sum', 2, 2)('sub', 2, 2)('div', 2, 2)('mult', 2, 2).call() print(f"2 + 2 = {result[0]}") print(f"2 - 2 = {result[1]}") print(f"2 / 2 = {result[2]}") print(f"2 * 2 = {result[3]}") result = await client.batch[ ('sum', 2, 2), ('sub', 2, 2), ('div', 2, 2), ('mult', 2, 2), ] print(f"2 + 2 = {result[0]}") print(f"2 - 2 = {result[1]}") print(f"2 / 2 = {result[2]}") print(f"2 * 2 = {result[3]}") result = await client.batch.proxy.sum(2, 2).sub(2, 2).div(2, 2).mult(2, 2).call() print(f"2 + 2 = {result[0]}") print(f"2 - 2 = {result[1]}") print(f"2 / 2 = {result[2]}") print(f"2 * 2 = {result[3]}") await client.batch.notify('tick').notify('tack').call()
async def div(a, b): cli = async_client.Client('http://127.0.0.2:8000/api/v1') return await cli.proxy.div(a, b)