Skip to content

Commit bf41581

Browse files
author
Shubham
committed
Fix type error, add obx_sync_state call in SyncClient
1 parent a9edd55 commit bf41581

File tree

2 files changed

+66
-40
lines changed

2 files changed

+66
-40
lines changed

objectbox/c.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
# Version of the library used by the binding. This version is checked at runtime to ensure binary compatibility.
2929
# Don't forget to update download-c-lib.py when upgrading to a newer version.
30-
required_version = "4.0.0"
30+
required_version = "5.0.0"
3131

3232

3333
def shlib_name(library: str) -> str:
@@ -266,8 +266,12 @@ class OBX_sync_server(ctypes.Structure):
266266

267267
OBX_sync_server_p = ctypes.POINTER(OBX_sync_server)
268268

269+
OBXSyncCredentialsType = ctypes.c_int
270+
OBXRequestUpdatesMode = ctypes.c_int
271+
OBXSyncState = ctypes.c_int
272+
OBXSyncCode = ctypes.c_int
269273

270-
class OBXSyncCredentialsType(IntEnum):
274+
class SyncCredentialsType(IntEnum):
271275
NONE = 1
272276
SHARED_SECRET = 2 # Deprecated, use SHARED_SECRET_SIPPED instead
273277
GOOGLE_AUTH = 3
@@ -280,13 +284,13 @@ class OBXSyncCredentialsType(IntEnum):
280284
JWT_CUSTOM = 10 # JSON Web Token (JWT): custom token type
281285

282286

283-
class OBXRequestUpdatesMode(IntEnum):
287+
class RequestUpdatesMode(IntEnum):
284288
MANUAL = 0 # No updates by default, must call obx_sync_updates_request() manually
285289
AUTO = 1 # Same as calling obx_sync_updates_request(sync, TRUE)
286290
AUTO_NO_PUSHES = 2 # Same as calling obx_sync_updates_request(sync, FALSE)
287291

288292

289-
class OBXSyncState(IntEnum):
293+
class SyncState(IntEnum):
290294
CREATED = 1
291295
STARTED = 2
292296
CONNECTED = 3
@@ -296,7 +300,7 @@ class OBXSyncState(IntEnum):
296300
DEAD = 7
297301

298302

299-
class OBXSyncCode(IntEnum):
303+
class SyncCode(IntEnum):
300304
OK = 20
301305
REQ_REJECTED = 40
302306
CREDENTIALS_REJECTED = 43
@@ -1191,21 +1195,23 @@ def c_array_pointer(py_list: Union[List[Any], np.ndarray], c_type):
11911195
OBXBackupRestoreFlags_None = 0
11921196
OBXBackupRestoreFlags_OverwriteExistingData = 1
11931197

1194-
obx_sync = c_fn("obx_sync", obx_err, [OBX_store_p, ctypes.c_char_p])
1195-
obx_sync_urls = c_fn("obx_sync_urls", obx_err, [OBX_store_p, ctypes.POINTER(ctypes.c_char_p), ctypes.c_size_t])
1198+
obx_sync = c_fn("obx_sync", OBX_sync_p, [OBX_store_p, ctypes.c_char_p])
1199+
obx_sync_urls = c_fn("obx_sync_urls", OBX_sync_p, [OBX_store_p, ctypes.POINTER(ctypes.c_char_p), ctypes.c_size_t])
11961200

11971201

11981202
obx_sync_credentials = c_fn_rc('obx_sync_credentials',
1199-
[OBX_sync_p, OBXSyncCredentialsType, ctypes.c_void_p, ctypes.c_size_t])
1203+
[OBX_sync_p, OBXSyncCredentialsType, ctypes.c_void_p, ctypes.c_size_t])
12001204
obx_sync_credentials_user_password = c_fn_rc('obx_sync_credentials_user_password',
12011205
[OBX_sync_p, OBXSyncCredentialsType, ctypes.c_char_p,
12021206
ctypes.c_char_p])
12031207
obx_sync_credentials_add = c_fn_rc('obx_sync_credentials_add',
1204-
[OBX_sync_p, OBXSyncCredentialsType, ctypes.c_void_p, ctypes.c_size_t, ctypes.c_bool])
1208+
[OBX_sync_p, OBXSyncCredentialsType, ctypes.c_void_p, ctypes.c_size_t, ctypes.c_bool])
12051209
obx_sync_credentials_add_user_password = c_fn_rc('obx_sync_credentials_add_user_password',
12061210
[OBX_sync_p, OBXSyncCredentialsType, ctypes.c_char_p, ctypes.c_char_p,
12071211
ctypes.c_bool])
12081212

