async def run_client(uri): async with WebSocketRpcClient(uri, RpcUtilityMethods(), extra_headers=[("X-TOKEN", "fake-super-secret-token")]) as client: # call echo on the other side (using ".other" syntactic sugar) response = await client.other.echo(text="Hello World!") print(response) # call get_process_details on the other side response = await client.call("get_proccess_details") print(response)
async def test_echo(server): """ Test basic RPC with a simple echo """ async with WebSocketRpcClient(uri, RpcUtilityMethods(), default_response_timeout=4) as client: text = "Hello World!" response = await client.other.echo(text=text) assert response.result == text
async def _fetch_(self): assert self._event is not None, "FastApiRpcFetchEvent not provided for FastApiRpcFetchProvider" args = self._event.config.rpc_arguments method = self._event.config.rpc_method_name result = None logger.info(f"{self.__class__.__name__} fetching from {self._url} with RPC call {method}({args})") async with WebSocketRpcClient(self._url, # we don't expose anything to the server RpcMethodsBase(), default_response_timeout=4) as client: result = await client.call(method, args) return result
async def test_keep_alive(server): """ Test basic RPC with a simple echo + keep alive in the background """ async with WebSocketRpcClient(uri, RpcUtilityMethods(), default_response_timeout=4, keep_alive=0.1) as client: text = "Hello World!" response = await client.other.echo(text=text) assert response.result == text await asyncio.sleep(0.6)
async def test_structured_response(server): """ Test RPC with structured (pydantic model) data response Using process details as example data """ async with WebSocketRpcClient(uri, RpcUtilityMethods(), default_response_timeout=4) as client: utils = RpcUtilityMethods() ourProcess = await utils.get_proccess_details() response = await client.other.get_proccess_details() # We got a valid process id assert isinstance(response.result["pid"], int) # We have all the details form the other process assert "cmd" in response.result
async def test_custom_server_methods(server): """ Test rpc with calling custom methods on server sides """ async with WebSocketRpcClient( uri, # we don't expose anything to the server RpcMethodsBase(), default_response_timeout=4) as client: import random a = random.random() b = random.random() response = await client.other.add(a=a, b=b) assert response.result == a + b response = await client.other.multiply(a=a, b=b) assert response.result == a * b
async def test_trigger_flow(server): """ test cascading async trigger flow from client to sever and back Request the server to call us back later """ async with WebSocketRpcClient(uri, ClientMethods(), default_response_timeout=4) as client: time_delta = 0.5 name = "Logan Nine Fingers" # Ask for a wake up call await client.other.register_wake_up_call(time_delta=time_delta, name=name) # Wait for our wake-up call (or fail on timeout) await asyncio.wait_for(client.methods.woke_up_event.wait(), 5) # Note: each channel has its own copy of the methods object assert client.channel.methods.name == name assert client.channel.methods.message == MESSAGE
async def test_recursive_rpc_calls(server): """ Test RPC recursive call - having the server call the client back - following the clients call this recursion isn't useful by itself - but it does test several mechanisms: - bi-directional cascading calls - follow-up calls - remote promise access """ async with WebSocketRpcClient(uri, RpcUtilityMethods(), default_response_timeout=4) as client: text = "recursive-helloworld" utils = RpcUtilityMethods() ourProcess = await utils.get_proccess_details() # we call the server's call_me_back, asking him to call our echo method remote_promise = await client.other.call_me_back(method_name="echo", args={"text": text}) # give the server a chance to call us await asyncio.sleep(1) # go back to the server to get our own response from it response = await client.other.get_response( call_id=remote_promise.result) # check the response we sent assert response.result['result'] == text