geobox 2.1.0__py3-none-any.whl → 2.2.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. geobox/__init__.py +61 -63
  2. geobox/aio/__init__.py +61 -63
  3. geobox/aio/api.py +491 -574
  4. geobox/aio/apikey.py +263 -263
  5. geobox/aio/attachment.py +341 -339
  6. geobox/aio/base.py +261 -262
  7. geobox/aio/basemap.py +196 -196
  8. geobox/aio/dashboard.py +340 -342
  9. geobox/aio/feature.py +35 -35
  10. geobox/aio/field.py +315 -321
  11. geobox/aio/file.py +72 -72
  12. geobox/aio/layout.py +340 -341
  13. geobox/aio/log.py +23 -23
  14. geobox/aio/map.py +1033 -1034
  15. geobox/aio/model3d.py +415 -415
  16. geobox/aio/mosaic.py +696 -696
  17. geobox/aio/plan.py +314 -314
  18. geobox/aio/query.py +693 -693
  19. geobox/aio/raster.py +88 -454
  20. geobox/aio/{analysis.py → raster_analysis.py} +153 -170
  21. geobox/aio/route.py +4 -4
  22. geobox/aio/scene.py +340 -342
  23. geobox/aio/settings.py +18 -18
  24. geobox/aio/task.py +404 -402
  25. geobox/aio/tile3d.py +337 -339
  26. geobox/aio/tileset.py +102 -103
  27. geobox/aio/usage.py +52 -51
  28. geobox/aio/user.py +506 -507
  29. geobox/aio/vector_tool.py +1968 -0
  30. geobox/aio/vectorlayer.py +316 -414
  31. geobox/aio/version.py +272 -273
  32. geobox/aio/view.py +1019 -983
  33. geobox/aio/workflow.py +340 -341
  34. geobox/api.py +14 -98
  35. geobox/apikey.py +262 -262
  36. geobox/attachment.py +336 -337
  37. geobox/base.py +384 -384
  38. geobox/basemap.py +194 -194
  39. geobox/dashboard.py +339 -341
  40. geobox/enums.py +31 -1
  41. geobox/feature.py +31 -10
  42. geobox/field.py +320 -320
  43. geobox/file.py +4 -4
  44. geobox/layout.py +339 -340
  45. geobox/log.py +4 -4
  46. geobox/map.py +1031 -1032
  47. geobox/model3d.py +410 -410
  48. geobox/mosaic.py +696 -696
  49. geobox/plan.py +313 -313
  50. geobox/query.py +691 -691
  51. geobox/raster.py +5 -368
  52. geobox/{analysis.py → raster_analysis.py} +108 -128
  53. geobox/scene.py +341 -342
  54. geobox/settings.py +194 -194
  55. geobox/task.py +399 -400
  56. geobox/tile3d.py +337 -338
  57. geobox/tileset.py +4 -4
  58. geobox/usage.py +3 -3
  59. geobox/user.py +503 -503
  60. geobox/vector_tool.py +1968 -0
  61. geobox/vectorlayer.py +5 -110
  62. geobox/version.py +272 -272
  63. geobox/view.py +981 -981
  64. geobox/workflow.py +338 -339
  65. {geobox-2.1.0.dist-info → geobox-2.2.1.dist-info}/METADATA +15 -1
  66. geobox-2.2.1.dist-info/RECORD +72 -0
  67. geobox-2.1.0.dist-info/RECORD +0 -70
  68. {geobox-2.1.0.dist-info → geobox-2.2.1.dist-info}/WHEEL +0 -0
  69. {geobox-2.1.0.dist-info → geobox-2.2.1.dist-info}/licenses/LICENSE +0 -0
  70. {geobox-2.1.0.dist-info → geobox-2.2.1.dist-info}/top_level.txt +0 -0
