Skip to content

Commit af9e7b3

Browse files
authored
Merge pull request #5 from Arkiv-Network/feature/expires-in-api
feat: add expires_in API with backward compatibility for BTL
2 parents c9b6349 + 2eed942 commit af9e7b3

File tree

4 files changed

+318
-28
lines changed

4 files changed

+318
-28
lines changed

arkiv_sdk/__init__.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
Sequence,
1313
)
1414
from typing import (
15+
TYPE_CHECKING,
1516
Any,
17+
TypeAlias,
1618
cast,
1719
)
1820

1921
from eth_typing import ChecksumAddress, HexStr
20-
from web3 import AsyncWeb3, WebSocketProvider
22+
from web3 import AsyncHTTPProvider, AsyncWeb3, WebSocketProvider
2123
from web3.contract import AsyncContract
2224
from web3.exceptions import ProviderConnectionError, Web3RPCError, Web3ValueError
2325
from web3.method import Method, default_root_munger
@@ -86,18 +88,23 @@
8688
"Wei",
8789
]
8890

91+
if TYPE_CHECKING:
92+
HTTPClient: TypeAlias = AsyncWeb3[AsyncHTTPProvider]
93+
WSClient: TypeAlias = AsyncWeb3[WebSocketProvider]
94+
else:
95+
HTTPClient: TypeAlias = AsyncWeb3
96+
WSClient: TypeAlias = AsyncWeb3
97+
8998

9099
logger = logging.getLogger(__name__)
91100
"""@private"""
92101

93102

94-
class ArkivHttpClient(AsyncWeb3):
103+
class ArkivHttpClient(HTTPClient):
95104
"""Subclass of AsyncWeb3 with added Arkiv methods."""
96105

97106
def __init__(self, rpc_url: str):
98-
super().__init__(
99-
AsyncWeb3.AsyncHTTPProvider(rpc_url, request_kwargs={"timeout": 60})
100-
)
107+
super().__init__(AsyncHTTPProvider(rpc_url, request_kwargs={"timeout": 60}))
101108

102109
self.eth.attach_methods(
103110
{
@@ -215,7 +222,7 @@ async def query_entities(self, query: str) -> Sequence[QueryEntitiesResult]:
215222

216223
class ArkivROClient:
217224
_http_client: ArkivHttpClient
218-
_ws_client: AsyncWeb3
225+
_ws_client: WSClient
219226
_arkiv_contract: AsyncContract
220227
_background_tasks: set[asyncio.Task[None]]
221228

@@ -229,11 +236,11 @@ async def create_ro_client(rpc_url: str, ws_url: str) -> "ArkivROClient":
229236
return ArkivROClient(rpc_url, await ArkivROClient._create_ws_client(ws_url))
230237

231238
@staticmethod
232-
async def _create_ws_client(ws_url: str) -> "AsyncWeb3":
233-
ws_client: AsyncWeb3 = await AsyncWeb3(WebSocketProvider(ws_url))
239+
async def _create_ws_client(ws_url: str) -> "AsyncWeb3[WebSocketProvider]":
240+
ws_client: WSClient = await AsyncWeb3(WebSocketProvider(ws_url))
234241
return ws_client
235242

236-
def __init__(self, rpc_url: str, ws_client: AsyncWeb3) -> None:
243+
def __init__(self, rpc_url: str, ws_client: WSClient) -> None:
237244
"""Initialise the ArkivClient instance."""
238245
self._http_client = ArkivHttpClient(rpc_url)
239246
self._ws_client = ws_client
@@ -242,7 +249,7 @@ def __init__(self, rpc_url: str, ws_client: AsyncWeb3) -> None:
242249
self._background_tasks = set()
243250

244251
def is_connected(
245-
client: AsyncWeb3,
252+
client: HTTPClient,
246253
) -> Callable[[bool], Coroutine[Any, Any, bool]]:
247254
async def inner(show_traceback: bool) -> bool:
248255
try:
@@ -291,7 +298,7 @@ def http_client(self) -> ArkivHttpClient:
291298
"""Get the underlying web3 http client."""
292299
return self._http_client
293300

294-
def ws_client(self) -> AsyncWeb3:
301+
def ws_client(self) -> WSClient:
295302
"""Get the underlying web3 websocket client."""
296303
return self._ws_client
297304

@@ -582,7 +589,7 @@ async def create(rpc_url: str, ws_url: str, private_key: bytes) -> "ArkivClient"
582589
"""
583590
return await ArkivClient.create_rw_client(rpc_url, ws_url, private_key)
584591

585-
def __init__(self, rpc_url: str, ws_client: AsyncWeb3, private_key: bytes) -> None:
592+
def __init__(self, rpc_url: str, ws_client: WSClient, private_key: bytes) -> None:
586593
"""Initialise the ArkivClient instance."""
587594
super().__init__(rpc_url, ws_client)
588595

0 commit comments

Comments
 (0)