diff --git a/chia/rpc/rpc_client.py b/chia/rpc/rpc_client.py index 4f736a0c3fea..b02766cbe63e 100644 --- a/chia/rpc/rpc_client.py +++ b/chia/rpc/rpc_client.py @@ -2,6 +2,7 @@ import asyncio from contextlib import asynccontextmanager +from dataclasses import dataclass from pathlib import Path from ssl import SSLContext from typing import Any, AsyncIterator, Dict, List, Optional, Type, TypeVar @@ -18,6 +19,7 @@ _T_RpcClient = TypeVar("_T_RpcClient", bound="RpcClient") +@dataclass class RpcClient: """ Client to Chia RPC, connects to a local service. Uses HTTP/JSON, and converts back from @@ -29,10 +31,10 @@ class RpcClient: url: str session: aiohttp.ClientSession - closing_task: Optional[asyncio.Task] - ssl_context: Optional[SSLContext] + ssl_context: SSLContext hostname: str port: uint16 + closing_task: Optional[asyncio.Task] = None @classmethod async def create( @@ -42,16 +44,18 @@ async def create( root_path: Path, net_config: Dict[str, Any], ) -> _T_RpcClient: - self = cls() - self.hostname = self_hostname - self.port = port - self.url = f"https://{self_hostname}:{str(port)}/" - self.session = aiohttp.ClientSession() ca_crt_path, ca_key_path = private_ssl_ca_paths(root_path, net_config) crt_path = root_path / net_config["daemon_ssl"]["private_crt"] key_path = root_path / net_config["daemon_ssl"]["private_key"] - self.ssl_context = ssl_context_for_client(ca_crt_path, ca_key_path, crt_path, key_path) - self.closing_task = None + + self = cls( + hostname=self_hostname, + port=port, + url=f"https://{self_hostname}:{str(port)}/", + session=aiohttp.ClientSession(), + ssl_context=ssl_context_for_client(ca_crt_path, ca_key_path, crt_path, key_path), + ) + return self @classmethod