1213+
obx_sync_state = c_fn('obx_sync_state', OBXSyncState, [OBX_sync_p])
1214+
12091215
obx_sync_request_updates_mode = c_fn_rc('obx_sync_request_updates_mode', [OBX_sync_p, OBXRequestUpdatesMode])
12101216

12111217
obx_sync_start = c_fn_rc('obx_sync_start', [OBX_sync_p])

objectbox/sync.py

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import ctypes
2-
import c as c
2+
import objectbox.c as c
33
from objectbox import Store
4-
from objectbox.c import c_array_pointer
5-
4+
from enum import Enum, auto
65

76
class SyncCredentials:
87

9-
def __init__(self, credential_type: c.OBXSyncCredentialsType):
8+
def __init__(self, credential_type: c.SyncCredentialsType):
109
self.type = credential_type
1110

1211
@staticmethod
@@ -15,60 +14,60 @@ def none() -> 'SyncCredentials':
1514

1615
@staticmethod
1716
def shared_secret_string(secret: str) -> 'SyncCredentials':
18-
return SyncCredentialsSecret(c.OBXSyncCredentialsType.SHARED_SECRET_SIPPED, secret.encode('utf-8'))
17+
return SyncCredentialsSecret(c.SyncCredentialsType.SHARED_SECRET_SIPPED, secret.encode('utf-8'))
1918

2019
@staticmethod
2120
def google_auth(secret: str) -> 'SyncCredentials':
22-
return SyncCredentialsSecret(c.OBXSyncCredentialsType.GOOGLE_AUTH, secret.encode('utf-8'))
21+
return SyncCredentialsSecret(c.SyncCredentialsType.GOOGLE_AUTH, secret.encode('utf-8'))
2322

2423
@staticmethod
2524
def user_and_password(username: str, password: str) -> 'SyncCredentials':
26-
return SyncCredentialsUserPassword(c.OBXSyncCredentialsType.USER_PASSWORD, username, password)
25+
return SyncCredentialsUserPassword(c.SyncCredentialsType.USER_PASSWORD, username, password)
2726

2827
@staticmethod
2928
def jwt_id_token(jwt_id_token: str) -> 'SyncCredentials':
30-
return SyncCredentialsSecret(c.OBXSyncCredentialsType.JWT_ID, jwt_id_token.encode('utf-8'))
29+
return SyncCredentialsSecret(c.SyncCredentialsType.JWT_ID, jwt_id_token.encode('utf-8'))
3130

3231
@staticmethod
3332
def jwt_access_token(jwt_access_token: str) -> 'SyncCredentials':
34-
return SyncCredentialsSecret(c.OBXSyncCredentialsType.JWT_ACCESS, jwt_access_token.encode('utf-8'))
33+
return SyncCredentialsSecret(c.SyncCredentialsType.JWT_ACCESS, jwt_access_token.encode('utf-8'))
3534

3635
@staticmethod
3736
def jwt_refresh_token(jwt_refresh_token: str) -> 'SyncCredentials':
38-
return SyncCredentialsSecret(c.OBXSyncCredentialsType.JWT_REFRESH, jwt_refresh_token.encode('utf-8'))
37+
return SyncCredentialsSecret(c.SyncCredentialsType.JWT_REFRESH, jwt_refresh_token.encode('utf-8'))
3938

4039
@staticmethod
4140
def jwt_custom_token(jwt_custom_token: str) -> 'SyncCredentials':
42-
return SyncCredentialsSecret(c.OBXSyncCredentialsType.JWT_CUSTOM, jwt_custom_token.encode('utf-8'))
41+
return SyncCredentialsSecret(c.SyncCredentialsType.JWT_CUSTOM, jwt_custom_token.encode('utf-8'))
4342

4443

4544
class SyncCredentialsNone(SyncCredentials):
4645
def __init__(self):
47-
super().__init__(c.OBXSyncCredentialsType.NONE)
46+
super().__init__(c.SyncCredentialsType.NONE)
4847

4948

5049
class SyncCredentialsSecret(SyncCredentials):
51-
def __init__(self, credential_type: c.OBXSyncCredentialsType, secret: bytes):
50+
def __init__(self, credential_type: c.SyncCredentialsType, secret: bytes):
5251
super().__init__(credential_type)
5352
self.secret = secret
5453

