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/user.py CHANGED
@@ -1,504 +1,504 @@
1
- from typing import List, Any, TYPE_CHECKING, Union, Dict
2
- from urllib.parse import urlencode, urljoin
3
-
4
- from .base import Base
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 GeoboxClient
11
- from .aio import AsyncGeoboxClient
12
- from .aio.user import Session as AsyncSession
13
- from .aio.user import User as AsyncUser
14
-
15
-
16
- class User(Base):
17
-
18
- BASE_ENDPOINT: str = 'users/'
19
-
20
- def __init__(self, api: 'GeoboxClient', user_id: int, data: dict = {}) -> None:
21
- """
22
- Initialize a User instance.
23
-
24
- Args:
25
- api (GeoboxClient): 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
- def get_users(cls, api: 'GeoboxClient', **kwargs) -> Union[List['User'], int]:
80
- """
81
- Retrieves a list of users (Permission Required)
82
-
83
- Args:
84
- api (GeoboxClient): 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 import Geoboxclient
103
- >>> from geobox.user import User
104
- >>> client = GeoboxClient()
105
- >>> users = User.get_users(client)
106
- or
107
- >>> users = 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 super()._get_list(api, cls.BASE_ENDPOINT, params, factory_func=lambda api, item: User(api, item['id'], item))
122
-
123
-
124
- @classmethod
125
- def create_user(cls,
126
- api: 'GeoboxClient',
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
- Create a User (Permission Required)
137
-
138
- Args:
139
- api (GeoboxClient): 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 import GeoboxClient
154
- >>> from geobox.user import User
155
- >>> client = GeoboxClient()
156
- >>> user = 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 = 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 super()._create(api, cls.BASE_ENDPOINT, data, factory_func=lambda api, item: User(api, item['id'], item))
186
-
187
-
188
- @classmethod
189
- def search_users(cls, api: 'GeoboxClient', search: str = None, skip: int = 0, limit: int = 10) -> List['User']:
190
- """
191
- Get list of users based on the search term.
192
-
193
- Args:
194
- api (GeoboxClient): The GeoboxClient 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 import GeoboxClient
204
- >>> from geobox.user import User
205
- >>> client = GeoboxClient()
206
- >>> users = User.get_users(client, search="John")
207
- or
208
- >>> users = 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 super()._get_list(api, endpoint, params, factory_func=lambda api, item: User(api, item['id'], item))
217
-
218
-
219
- @classmethod
220
- def get_user(cls, api: 'GeoboxClient', user_id: int = 'me') -> 'User':
221
- """
222
- Get a user by its id (Permission Required)
223
-
224
- Args:
225
- api (GeoboxClient): The GeoboxClient 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 import GeoboxClient
236
- >>> from geobox.user import User
237
- >>> client = GeoboxClient()
238
- >>> user = User.get_user(client, user_id=1)
239
- or
240
- >>> user = client.get_user(user_id=1)
241
-
242
- get the current user
243
- >>> user = User.get_user(client)
244
- or
245
- >>> user = client.get_user()
246
- """
247
- params = {
248
- 'f': 'json'
249
- }
250
- return super()._get_detail(api, cls.BASE_ENDPOINT, user_id, params, factory_func=lambda api, item: User(api, item['id'], item))
251
-
252
-
253
- def update(self, **kwargs) -> Dict:
254
- """
255
- 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 imoprt GeoboxClient
273
- >>> from geobox.user import User
274
- >>> client = GeoboxClient()
275
- >>> user = User.get_user(client, user_id=1)
276
- >>> 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 = self.api.put(self.endpoint, data)
304
- self._update_properties(response)
305
- return response
306
-
307
-
308
- def delete(self) -> None:
309
- """
310
- Delete the user (Permission Required)
311
-
312
- Returns:
313
- None
314
-
315
- Example:
316
- >>> from geobox import GeoboxClient
317
- >>> from geobox.user import User
318
- >>> client = GeoboxClient()
319
- >>> user = User.get_user(client, user_id=1)
320
- >>> user.delete()
321
- """
322
- super().delete(self.endpoint)
323
-
324
-
325
- def get_sessions(self, user_id: int = 'me') -> List['Session']:
326
- """
327
- 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 import GeoboxClient
337
- >>> from geobox.user import User
338
- >>> client = GeoboxClient()
339
- >>> user = User.get_user(client, user_id=1)
340
- or
341
- >>> user = client.get_user(user_id=1)
342
-
343
- >>> user.get_sessions()
344
- or
345
- >>> client.get_sessions()
346
- """
347
- params = clean_data({
348
- 'f': 'json'
349
- })
350
- query_string = urlencode(params)
351
- if user_id != 'me':
352
- user = 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 = self.api.get(endpoint)
359
- return [Session(item['uuid'], item, user) for item in response]
360
-
361
-
362
- def change_password(self, new_password: str) -> None:
363
- """
364
- 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 import GeoboxClient
374
- >>> from geobox.user import User
375
- >>> client = GeoboxClient()
376
- >>> user = client.get_user(user_id=1)
377
- >>> 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
- self.api.post(endpoint, data, is_json=False)
384
-
385
-
386
- def renew_plan(self) -> None:
387
- """
388
- Renew the user plan (privileges required)
389
-
390
- Returns:
391
- None
392
-
393
- Example:
394
- >>> from geobox import GeoboxClient
395
- >>> user = client.get_user(user_id=1)
396
- >>> user.renew_plan()
397
- """
398
- endpoint = urljoin(self.endpoint, 'renewPlan')
399
- self.api.post(endpoint)
400
-
401
-
402
- def to_async(self, async_client: 'AsyncGeoboxClient') -> 'AsyncUser':
403
- """
404
- Switch to async version of the user instance to have access to the async methods
405
-
406
- Args:
407
- async_client (AsyncGeoboxClient): The async version of the GeoboxClient instance for making requests.
408
-
409
- Returns:
410
- geobox.aio.user.User: the async instance of the user.
411
-
412
- Example:
413
- >>> from geobox import Geoboxclient
414
- >>> from geobox.aio import AsyncGeoboxClient
415
- >>> from geobox.user import User
416
- >>> client = GeoboxClient()
417
- >>> user = User.get_user(client) # without user_id parameter, it gets the current user
418
- or
419
- >>> user = client.get_user() # without user_id parameter, it gets the current user
420
- >>> async with AsyncGeoboxClient() as async_client:
421
- >>> async_user = user.to_async(async_client)
422
- """
423
- from .aio.user import User as AsyncUser
424
-
425
- return AsyncUser(api=async_client, user_id=self.user_id, data=self.data)
426
-
427
-
428
-
429
- class Session(Base):
430
- def __init__(self, uuid: str, data: Dict, user: 'User'):
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"Session(user={self.user}, agent='{self.agent}')"
453
-
454
-
455
- def close(self) -> None:
456
- """
457
- Close the user session
458
-
459
- Returns:
460
- None
461
-
462
- Example:
463
- >>> from geobox import geoboxClient
464
- >>> from geobox.user import User
465
- >>> client = GeoboxClient()
466
- >>> user = User.get_user(client) # without user_id parameter, it gets the current user
467
- or
468
- >>> user = client.get_user() # without user_id parameter, it gets the current user
469
- >>> session = user.get_sessions()[0]
470
- >>> session.close()
471
- """
472
- data = clean_data({
473
- 'user_id': self.user.user_id,
474
- 'session_uuid': self.uuid
475
- })
476
- self.user.api.post(self.endpoint, data)
477
-
478
-
479
- def to_async(self, async_client: 'AsyncGeoboxClient') -> 'AsyncSession':
480
- """
481
- Switch to async version of the session instance to have access to the async methods
482
-
483
- Args:
484
- async_client (AsyncGeoboxClient): The async version of the GeoboxClient instance for making requests.
485
-
486
- Returns:
487
- geobox.aio.user.Session: the async instance of the session.
488
-
489
- Example:
490
- >>> from geobox import Geoboxclient
491
- >>> from geobox.aio import AsyncGeoboxClient
492
- >>> from geobox.user import User
493
- >>> client = GeoboxClient()
494
- >>> user = User.get_user(client) # without user_id parameter, it gets the current user
495
- or
496
- >>> user = client.get_user() # without user_id parameter, it gets the current user
497
- >>> session = user.get_sessions()[0]
498
- >>> async with AsyncGeoboxClient() as async_client:
499
- >>> async_session = session.to_async(async_client)
500
- """
501
- from .aio.user import Session as AsyncSession
502
-
503
- async_user = self.user.to_async(async_client=async_client)
1
+ from typing import List, Any, TYPE_CHECKING, Union, Dict
2
+ from urllib.parse import urlencode, urljoin
3
+
4
+ from .base import Base
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 GeoboxClient
11
+ from .aio import AsyncGeoboxClient
12
+ from .aio.user import AsyncUser
13
+ from .aio.user import AsyncSession
14
+
15
+
16
+ class User(Base):
17
+
18
+ BASE_ENDPOINT: str = 'users/'
19
+
20
+ def __init__(self, api: 'GeoboxClient', user_id: int, data: dict = {}) -> None:
21
+ """
22
+ Initialize a User instance.
23
+
24
+ Args:
25
+ api (GeoboxClient): 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
+ def get_users(cls, api: 'GeoboxClient', **kwargs) -> Union[List['User'], int]:
80
+ """
81
+ Retrieves a list of users (Permission Required)
82
+
83
+ Args:
84
+ api (GeoboxClient): 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 import Geoboxclient
103
+ >>> from geobox.user import User
104
+ >>> client = GeoboxClient()
105
+ >>> users = User.get_users(client)
106
+ or
107
+ >>> users = 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 super()._get_list(api, cls.BASE_ENDPOINT, params, factory_func=lambda api, item: User(api, item['id'], item))
122
+
123
+
124
+ @classmethod
125
+ def create_user(cls,
126
+ api: 'GeoboxClient',
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
+ Create a User (Permission Required)
137
+
138
+ Args:
139
+ api (GeoboxClient): 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 import GeoboxClient
154
+ >>> from geobox.user import User
155
+ >>> client = GeoboxClient()
156
+ >>> user = 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 = 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 super()._create(api, cls.BASE_ENDPOINT, data, factory_func=lambda api, item: User(api, item['id'], item))
186
+
187
+
188
+ @classmethod
189
+ def search_users(cls, api: 'GeoboxClient', search: str = None, skip: int = 0, limit: int = 10) -> List['User']:
190
+ """
191
+ Get list of users based on the search term.
192
+
193
+ Args:
194
+ api (GeoboxClient): The GeoboxClient 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 import GeoboxClient
204
+ >>> from geobox.user import User
205
+ >>> client = GeoboxClient()
206
+ >>> users = User.get_users(client, search="John")
207
+ or
208
+ >>> users = 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 super()._get_list(api, endpoint, params, factory_func=lambda api, item: User(api, item['id'], item))
217
+
218
+
219
+ @classmethod
220
+ def get_user(cls, api: 'GeoboxClient', user_id: int = 'me') -> 'User':
221
+ """
222
+ Get a user by its id (Permission Required)
223
+
224
+ Args:
225
+ api (GeoboxClient): The GeoboxClient 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 import GeoboxClient
236
+ >>> from geobox.user import User
237
+ >>> client = GeoboxClient()
238
+ >>> user = User.get_user(client, user_id=1)
239
+ or
240
+ >>> user = client.get_user(user_id=1)
241
+
242
+ get the current user
243
+ >>> user = User.get_user(client)
244
+ or
245
+ >>> user = client.get_user()
246
+ """
247
+ params = {
248
+ 'f': 'json'
249
+ }
250
+ return super()._get_detail(api, cls.BASE_ENDPOINT, user_id, params, factory_func=lambda api, item: User(api, item['id'], item))
251
+
252
+
253
+ def update(self, **kwargs) -> Dict:
254
+ """
255
+ 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 imoprt GeoboxClient
273
+ >>> from geobox.user import User
274
+ >>> client = GeoboxClient()
275
+ >>> user = User.get_user(client, user_id=1)
276
+ >>> 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 = self.api.put(self.endpoint, data)
304
+ self._update_properties(response)
305
+ return response
306
+
307
+
308
+ def delete(self) -> None:
309
+ """
310
+ Delete the user (Permission Required)
311
+
312
+ Returns:
313
+ None
314
+
315
+ Example:
316
+ >>> from geobox import GeoboxClient
317
+ >>> from geobox.user import User
318
+ >>> client = GeoboxClient()
319
+ >>> user = User.get_user(client, user_id=1)
320
+ >>> user.delete()
321
+ """
322
+ super()._delete(self.endpoint)
323
+
324
+
325
+ def get_sessions(self, user_id: int = 'me') -> List['Session']:
326
+ """
327
+ 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 import GeoboxClient
337
+ >>> from geobox.user import User
338
+ >>> client = GeoboxClient()
339
+ >>> user = User.get_user(client, user_id=1)
340
+ or
341
+ >>> user = client.get_user(user_id=1)
342
+
343
+ >>> user.get_sessions()
344
+ or
345
+ >>> client.get_sessions()
346
+ """
347
+ params = clean_data({
348
+ 'f': 'json'
349
+ })
350
+ query_string = urlencode(params)
351
+ if user_id != 'me':
352
+ user = 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 = self.api.get(endpoint)
359
+ return [Session(item['uuid'], item, user) for item in response]
360
+
361
+
362
+ def change_password(self, new_password: str) -> None:
363
+ """
364
+ 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 import GeoboxClient
374
+ >>> from geobox.user import User
375
+ >>> client = GeoboxClient()
376
+ >>> user = client.get_user(user_id=1)
377
+ >>> 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
+ self.api.post(endpoint, data, is_json=False)
384
+
385
+
386
+ def renew_plan(self) -> None:
387
+ """
388
+ Renew the user plan (privileges required)
389
+
390
+ Returns:
391
+ None
392
+
393
+ Example:
394
+ >>> from geobox import GeoboxClient
395
+ >>> user = client.get_user(user_id=1)
396
+ >>> user.renew_plan()
397
+ """
398
+ endpoint = urljoin(self.endpoint, 'renewPlan')
399
+ self.api.post(endpoint)
400
+
401
+
402
+ def to_async(self, async_client: 'AsyncGeoboxClient') -> 'AsyncUser':
403
+ """
404
+ Switch to async version of the user instance to have access to the async methods
405
+
406
+ Args:
407
+ async_client (AsyncGeoboxClient): The async version of the GeoboxClient instance for making requests.
408
+
409
+ Returns:
410
+ AsyncUser: the async instance of the user.
411
+
412
+ Example:
413
+ >>> from geobox import Geoboxclient
414
+ >>> from geobox.aio import AsyncGeoboxClient
415
+ >>> from geobox.user import User
416
+ >>> client = GeoboxClient()
417
+ >>> user = User.get_user(client) # without user_id parameter, it gets the current user
418
+ or
419
+ >>> user = client.get_user() # without user_id parameter, it gets the current user
420
+ >>> async with AsyncGeoboxClient() as async_client:
421
+ >>> async_user = user.to_async(async_client)
422
+ """
423
+ from .aio.user import AsyncUser
424
+
425
+ return AsyncUser(api=async_client, user_id=self.user_id, data=self.data)
426
+
427
+
428
+
429
+ class Session(Base):
430
+ def __init__(self, uuid: str, data: Dict, user: 'User'):
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"Session(user={self.user}, agent='{self.agent}')"
453
+
454
+
455
+ def close(self) -> None:
456
+ """
457
+ Close the user session
458
+
459
+ Returns:
460
+ None
461
+
462
+ Example:
463
+ >>> from geobox import geoboxClient
464
+ >>> from geobox.user import User
465
+ >>> client = GeoboxClient()
466
+ >>> user = User.get_user(client) # without user_id parameter, it gets the current user
467
+ or
468
+ >>> user = client.get_user() # without user_id parameter, it gets the current user
469
+ >>> session = user.get_sessions()[0]
470
+ >>> session.close()
471
+ """
472
+ data = clean_data({
473
+ 'user_id': self.user.user_id,
474
+ 'session_uuid': self.uuid
475
+ })
476
+ self.user.api.post(self.endpoint, data)
477
+
478
+
479
+ def to_async(self, async_client: 'AsyncGeoboxClient') -> 'AsyncSession':
480
+ """
481
+ Switch to async version of the session instance to have access to the async methods
482
+
483
+ Args:
484
+ async_client (AsyncGeoboxClient): The async version of the GeoboxClient instance for making requests.
485
+
486
+ Returns:
487
+ AsyncSession: the async instance of the session.
488
+
489
+ Example:
490
+ >>> from geobox import Geoboxclient
491
+ >>> from geobox.aio import AsyncGeoboxClient
492
+ >>> from geobox.user import User
493
+ >>> client = GeoboxClient()
494
+ >>> user = User.get_user(client) # without user_id parameter, it gets the current user
495
+ or
496
+ >>> user = client.get_user() # without user_id parameter, it gets the current user
497
+ >>> session = user.get_sessions()[0]
498
+ >>> async with AsyncGeoboxClient() as async_client:
499
+ >>> async_session = session.to_async(async_client)
500
+ """
501
+ from .aio.user import AsyncSession
502
+
503
+ async_user = self.user.to_async(async_client=async_client)
504
504
  return AsyncSession(uuid=self.uuid, data=self.data, user=async_user)