geobox/aio/user.py CHANGED
@@ -1,507 +1,506 @@
1
- from typing import List, Any, TYPE_CHECKING, Union, Dict
2
- from urllib.parse import urlencode, urljoin
3
-
4
- from .base import AsyncBase
5
- from ..utils import clean_data, xor_encode
6
- from ..enums import UserRole, UserStatus
7
- from .plan import Plan
8
-
9
- if TYPE_CHECKING:
10
- from . import AsyncGeoboxClient
11
- from ..api import GeoboxClient as SyncGeoboxClient
12
- from ..api import User as SyncUser
13
- from ..api import Session as SyncSession
14
-
15
-
16
- class User(AsyncBase):
17
-
18
- BASE_ENDPOINT: str = 'users/'
19
-
20
- def __init__(self, api: 'AsyncGeoboxClient', user_id: int, data: dict = {}) -> None:
21
- """
22
- Initialize a User instance.
23
-
24
- Args:
25
- api (AsyncGeoboxClient): The GeoboxClient instance for making requests.
26
- user_id (int): the id of the user
27
- data (Dict): The data of the user.
28
- """
29
- super().__init__(api, data=data)
30
- self.user_id = user_id
31
- self.endpoint = urljoin(self.BASE_ENDPOINT, f'{self.user_id}/') if self.user_id else 'me'
32
-
33
-
34
- def __repr__(self) -> str:
35
- """
36
- Return a string representation of the User instance.
37
-
38
- Returns:
39
- str: A string representation of the User instance.
40
- """
41
- return f'User(id={self.id}, first_name={self.first_name}, last_name={self.last_name})'
42
-
43
-
44
- @property
45
- def role(self) -> 'UserRole':
46
- """
47
- User role property
48
-
49
- Returns:
50
- UserRole: the user role
51
- """
52
- return UserRole(self.data.get('role')) if self.data.get('role') else None
53
-
54
-
55
- @property
56
- def status(self) -> 'UserStatus':
57
- """
58
- User status Property
59
-
60
- Returns:
61
- UserStatus: the user status
62
- """
63
- return UserStatus(self.data.get('status')) if self.data.get('status') else None
64
-
65
-
66
- @property
67
- def plan(self) -> 'Plan':
68
- """
69
- User plan Property
70
-
71
- Returns:
72
- Plan: the plan object
73
- """
74
- plan = self.data.get('plan', {})
75
- return Plan(self.api, plan.get('id'), plan) if plan else None
76
-
77
-
78
- @classmethod
79
- async def get_users(cls, api: 'AsyncGeoboxClient', **kwargs) -> Union[List['User'], int]:
80
- """
81
- [async] Retrieves a list of users (Permission Required)
82
-
83
- Args:
84
- api (AsyncGeoboxClient): The API instance.
85
-
86
- Keyword Args:
87
- status (UserStatus): the status of the users filter.
88
- q (str): query filter based on OGC CQL standard. e.g. "field1 LIKE '%GIS%' AND created_at > '2021-01-01'"
89
- search (str): search term for keyword-based searching among search_fields or all textual fields if search_fields does not have value. NOTE: if q param is defined this param will be ignored.
90
- search_fields (str): comma separated list of fields for searching.
91
- order_by (str): comma separated list of fields for sorting results [field1 A|D, field2 A|D, …]. e.g. name A, type D. NOTE: "A" denotes ascending order and "D" denotes descending order.
92
- return_count (bool): Whether to return total count. default is False.
93
- skip (int): Number of items to skip. default is 0.
94
- limit (int): Number of items to return. default is 10.
95
- user_id (int): Specific user. privileges required.
96
- shared (bool): Whether to return shared maps. default is False.
97
-
98
- Returns:
99
- List[User] | int: list of users or the count number.
100
-
101
- Example:
102
- >>> from geobox.aio import AsyncGeoboxClient
103
- >>> from geobox.aio.user import User
104
- >>> async with AsyncGeoboxClient() as client:
105
- >>> users = await User.get_users(client)
106
- or
107
- >>> users = await client.get_users()
108
- """
109
- params = {
110
- 'f': 'json',
111
- 'status': kwargs.get('status').value if kwargs.get('status') else None,
112
- 'q': kwargs.get('q'),
113
- 'search': kwargs.get('search'),
114
- 'search_fields': kwargs.get('search_fields'),
115
- 'order_by': kwargs.get('order_by'),
116
- 'return_count': kwargs.get('return_count', False),
117
- 'skip': kwargs.get('skip', 0),
118
- 'limit': kwargs.get('limit', 10),
119
- 'user_id': kwargs.get('user_id')
120
- }
121
- return await super()._get_list(api, cls.BASE_ENDPOINT, params, factory_func=lambda api, item: User(api, item['id'], item))
122
-
123
-
124
- @classmethod
125
- async def create_user(cls,
126
- api: 'AsyncGeoboxClient',
127
- username: str,
128
- email: str,
129
- password: str,
130
- role: 'UserRole',
131
- first_name: str,
132
- last_name: str,
133
- mobile: str,
134
- status: 'UserStatus') -> 'User':
135
- """
136
- [async] Create a User (Permission Required)
137
-
138
- Args:
139
- api (AsyncGeoboxClient): The GeoboxClient instance for making requests.
140
- username (str): the username of the user.
141
- email (str): the email of the user.
142
- password (str): the password of the user.
143
- role (UserRole): the role of the user.
144
- first_name (str): the firstname of the user.
145
- last_name (str): the lastname of the user.
146
- mobile (str): the mobile number of the user. e.g. "+98 9120123456".
147
- status (UserStatus): the status of the user.
148
-
149
- Returns:
150
- User: the user object.
151
-
152
- Example:
153
- >>> from geobox.aio import AsyncGeoboxClient
154
- >>> from geobox.aio.user import User
155
- >>> async with AsyncGeoboxClient() as client:
156
- >>> user = await User.create_user(client,
157
- ... username="user1",
158
- ... email="user1@example.com",
159
- ... password="P@ssw0rd",
160
- ... role=UserRole.ACCOUNT_ADMIN,
161
- ... first_name="user 1",
162
- ... last_name="user 1",
163
- ... mobile="+98 9120123456",
164
- ... status=UserStatus.ACTIVE)
165
- or
166
- >>> user = await client.create_user(username="user1",
167
- ... email="user1@example.com",
168
- ... password="P@ssw0rd",
169
- ... role=UserRole.ACCOUNT_ADMIN,
170
- ... first_name="user 1",
171
- ... last_name="user 1",
172
- ... mobile="+98 9120123456",
173
- ... status=UserStatus.ACTIVE)
174
- """
175
- data = {
176
- "username": username,
177
- "email": email,
178
- "password": xor_encode(password),
179
- "role": role.value,
180
- "first_name": first_name,
181
- "last_name": last_name,
182
- "mobile": mobile,
183
- "status": status.value
184
- }
185
- return await super()._create(api, cls.BASE_ENDPOINT, data, factory_func=lambda api, item: User(api, item['id'], item))
186
-
187
-
188
- @classmethod
189
- async def search_users(cls, api: 'AsyncGeoboxClient', search: str = None, skip: int = 0, limit: int = 10) -> List['User']:
190
- """
191
- [async] Get list of users based on the search term.
192
-
193
- Args:
194
- api (AsyncGeoboxClient): The AsyncGeoboxClient instance for making requests.
195
- search (str, optional): The Search Term.
196
- skip (int, optional): Number of items to skip. default is 0.
197
- limit (int, optional): Number of items to return. default is 10.
198
-
199
- Returns:
200
- List[User]: A list of User instances.
201
-
202
- Example:
203
- >>> from geobox.aio import AsyncGeoboxClient
204
- >>> from geobox.aio.user import User
205
- >>> async with AsyncGeoboxClient() as client:
206
- >>> users = await User.get_users(client, search="John")
207
- or
208
- >>> users = await client.get_users(search="John")
209
- """
210
- params = {
211
- 'search': search,
212
- 'skip': skip,
213
- 'limit': limit
214
- }
215
- endpoint = urljoin(cls.BASE_ENDPOINT, 'search/')
216
- return await super()._get_list(api, endpoint, params, factory_func=lambda api, item: User(api, item['id'], item))
217
-
218
-
219
- @classmethod
220
- async def get_user(cls, api: 'AsyncGeoboxClient', user_id: int = 'me') -> 'User':
221
- """
222
- [async] Get a user by its id (Permission Required)
223
-
224
- Args:
225
- api (AsyncGeoboxClient): The AsyncGeoboxClient instance for making requests.
226
- user_id (int, optional): Specific user. don't specify a user_id to get the current user.
227
-
228
- Returns:
229
- User: the user object.
230
-
231
- Raises:
232
- NotFoundError: If the user with the specified id is not found.
233
-
234
- Example:
235
- >>> from geobox.aio import AsyncGeoboxClient
236
- >>> from geobox.aio.user import User
237
- >>> async with AsyncGeoboxClient() as client:
238
- >>> user = await User.get_user(client, user_id=1)
239
- or
240
- >>> user = await client.get_user(user_id=1)
241
-
242
- get the current user
243
- >>> user = await User.get_user(client)
244
- or
245
- >>> user = await client.get_user()
246
- """
247
- params = {
248
- 'f': 'json'
249
- }
250
- return await super()._get_detail(api, cls.BASE_ENDPOINT, user_id, params, factory_func=lambda api, item: User(api, item['id'], item))
251
-
252
-
253
- async def update(self, **kwargs) -> Dict:
254
- """
255
- [async] Update the user (Permission Required)
256
-
257
- Keyword Args:
258
- username (str)
259
- email (str)
260
- first_name (str)
261
- last_name (str)
262
- mobile (str): e.g. "+98 9120123456"
263
- status (UserStatus)
264
- role (UserRole)
265
- plan (Plan)
266
- expiration_date (str)
267
-
268
- Returns:
269
- Dict: updated data
270
-
271
- Example:
272
- >>> from geobox.aio import AsyncGeoboxClient
273
- >>> from geobox.aio.user import User
274
- >>> async with AsyncGeoboxClient() as client:
275
- >>> user = await User.get_user(client, user_id=1)
276
- >>> await user.update(status=UserStatus.PENDING)
277
- """
278
- data = {
279
- "username": kwargs.get('username'),
280
- "email": kwargs.get('email'),
281
- "first_name": kwargs.get('first_name'),
282
- "last_name": kwargs.get('last_name'),
283
- "status": kwargs.get('status').value if kwargs.get('status') else None,
284
- "role": kwargs.get('role').value if kwargs.get('role') else None,
285
- }
286
- data = clean_data(data)
287
-
288
- try:
289
- data['mobile'] = None if kwargs['mobile'] == '' else kwargs['mobile']
290
- except:
291
- pass
292
-
293
- try:
294
- data['plan_id'] = None if kwargs['plan'] == '' else kwargs['plan'].id
295
- except:
296
- pass
297
-
298
- try:
299
- data['expiration_date'] = None if kwargs['expiration_date'] == '' else kwargs['expiration_date']
300
- except:
301
- pass
302
-
303
- response = await self.api.put(self.endpoint, data)
304
- self._update_properties(response)
305
- return response
306
-
307
-
308
- async def delete(self) -> None:
309
- """
310
- [async] Delete the user (Permission Required)
311
-
312
- Returns:
313
- None
314
-
315
- Example:
316
- >>> from geobox.aio import AsyncGeoboxClient
317
- >>> from geobox.aio.user import User
318
- >>> async with AsyncGeoboxClient() as client:
319
- >>> user = await User.get_user(client, user_id=1)
320
- >>> await user.delete()
321
- """
322
- await super().delete(self.endpoint)
323
-
324
-
325
- async def get_sessions(self, user_id: int = 'me') -> List['Session']:
326
- """
327
- [async] Get a list of user available sessions (Permission Required)
328
-
329
- Args:
330
- user_id (int, optional): Specific user. don't specify user_id to get the current user.
331
-
332
- Returns:
333
- List[Session]: list of user sessions.
334
-
335
- Example:
336
- >>> from geobox.aio import AsyncGeoboxClient
337
- >>> from geobox.aio.user import User
338
- >>> async with AsyncGeoboxClient() as client:
339
- >>> user = await User.get_user(client, user_id=1)
340
- or
341
- >>> user = await client.get_user(user_id=1)
342
-
343
- >>> await user.get_sessions()
344
- or
345
- >>> await client.get_sessions()
346
- """
347
- params = clean_data({
348
- 'f': 'json'
349
- })
350
- query_string = urlencode(params)
351
- if user_id != 'me':
352
- user = await self.get_user(self.api, user_id=user_id)
353
- endpoint = f"{self.BASE_ENDPOINT}{user_id}/sessions/?{query_string}"
354
- else:
355
- user = self
356
- endpoint = urljoin(self.endpoint, f'sessions/?{query_string}')
357
-
358
- response = await self.api.get(endpoint)
359
- return [Session(item['uuid'], item, user) for item in response]
360
-
361
-
362
- async def change_password(self, new_password: str) -> None:
363
- """
364
- [async] Change the user password (privileges required)
365
-
366
- Args:
367
- new_password (str): new password for the user.
368
-
369
- Returns:
370
- None
371
-
372
- Example:
373
- >>> from geobox.aio import AsyncGeoboxClient
374
- >>> from geobox.aio.user import User
375
- >>> async with AsyncGeoboxClient() as client:
376
- >>> user = await client.get_user(user_id=1)
377
- >>> await user.change_password(new_password='user_new_password')
378
- """
379
- data = clean_data({
380
- "new_password": xor_encode(new_password)
381
- })
382
- endpoint = urljoin(self.endpoint, 'change-password')
383
- await self.api.post(endpoint, data, is_json=False)
384
-
385
-
386
- async def renew_plan(self) -> None:
387
- """
388
- [async] Renew the user plan (privileges required)
389
-
390
- Returns:
391
- None
392
-
393
- Example:
394
- >>> from geobox.aio import AsyncGeoboxClient
395
- >>> async with AsyncGeoboxClient() as client:
396
- >>> user = await client.get_user(user_id=1)
397
- >>> await user.renew_plan()
398
- """
399
- endpoint = urljoin(self.endpoint, 'renewPlan')
400
- await self.api.post(endpoint)
401
-
402
-
403
- def to_sync(self, sync_client: 'SyncGeoboxClient') -> 'SyncUser':
404
- """
405
- Switch to sync version of the user instance to have access to the sync methods
406
-
407
- Args:
408
- sync_client (SyncGeoboxClient): The sync version of the GeoboxClient instance for making requests.
409
-
410
- Returns:
411
- geobox.user.User: the async instance of the user.
412
-
413
- Example:
414
- >>> from geobox import Geoboxclient
415
- >>> from geobox.aio import AsyncGeoboxClient
416
- >>> from geobox.aio.user import User
417
- >>> client = GeoboxClient()
418
- >>> async with AsyncGeoboxClient() as async_client:
419
- >>> user = await User.get_user(async_client) # without user_id parameter, it gets the current user
420
- or
421
- >>> user = await async_client.get_user() # without user_id parameter, it gets the current user
422
- >>> sync_user = user.to_sync(client)
423
- """
424
- from ..user import User as SyncUser
425
-
426
- return SyncUser(api=sync_client, user_id=self.user_id, data=self.data)
427
-
428
-
429
-
430
- class Session(AsyncBase):
431
- def __init__(self, uuid: str, data: Dict, user: 'User'):
432
- """
433
- Initialize a user session instance.
434
-
435
- Args:
436
- uuid (str): The unique identifier for the user session.
437
- data (Dict): The data of the session.
438
- user (User): the user instance.
439
- """
440
- self.uuid = uuid
441
- self.data = data
442
- self.user = user
443
- self.endpoint = urljoin(self.user.endpoint, f'sessions/{self.uuid}')
444
-
445
-
446
- def __repr__(self) -> str:
447
- """
448
- Return a string representation of the resource.
449
-
450
- Returns:
451
- str: A string representation of the Session object.
452
- """
453
- return f"Session(user={self.user}, agent='{self.agent}')"
454
-
455
-
456
- async def close(self) -> None:
457
- """
458
- [async] Close the user session
459
-
460
- Returns:
461
- None
462
-
463
- Example:
464
- >>> from geobox.aio import AsyncGeoboxClient
465
- >>> from geobox.aio.user import User
466
- >>> async with AsyncGeoboxClient() as client:
467
- >>> user = await User.get_user(client) # without user_id parameter, it gets the current user
468
- or
469
- >>> user = await client.get_user() # without user_id parameter, it gets the current user
470
- >>> sessions = await user.get_sessions()
471
- >>> session = session[0]
472
- >>> await session.close()
473
- """
474
- data = clean_data({
475
- 'user_id': self.user.user_id,
476
- 'session_uuid': self.uuid
477
- })
478
- await self.user.api.post(self.endpoint, data)
479
-
480
-
481
- def to_sync(self, sync_client: 'SyncGeoboxClient') -> 'SyncSession':
482
- """
483
- Switch to sync version of the session instance to have access to the sync methods
484
-
485
- Args:
486
- sync_client (SyncGeoboxClient): The sync version of the GeoboxClient instance for making requests.
487
-
488
- Returns:
489
- geobox.user.Session: the sync instance of the session.
490
-
491
- Example:
492
- >>> from geobox import Geoboxclient
493
- >>> from geobox.aio import AsyncGeoboxClient
494
- >>> from geobox.aio.user import User
495
- >>> client = GeoboxClient()
496
- >>> async with AsyncGeoboxClient() as async_client:
497
- >>> user = await User.get_user(async_client) # without user_id parameter, it gets the current user
498
- or
499
- >>> user = await async_client.get_user() # without user_id parameter, it gets the current user
500
- >>> sessions = await user.get_sessions()
501
- >>> session = sessions[0]
502
- >>> sync_session = session.to_sync(client)
503
- """
504
- from ..user import Session as SyncSession
505
-
506
- sync_user = self.user.to_sync(sync_client)
507
- return SyncSession(uuid=self.uuid, data=self.data, user=sync_user)
1
+ from typing import List, TYPE_CHECKING, Union, Dict
2
+ from urllib.parse import urlencode, urljoin
3
+
4
+ from .base import AsyncBase
5
+ from ..utils import clean_data, xor_encode
6
+ from ..enums import UserRole, UserStatus
7
+ from .plan import AsyncPlan
8
+
9
+ if TYPE_CHECKING:
10
+ from . import AsyncGeoboxClient
11
+ from ..api import GeoboxClient
12
+ from ..api import User
13
+ from ..api import Session
14
+
15
+
16
+ class AsyncUser(AsyncBase):
17
+
18
+ BASE_ENDPOINT: str = 'users/'
19
+
20
+ def __init__(self, api: 'AsyncGeoboxClient', user_id: int, data: dict = {}) -> None:
21
+ """
22
+ Initialize a User instance.
23
+
24
+ Args:
25
+ api (AsyncGeoboxClient): The GeoboxClient instance for making requests.
26
+ user_id (int): the id of the user
27
+ data (Dict): The data of the user.
28
+ """
29
+ super().__init__(api, data=data)
30
+ self.user_id = user_id
31
+ self.endpoint = urljoin(self.BASE_ENDPOINT, f'{self.user_id}/') if self.user_id else 'me'
32
+
33
+
34
+ def __repr__(self) -> str:
35
+ """
36
+ Return a string representation of the User instance.
37
+
38
+ Returns:
39
+ str: A string representation of the User instance.
40
+ """
41
+ return f'AsyncUser(id={self.id}, first_name={self.first_name}, last_name={self.last_name})'
42
+
43
+
44
+ @property
45
+ def role(self) -> 'UserRole':
46
+ """
47
+ User role property
48
+
49
+ Returns:
50
+ UserRole: the user role
51
+ """
52
+ return UserRole(self.data.get('role')) if self.data.get('role') else None
53
+
54
+
55
+ @property
56
+ def status(self) -> 'UserStatus':
57
+ """
58
+ User status Property
59
+
60
+ Returns:
61
+ UserStatus: the user status
62
+ """
63
+ return UserStatus(self.data.get('status')) if self.data.get('status') else None
64
+
65
+
66
+ @property
67
+ def plan(self) -> 'AsyncPlan':
68
+ """
69
+ User plan Property
70
+
71
+ Returns:
72
+ Plan: the plan object
73
+ """
74
+ plan = self.data.get('plan', {})
75
+ return AsyncPlan(self.api, plan.get('id'), plan) if plan else None
76
+
77
+
78
+ @classmethod
79
+ async def get_users(cls, api: 'AsyncGeoboxClient', **kwargs) -> Union[List['AsyncUser'], int]:
80
+ """
81
+ [async] Retrieves a list of users (Permission Required)
82
+
83
+ Args:
84
+ api (AsyncGeoboxClient): The API instance.
85
+
86
+ Keyword Args:
87
+ status (UserStatus): the status of the users filter.
88
+ q (str): query filter based on OGC CQL standard. e.g. "field1 LIKE '%GIS%' AND created_at > '2021-01-01'"
89
+ search (str): search term for keyword-based searching among search_fields or all textual fields if search_fields does not have value. NOTE: if q param is defined this param will be ignored.
90
+ search_fields (str): comma separated list of fields for searching.
91
+ order_by (str): comma separated list of fields for sorting results [field1 A|D, field2 A|D, …]. e.g. name A, type D. NOTE: "A" denotes ascending order and "D" denotes descending order.
92
+ return_count (bool): Whether to return total count. default is False.
93
+ skip (int): Number of items to skip. default is 0.
94
+ limit (int): Number of items to return. default is 10.
95
+ user_id (int): Specific user. privileges required.
96
+ shared (bool): Whether to return shared maps. default is False.
97
+
98
+ Returns:
99
+ List[AsyncUser] | int: list of users or the count number.
100
+
101
+ Example:
102
+ >>> from geobox.aio import AsyncGeoboxClient
103
+ >>> from geobox.aio.user import AsyncUser
104
+ >>> async with AsyncGeoboxClient() as client:
105
+ >>> users = await AsyncUser.get_users(client)
106
+ or
107
+ >>> users = await client.get_users()
108
+ """
109
+ params = {
110
+ 'f': 'json',
111
+ 'status': kwargs.get('status').value if kwargs.get('status') else None,
112
+ 'q': kwargs.get('q'),
113
+ 'search': kwargs.get('search'),
114
+ 'search_fields': kwargs.get('search_fields'),
115
+ 'order_by': kwargs.get('order_by'),
116
+ 'return_count': kwargs.get('return_count', False),
117
+ 'skip': kwargs.get('skip', 0),
118
+ 'limit': kwargs.get('limit', 10),
119
+ 'user_id': kwargs.get('user_id')
120
+ }
121
+ return await super()._get_list(api, cls.BASE_ENDPOINT, params, factory_func=lambda api, item: AsyncUser(api, item['id'], item))
122
+
123
+
124
+ @classmethod
125
+ async def create_user(cls,
126
+ api: 'AsyncGeoboxClient',
127
+ username: str,
128
+ email: str,
129
+ password: str,
130
+ role: 'UserRole',
131
+ first_name: str,
132
+ last_name: str,
133
+ mobile: str,
134
+ status: 'UserStatus') -> 'AsyncUser':
135
+ """
136
+ [async] Create a User (Permission Required)
137
+
138
+ Args:
139
+ api (AsyncGeoboxClient): The GeoboxClient instance for making requests.
140
+ username (str): the username of the user.
141
+ email (str): the email of the user.
142
+ password (str): the password of the user.
143
+ role (UserRole): the role of the user.
144
+ first_name (str): the firstname of the user.
145
+ last_name (str): the lastname of the user.
146
+ mobile (str): the mobile number of the user. e.g. "+98 9120123456".
147
+ status (UserStatus): the status of the user.
148
+
149
+ Returns:
150
+ AsyncUser: the user object.
151
+
152
+ Example:
153
+ >>> from geobox.aio import AsyncGeoboxClient
154
+ >>> from geobox.aio.user import AsyncUser
155
+ >>> async with AsyncGeoboxClient() as client:
156
+ >>> user = await AsyncUser.create_user(client,
157
+ ... username="user1",
158
+ ... email="user1@example.com",
159
+ ... password="P@ssw0rd",
160
+ ... role=UserRole.ACCOUNT_ADMIN,
161
+ ... first_name="user 1",
162
+ ... last_name="user 1",
163
+ ... mobile="+98 9120123456",
164
+ ... status=UserStatus.ACTIVE)
165
+ or
166
+ >>> user = await client.create_user(username="user1",
167
+ ... email="user1@example.com",
168
+ ... password="P@ssw0rd",
169
+ ... role=UserRole.ACCOUNT_ADMIN,
170
+ ... first_name="user 1",
171
+ ... last_name="user 1",
172
+ ... mobile="+98 9120123456",
173
+ ... status=UserStatus.ACTIVE)
174
+ """
175
+ data = {
176
+ "username": username,
177
+ "email": email,
178
+ "password": xor_encode(password),
179
+ "role": role.value,
180
+ "first_name": first_name,
181
+ "last_name": last_name,
182
+ "mobile": mobile,
183
+ "status": status.value
184
+ }
185
+ return await super()._create(api, cls.BASE_ENDPOINT, data, factory_func=lambda api, item: AsyncUser(api, item['id'], item))
186
+
187
+
188
+ @classmethod
189
+ async def search_users(cls, api: 'AsyncGeoboxClient', search: str = None, skip: int = 0, limit: int = 10) -> List['AsyncUser']:
190
+ """
191
+ [async] Get list of users based on the search term.
192
+
193
+ Args:
194
+ api (AsyncGeoboxClient): The AsyncGeoboxClient instance for making requests.
195
+ search (str, optional): The Search Term.
196
+ skip (int, optional): Number of items to skip. default is 0.
197
+ limit (int, optional): Number of items to return. default is 10.
198
+
199
+ Returns:
200
+ List[AsyncUser]: A list of User instances.
201
+
202
+ Example:
203
+ >>> from geobox.aio import AsyncGeoboxClient
204
+ >>> from geobox.aio.user import AsyncUser
205
+ >>> async with AsyncGeoboxClient() as client:
206
+ >>> users = await AsyncUser.get_users(client, search="John")
207
+ or
208
+ >>> users = await client.get_users(search="John")
209
+ """
210
+ params = {
211
+ 'search': search,
212
+ 'skip': skip,
213
+ 'limit': limit
214
+ }
215
+ endpoint = urljoin(cls.BASE_ENDPOINT, 'search/')
216
+ return await super()._get_list(api, endpoint, params, factory_func=lambda api, item: AsyncUser(api, item['id'], item))
217
+
218
+
219
+ @classmethod
220
+ async def get_user(cls, api: 'AsyncGeoboxClient', user_id: int = 'me') -> 'AsyncUser':
221
+ """
222
+ [async] Get a user by its id (Permission Required)
223
+
224
+ Args:
225
+ api (AsyncGeoboxClient): The AsyncGeoboxClient instance for making requests.
226
+ user_id (int, optional): Specific user. don't specify a user_id to get the current user.
227
+
228
+ Returns:
229
+ AsyncUser: the user object.
230
+
231
+ Raises:
232
+ NotFoundError: If the user with the specified id is not found.
233
+
234
+ Example:
235
+ >>> from geobox.aio import AsyncGeoboxClient
236
+ >>> from geobox.aio.user import AsyncUser
237
+ >>> async with AsyncGeoboxClient() as client:
238
+ >>> user = await AsyncUser.get_user(client, user_id=1)
239
+ or
240
+ >>> user = await client.get_user(user_id=1)
241
+
242
+ get the current user
243
+ >>> user = await AsyncUser.get_user(client)
244
+ or
245
+ >>> user = await client.get_user()
246
+ """
247
+ params = {
248
+ 'f': 'json'
249
+ }
250
+ return await super()._get_detail(api, cls.BASE_ENDPOINT, user_id, params, factory_func=lambda api, item: AsyncUser(api, item['id'], item))
251
+
252
+
253
+ async def update(self, **kwargs) -> Dict:
254
+ """
255
+ [async] Update the user (Permission Required)
256
+
257
+ Keyword Args:
258
+ username (str)
259
+ email (str)
260
+ first_name (str)
261
+ last_name (str)
262
+ mobile (str): e.g. "+98 9120123456"
263
+ status (UserStatus)
264
+ role (UserRole)
265
+ plan (Plan)
266
+ expiration_date (str)
267
+
268
+ Returns:
269
+ Dict: updated data
270
+
271
+ Example:
272
+ >>> from geobox.aio import AsyncGeoboxClient
273
+ >>> from geobox.aio.user import AsyncUser
274
+ >>> async with AsyncGeoboxClient() as client:
275
+ >>> user = await AsyncUser.get_user(client, user_id=1)
276
+ >>> await user.update(status=UserStatus.PENDING)
277
+ """
278
+ data = {
279
+ "username": kwargs.get('username'),
280
+ "email": kwargs.get('email'),
281
+ "first_name": kwargs.get('first_name'),
282
+ "last_name": kwargs.get('last_name'),
283
+ "status": kwargs.get('status').value if kwargs.get('status') else None,
284
+ "role": kwargs.get('role').value if kwargs.get('role') else None,
285
+ }
286
+ data = clean_data(data)
287
+
288
+ try:
289
+ data['mobile'] = None if kwargs['mobile'] == '' else kwargs['mobile']
290
+ except:
291
+ pass
292
+
293
+ try:
294
+ data['plan_id'] = None if kwargs['plan'] == '' else kwargs['plan'].id
295
+ except:
296
+ pass
297
+
298
+ try:
299
+ data['expiration_date'] = None if kwargs['expiration_date'] == '' else kwargs['expiration_date']
300
+ except:
301
+ pass
302
+
303
+ response = await self.api.put(self.endpoint, data)
304
+ self._update_properties(response)
305
+ return response
306
+
307
+
308
+ async def delete(self) -> None:
309
+ """
310
+ [async] Delete the user (Permission Required)
311
+
312
+ Returns:
313
+ None
314
+
315
+ Example:
316
+ >>> from geobox.aio import AsyncGeoboxClient
317
+ >>> from geobox.aio.user import AsyncUser
318
+ >>> async with AsyncGeoboxClient() as client:
319
+ >>> user = await AsyncUser.get_user(client, user_id=1)
320
+ >>> await user.delete()
321
+ """
322
+ await super()._delete(self.endpoint)
323
+
324
+
325
+ async def get_sessions(self, user_id: int = 'me') -> List['AsyncSession']:
326
+ """
327
+ [async] Get a list of user available sessions (Permission Required)
328
+
329
+ Args:
330
+ user_id (int, optional): Specific user. don't specify user_id to get the current user.
331
+
332
+ Returns:
333
+ List[AsyncSession]: list of user sessions.
334
+
335
+ Example:
336
+ >>> from geobox.aio import AsyncGeoboxClient
337
+ >>> from geobox.aio.user import AsyncUser
338
+ >>> async with AsyncGeoboxClient() as client:
339
+ >>> user = await AsyncUser.get_user(client, user_id=1)
340
+ or
341
+ >>> user = await client.get_user(user_id=1)
342
+
343
+ >>> await user.get_sessions()
344
+ or
345
+ >>> await client.get_sessions()
346
+ """
347
+ params = clean_data({
348
+ 'f': 'json'
349
+ })
350
+ query_string = urlencode(params)
351
+ if user_id != 'me':
352
+ user = await self.get_user(self.api, user_id=user_id)
353
+ endpoint = f"{self.BASE_ENDPOINT}{user_id}/sessions/?{query_string}"
354
+ else:
355
+ user = self
356
+ endpoint = urljoin(self.endpoint, f'sessions/?{query_string}')
357
+
358
+ response = await self.api.get(endpoint)
359
+ return [AsyncSession(item['uuid'], item, user) for item in response]
360
+
361
+
362
+ async def change_password(self, new_password: str) -> None:
363
+ """
364
+ [async] Change the user password (privileges required)
365
+
366
+ Args:
367
+ new_password (str): new password for the user.
368
+
369
+ Returns:
370
+ None
371
+
372
+ Example:
373
+ >>> from geobox.aio import AsyncGeoboxClient
374
+ >>> async with AsyncGeoboxClient() as client:
375
+ >>> user = await client.get_user(user_id=1)
376
+ >>> await user.change_password(new_password='user_new_password')
377
+ """
378
+ data = clean_data({
379
+ "new_password": xor_encode(new_password)
380
+ })
381
+ endpoint = urljoin(self.endpoint, 'change-password')
382
+ await self.api.post(endpoint, data, is_json=False)
383
+
384
+
385
+ async def renew_plan(self) -> None:
386
+ """
387
+ [async] Renew the user plan (privileges required)
388
+
389
+ Returns:
390
+ None
391
+
392
+ Example:
393
+ >>> from geobox.aio import AsyncGeoboxClient
394
+ >>> async with AsyncGeoboxClient() as client:
395
+ >>> user = await client.get_user(user_id=1)
396
+ >>> await user.renew_plan()
397
+ """
398
+ endpoint = urljoin(self.endpoint, 'renewPlan')
399
+ await self.api.post(endpoint)
400
+
401
+
402
+ def to_sync(self, sync_client: 'GeoboxClient') -> 'User':
403
+ """
404
+ Switch to sync version of the user instance to have access to the sync methods
405
+
406
+ Args:
407
+ sync_client (GeoboxClient): The sync version of the GeoboxClient instance for making requests.
408
+
409
+ Returns:
410
+ User: the async instance of the user.
411
+
412
+ Example:
413
+ >>> from geobox import Geoboxclient
414
+ >>> from geobox.aio import AsyncGeoboxClient
415
+ >>> from geobox.aio.user import AsyncUser
416
+ >>> client = GeoboxClient()
417
+ >>> async with AsyncGeoboxClient() as async_client:
418
+ >>> user = await AsyncUser.get_user(async_client) # without user_id parameter, it gets the current user
419
+ or
420
+ >>> user = await async_client.get_user() # without user_id parameter, it gets the current user
421
+ >>> sync_user = user.to_sync(client)
422
+ """
423
+ from ..user import User
424
+
425
+ return User(api=sync_client, user_id=self.user_id, data=self.data)
426
+
427
+
428
+
429
+ class AsyncSession(AsyncBase):
430
+ def __init__(self, uuid: str, data: Dict, user: 'AsyncUser'):
431
+ """
432
+ Initialize a user session instance.
433
+
434
+ Args:
435
+ uuid (str): The unique identifier for the user session.
436
+ data (Dict): The data of the session.
437
+ user (User): the user instance.
438
+ """
439
+ self.uuid = uuid
440
+ self.data = data
441
+ self.user = user
442
+ self.endpoint = urljoin(self.user.endpoint, f'sessions/{self.uuid}')
443
+
444
+
445
+ def __repr__(self) -> str:
446
+ """
447
+ Return a string representation of the resource.
448
+
449
+ Returns:
450
+ str: A string representation of the Session object.
451
+ """
452
+ return f"AsyncSession(user={self.user}, agent='{self.agent}')"
453
+
454
+
455
+ async def close(self) -> None:
456
+ """
457
+ [async] Close the user session
458
+
459
+ Returns:
460
+ None
461
+
462
+ Example:
463
+ >>> from geobox.aio import AsyncGeoboxClient
464
+ >>> from geobox.aio.user import AsyncUser
465
+ >>> async with AsyncGeoboxClient() as client:
466
+ >>> user = await AsyncUser.get_user(client) # without user_id parameter, it gets the current user
467
+ or
468
+ >>> user = await client.get_user() # without user_id parameter, it gets the current user
469
+ >>> sessions = await user.get_sessions()
470
+ >>> session = session[0]
471
+ >>> await session.close()
472
+ """
473
+ data = clean_data({
474
+ 'user_id': self.user.user_id,
475
+ 'session_uuid': self.uuid
476
+ })
477
+ await self.user.api.post(self.endpoint, data)
478
+
479
+
480
+ def to_sync(self, sync_client: 'GeoboxClient') -> 'Session':
481
+ """
482
+ Switch to sync version of the session instance to have access to the sync methods
483
+
484
+ Args:
485
+ sync_client (GeoboxClient): The sync version of the GeoboxClient instance for making requests.
486
+
487
+ Returns:
488
+ Session: the sync instance of the session.
489
+
490
+ Example:
491
+ >>> from geobox import Geoboxclient
492
+ >>> from geobox.aio import AsyncGeoboxClient
493
+ >>> from geobox.aio.user import AsyncUser
494
+ >>> client = GeoboxClient()
495
+ >>> async with AsyncGeoboxClient() as async_client:
496
+ >>> user = await AsyncUser.get_user(async_client) # without user_id parameter, it gets the current user
497
+ or
498
+ >>> user = await async_client.get_user() # without user_id parameter, it gets the current user
499
+ >>> sessions = await user.get_sessions()
500
+ >>> session = sessions[0]
501
+ >>> sync_session = session.to_sync(client)
502
+ """
503
+ from ..user import Session
504
+
505
+ sync_user = self.user.to_sync(sync_client)
506
+ return Session(uuid=self.uuid, data=self.data, user=sync_user)