5554

5655
class SyncCredentialsUserPassword(SyncCredentials):
57-
def __init__(self, credential_type: c.OBXSyncCredentialsType, username: str, password: str):
56+
def __init__(self, credential_type: c.SyncCredentialsType, username: str, password: str):
5857
super().__init__(credential_type)
5958
self.username = username
6059
self.password = password
6160

6261

63-
class SyncState:
64-
UNKNOWN = 'unknown'
65-
CREATED = 'created'
66-
STARTED = 'started'
67-
CONNECTED = 'connected'
68-
LOGGED_IN = 'logged_in'
69-
DISCONNECTED = 'disconnected'
70-
STOPPED = 'stopped'
71-
DEAD = 'dead'
62+
class SyncState(Enum):
63+
UNKNOWN = auto()
64+
CREATED = auto()
65+
STARTED = auto()
66+
CONNECTED = auto()
67+
LOGGED_IN = auto()
68+
DISCONNECTED = auto()
69+
STOPPED = auto()
70+
DEAD = auto()
7271

7372

7473
class SyncRequestUpdatesMode:
@@ -103,16 +102,18 @@ def __init__(self, store: Store, server_urls: list[str], credentials: list[SyncC
103102
if not server_urls:
104103
raise ValueError("Provide at least one server URL")
105104

106-
if not Sync.is_available():
107-
raise RuntimeError(
108-
'Sync is not available in the loaded ObjectBox runtime library. '
109-
'Please visit https://objectbox.io/sync/ for options.')
105+
# TODO: Implement sync availability check
106+
# if not c.Sync.is_available():
107+
# raise RuntimeError(
108+
# 'Sync is not available in the loaded ObjectBox runtime library. '
109+
# 'Please visit https://objectbox.io/sync/ for options.')
110110

111111
self.__store = store
112112
self.__server_urls = server_urls
113113
self.__credentials = credentials
114114

115-
self.__c_sync_client_ptr = c.obx_sync_urls(store.c_store(), c_array_pointer(server_urls, ctypes.c_char_p),
115+
server_urls = [url.encode('utf-8') for url in server_urls]
116+
self.__c_sync_client_ptr = c.obx_sync_urls(store.c_store(), c.c_array_pointer(server_urls, ctypes.c_char_p),
116117
len(server_urls))
117118

118119
def set_credentials(self, credentials: SyncCredentials):
@@ -130,15 +131,34 @@ def set_credentials(self, credentials: SyncCredentials):
130131

131132
def set_request_updates_mode(self, mode: SyncRequestUpdatesMode):
132133
if mode == SyncRequestUpdatesMode.MANUAL:
133-
c_mode = c.OBXRequestUpdatesMode.MANUAL
134+
c_mode = c.RequestUpdatesMode.MANUAL
134135
elif mode == SyncRequestUpdatesMode.AUTO:
135-
c_mode = c.OBXRequestUpdatesMode.AUTO
136+
c_mode = c.RequestUpdatesMode.AUTO
136137
elif mode == SyncRequestUpdatesMode.AUTO_NO_PUSHES:
137-
c_mode = c.OBXRequestUpdatesMode.AUTO_NO_PUSHES
138+
c_mode = c.RequestUpdatesMode.AUTO_NO_PUSHES
138139
else:
139140
raise ValueError(f"Invalid mode: {mode}")
140141
c.obx_sync_request_updates_mode(self.__c_sync_client_ptr, c_mode)
141142

143+
def get_sync_state(self) -> SyncState:
144+
c_state = c.obx_sync_state(self.__c_sync_client_ptr)
145+
if c_state == c.SyncState.CREATED:
146+
return SyncState.CREATED
147+
elif c_state == c.SyncState.STARTED:
148+
return SyncState.STARTED
149+
elif c_state == c.SyncState.CONNECTED:
150+
return SyncState.CONNECTED
151+
elif c_state == c.SyncState.LOGGED_IN:
152+
return SyncState.LOGGED_IN
153+
elif c_state == c.SyncState.DISCONNECTED:
154+
return SyncState.DISCONNECTED
155+
elif c_state == c.SyncState.STOPPED:
156+
return SyncState.STOPPED
157+
elif c_state == c.SyncState.DEAD:
158+
return SyncState.DEAD
159+
else:
160+
return SyncState.UNKNOWN
161+
142162
def start(self):
143163
c.obx_sync_start(self.__c_sync_client_ptr)
144164

0 commit comments

Comments
 (0)