|
1 | 1 | import json |
2 | 2 | import platform |
3 | 3 | 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 |
5 | 6 |
|
6 | 7 | # In empirical tests, we determined that maximum message size that can cross the FFI boundary |
7 | 8 | # is ~128MB. Past this limit, FFI will throw an error and the program will crash. |
8 | 9 | # 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 |
9 | 10 | MESSAGE_LIMIT = 50 * 1024 * 1024 |
10 | 11 |
|
| 12 | + |
11 | 13 | class Core(Protocol): |
12 | 14 | async def init_client(self, client_config: dict) -> str: ... |
13 | 15 | async def invoke(self, invoke_config: dict) -> str: ... |
14 | 16 | def invoke_sync(self, invoke_config: dict) -> str: ... |
15 | 17 | def release_client(self, client_id: int) -> None: ... |
16 | 18 |
|
| 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 | + |
17 | 42 | class UniffiCore: |
18 | 43 | def __init__(self): |
19 | 44 | machine_arch = platform.machine().lower() |
|
0 commit comments