Skip to content

Commit 5e8dde3

Browse files
committed
Add InnerClient class
This class stores the client ID, the core used for making requests and the client config. This Inner client is capable of overriding its client id in case the desktop session expires due to the app getting locked.
1 parent ec9cdc2 commit 5e8dde3

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/onepassword/core.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,44 @@
11
import json
22
import platform
33
from typing import Protocol
4-
from onepassword.errors import raise_typed_exception
4+
from onepassword.desktop_core import DesktopCore
5+
from onepassword.errors import raise_typed_exception, DesktopSessionExpired
56

67
# In empirical tests, we determined that maximum message size that can cross the FFI boundary
78
# is ~128MB. Past this limit, FFI will throw an error and the program will crash.
89
# We set the limit to 50MB to be safe and consistent with the other SDKs (where this limit is 64MB), to be reconsidered upon further testing
910
MESSAGE_LIMIT = 50 * 1024 * 1024
1011

12+
1113
class Core(Protocol):
1214
async def init_client(self, client_config: dict) -> str: ...
1315
async def invoke(self, invoke_config: dict) -> str: ...
1416
def invoke_sync(self, invoke_config: dict) -> str: ...
1517
def release_client(self, client_id: int) -> None: ...
1618

19+
20+
class InnerClient:
21+
client_id: int
22+
core: DesktopCore | UniffiCore
23+
config: dict[str, any]
24+
25+
def __init__(self, client_id: int, core: "DesktopCore | UniffiCore", config: dict[str, any]):
26+
self.client_id = client_id
27+
self.core = core
28+
self.config = config
29+
30+
async def invoke(self, invoke_config: dict):
31+
try:
32+
return await self.core.invoke(invoke_config)
33+
except DesktopSessionExpired as e:
34+
new_client_id = await self.core.init_client(self.config)
35+
self.client_id = new_client_id
36+
invoke_config["invocation"]["clientId"] = self.client_id
37+
return await self.core.invoke(invoke_config)
38+
except Exception as e:
39+
raise e
40+
41+
1742
class UniffiCore:
1843
def __init__(self):
1944
machine_arch = platform.machine().lower()

0 commit comments

Comments
 (0)