communex.module.client
Client for Commune modules.
1""" 2Client for Commune modules. 3""" 4 5import asyncio 6import json 7from typing import Any 8 9import aiohttp 10import aiohttp.client_exceptions 11import aiohttp.web_exceptions 12from substrateinterface import Keypair 13 14from communex.errors import NetworkTimeoutError 15from communex.key import check_ss58_address 16from communex.types import Ss58Address 17 18from ._protocol import create_method_endpoint, create_request_data 19from ._signer import TESTING_MNEMONIC 20 21 22class ModuleClient: 23 host: str 24 port: int 25 key: Keypair 26 27 def __init__(self, host: str, port: int, key: Keypair): 28 self.host = host 29 self.port = port 30 self.key = key 31 32 async def call( 33 self, 34 fn: str, 35 target_key: Ss58Address, 36 params: Any = {}, 37 timeout: int = 16, 38 ) -> Any: 39 serialized_data, headers = create_request_data( 40 self.key, target_key, params 41 ) 42 43 out = aiohttp.ClientTimeout(total=timeout) 44 try: 45 async with aiohttp.ClientSession(timeout=out) as session: 46 async with session.post( 47 create_method_endpoint(self.host, self.port, fn), 48 json=json.loads(serialized_data), 49 headers=headers, 50 ) as response: 51 match response.status: 52 case 200: 53 pass 54 case status_code: 55 response_j = await response.json() 56 raise Exception( 57 f"Unexpected status code: {status_code}, response: {response_j}" 58 ) 59 match response.content_type: 60 case "application/json": 61 result = await asyncio.wait_for( 62 response.json(), timeout=timeout 63 ) 64 # TODO: deserialize result 65 return result 66 case _: 67 raise Exception( 68 f"Unknown content type: {response.content_type}" 69 ) 70 except asyncio.exceptions.TimeoutError as e: 71 raise NetworkTimeoutError( 72 f"The call took longer than the timeout of {timeout} second(s)" 73 ).with_traceback(e.__traceback__) 74 75 76if __name__ == "__main__": 77 keypair = Keypair.create_from_mnemonic(TESTING_MNEMONIC) 78 client = ModuleClient("localhost", 8000, keypair) 79 ss58_address = check_ss58_address(keypair.ss58_address) 80 result = asyncio.run( 81 client.call( 82 "do_the_thing", ss58_address, {"awesomness": 45, "extra": "hi"} 83 ) 84 ) 85 print(result)
class
ModuleClient:
23class ModuleClient: 24 host: str 25 port: int 26 key: Keypair 27 28 def __init__(self, host: str, port: int, key: Keypair): 29 self.host = host 30 self.port = port 31 self.key = key 32 33 async def call( 34 self, 35 fn: str, 36 target_key: Ss58Address, 37 params: Any = {}, 38 timeout: int = 16, 39 ) -> Any: 40 serialized_data, headers = create_request_data( 41 self.key, target_key, params 42 ) 43 44 out = aiohttp.ClientTimeout(total=timeout) 45 try: 46 async with aiohttp.ClientSession(timeout=out) as session: 47 async with session.post( 48 create_method_endpoint(self.host, self.port, fn), 49 json=json.loads(serialized_data), 50 headers=headers, 51 ) as response: 52 match response.status: 53 case 200: 54 pass 55 case status_code: 56 response_j = await response.json() 57 raise Exception( 58 f"Unexpected status code: {status_code}, response: {response_j}" 59 ) 60 match response.content_type: 61 case "application/json": 62 result = await asyncio.wait_for( 63 response.json(), timeout=timeout 64 ) 65 # TODO: deserialize result 66 return result 67 case _: 68 raise Exception( 69 f"Unknown content type: {response.content_type}" 70 ) 71 except asyncio.exceptions.TimeoutError as e: 72 raise NetworkTimeoutError( 73 f"The call took longer than the timeout of {timeout} second(s)" 74 ).with_traceback(e.__traceback__)
async def
call( self, fn: str, target_key: communex.types.Ss58Address, params: Any = {}, timeout: int = 16) -> Any:
33 async def call( 34 self, 35 fn: str, 36 target_key: Ss58Address, 37 params: Any = {}, 38 timeout: int = 16, 39 ) -> Any: 40 serialized_data, headers = create_request_data( 41 self.key, target_key, params 42 ) 43 44 out = aiohttp.ClientTimeout(total=timeout) 45 try: 46 async with aiohttp.ClientSession(timeout=out) as session: 47 async with session.post( 48 create_method_endpoint(self.host, self.port, fn), 49 json=json.loads(serialized_data), 50 headers=headers, 51 ) as response: 52 match response.status: 53 case 200: 54 pass 55 case status_code: 56 response_j = await response.json() 57 raise Exception( 58 f"Unexpected status code: {status_code}, response: {response_j}" 59 ) 60 match response.content_type: 61 case "application/json": 62 result = await asyncio.wait_for( 63 response.json(), timeout=timeout 64 ) 65 # TODO: deserialize result 66 return result 67 case _: 68 raise Exception( 69 f"Unknown content type: {response.content_type}" 70 ) 71 except asyncio.exceptions.TimeoutError as e: 72 raise NetworkTimeoutError( 73 f"The call took longer than the timeout of {timeout} second(s)" 74 ).with_traceback(e.__traceback__)