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