|
| 1 | +from __future__ import annotations |
1 | 2 | import json |
2 | 3 | import platform |
3 | | - |
4 | | -from onepassword.errors import raise_typed_exception |
| 4 | +from typing import Any, Protocol |
| 5 | +from onepassword.desktop_core import DesktopCore |
| 6 | +from onepassword.errors import raise_typed_exception, DesktopSessionExpiredException |
5 | 7 |
|
6 | 8 | # In empirical tests, we determined that maximum message size that can cross the FFI boundary |
7 | 9 | # is ~128MB. Past this limit, FFI will throw an error and the program will crash. |
8 | 10 | # 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 | 11 | MESSAGE_LIMIT = 50 * 1024 * 1024 |
10 | 12 |
|
11 | | -machine_arch = platform.machine().lower() |
12 | | - |
13 | | -if machine_arch in ["x86_64", "amd64"]: |
14 | | - import onepassword.lib.x86_64.op_uniffi_core as core |
15 | | -elif machine_arch in ["aarch64", "arm64"]: |
16 | | - import onepassword.lib.aarch64.op_uniffi_core as core |
17 | | -else: |
18 | | - raise ImportError( |
19 | | - f"Your machine's architecture is not currently supported: {machine_arch}" |
20 | | - ) |
21 | | - |
22 | | - |
23 | | -# InitClient creates a client instance in the current core module and returns its unique ID. |
24 | | -async def _init_client(client_config): |
25 | | - try: |
26 | | - return await core.init_client(json.dumps(client_config)) |
27 | | - except Exception as e: |
28 | | - raise_typed_exception(e) |
29 | | - |
30 | | - |
31 | | -# Invoke calls specified business logic from the SDK core. |
32 | | -async def _invoke(invoke_config): |
33 | | - serialized_config = json.dumps(invoke_config) |
34 | | - if len(serialized_config.encode()) > MESSAGE_LIMIT: |
35 | | - raise ValueError( |
36 | | - f"message size exceeds the limit of {MESSAGE_LIMIT} bytes, please contact 1Password at support@1password.com or https://developer.1password.com/joinslack if you need help." |
37 | | - ) |
38 | | - try: |
39 | | - return await core.invoke(serialized_config) |
40 | | - except Exception as e: |
41 | | - raise_typed_exception(e) |
42 | | - |
43 | | - |
44 | | -# Invoke calls specified business logic from the SDK core. |
45 | | -def _invoke_sync(invoke_config): |
46 | | - serialized_config = json.dumps(invoke_config) |
47 | | - if len(serialized_config.encode()) > MESSAGE_LIMIT: |
48 | | - raise ValueError( |
49 | | - f"message size exceeds the limit of {MESSAGE_LIMIT} bytes, please contact 1Password at support@1password.com or https://developer.1password.com/joinslack if you need help." |
50 | | - ) |
51 | | - try: |
52 | | - return core.invoke_sync(serialized_config) |
53 | | - except Exception as e: |
54 | | - raise_typed_exception(e) |
55 | | - |
56 | | - |
57 | | -# ReleaseClient releases memory in the SDK core associated with the given client ID. |
58 | | -def _release_client(client_id): |
59 | | - return core.release_client(json.dumps(client_id)) |
| 13 | + |
| 14 | +class Core(Protocol): |
| 15 | + async def init_client(self, client_config: dict) -> str: ... |
| 16 | + async def invoke(self, invoke_config: dict) -> str: ... |
| 17 | + def invoke_sync(self, invoke_config: dict) -> str: ... |
| 18 | + def release_client(self, client_id: int) -> None: ... |
| 19 | + |
| 20 | + |
| 21 | +class InnerClient: |
| 22 | + client_id: int |
| 23 | + core: DesktopCore | UniffiCore |
| 24 | + config: dict[str, Any] |
| 25 | + |
| 26 | + def __init__(self, client_id: int, core: "DesktopCore | UniffiCore", config: dict[str, any]): |
| 27 | + self.client_id = client_id |
| 28 | + self.core = core |
| 29 | + self.config = config |
| 30 | + |
| 31 | + async def invoke(self, invoke_config: dict): |
| 32 | + try: |
| 33 | + return await self.core.invoke(invoke_config) |
| 34 | + except DesktopSessionExpiredException: |
| 35 | + new_client_id = await self.core.init_client(self.config) |
| 36 | + self.client_id = new_client_id |
| 37 | + invoke_config["invocation"]["clientId"] = self.client_id |
| 38 | + return await self.core.invoke(invoke_config) |
| 39 | + except Exception as e: |
| 40 | + raise e |
| 41 | + |
| 42 | + |
| 43 | +class UniffiCore: |
| 44 | + def __init__(self): |
| 45 | + machine_arch = platform.machine().lower() |
| 46 | + |
| 47 | + if machine_arch in ["x86_64", "amd64"]: |
| 48 | + import onepassword.lib.x86_64.op_uniffi_core as core |
| 49 | + elif machine_arch in ["aarch64", "arm64"]: |
| 50 | + import onepassword.lib.aarch64.op_uniffi_core as core |
| 51 | + else: |
| 52 | + raise ImportError( |
| 53 | + f"Your machine's architecture is not currently supported: {machine_arch}" |
| 54 | + ) |
| 55 | + |
| 56 | + self.core = core |
| 57 | + |
| 58 | + async def init_client(self, client_config: dict): |
| 59 | + """Creates a client instance in the current core module and returns its unique ID.""" |
| 60 | + try: |
| 61 | + return await self.core.init_client(json.dumps(client_config)) |
| 62 | + except Exception as e: |
| 63 | + raise_typed_exception(e) |
| 64 | + |
| 65 | + async def invoke(self, invoke_config: dict): |
| 66 | + """Invoke business logic asynchronously.""" |
| 67 | + serialized_config = json.dumps(invoke_config) |
| 68 | + if len(serialized_config.encode()) > MESSAGE_LIMIT: |
| 69 | + raise ValueError( |
| 70 | + f"message size exceeds the limit of {MESSAGE_LIMIT} bytes, " |
| 71 | + "please contact 1Password at support@1password.com or " |
| 72 | + "https://developer.1password.com/joinslack if you need help." |
| 73 | + ) |
| 74 | + try: |
| 75 | + return await self.core.invoke(serialized_config) |
| 76 | + except Exception as e: |
| 77 | + raise_typed_exception(e) |
| 78 | + |
| 79 | + def invoke_sync(self, invoke_config: dict): |
| 80 | + """Invoke business logic synchronously.""" |
| 81 | + serialized_config = json.dumps(invoke_config) |
| 82 | + if len(serialized_config.encode()) > MESSAGE_LIMIT: |
| 83 | + raise ValueError( |
| 84 | + f"message size exceeds the limit of {MESSAGE_LIMIT} bytes, " |
| 85 | + "please contact 1Password at support@1password.com or " |
| 86 | + "https://developer.1password.com/joinslack if you need help." |
| 87 | + ) |
| 88 | + try: |
| 89 | + return self.core.invoke_sync(serialized_config) |
| 90 | + except Exception as e: |
| 91 | + raise_typed_exception(e) |
| 92 | + |
| 93 | + def release_client(self, client_id: int): |
| 94 | + """Releases memory in the SDK core associated with the given client ID.""" |
| 95 | + try: |
| 96 | + return self.core.release_client(json.dumps(client_id)) |
| 97 | + except Exception as e: |
| 98 | + raise_typed_exception(e) |
0 commit comments