Skip to content

Commit

Permalink
@dataclass RpcClient (#16385)
Browse files Browse the repository at this point in the history
  • Loading branch information
altendky authored Oct 3, 2023
1 parent f1059bc commit 13b7410
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions chia/rpc/rpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit 13b7410

Please sign in to comment.