geobox 1.4.1__py3-none-any.whl → 2.0.0__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 (66) hide show
  1. geobox/__init__.py +2 -2
  2. geobox/aio/__init__.py +63 -0
  3. geobox/aio/api.py +2640 -0
  4. geobox/aio/apikey.py +263 -0
  5. geobox/aio/attachment.py +339 -0
  6. geobox/aio/base.py +262 -0
  7. geobox/aio/basemap.py +196 -0
  8. geobox/aio/dashboard.py +342 -0
  9. geobox/aio/feature.py +527 -0
  10. geobox/aio/field.py +321 -0
  11. geobox/aio/file.py +522 -0
  12. geobox/aio/layout.py +341 -0
  13. geobox/aio/log.py +145 -0
  14. geobox/aio/map.py +1034 -0
  15. geobox/aio/model3d.py +415 -0
  16. geobox/aio/mosaic.py +696 -0
  17. geobox/aio/plan.py +315 -0
  18. geobox/aio/query.py +702 -0
  19. geobox/aio/raster.py +869 -0
  20. geobox/aio/route.py +63 -0
  21. geobox/aio/scene.py +342 -0
  22. geobox/aio/settings.py +194 -0
  23. geobox/aio/task.py +402 -0
  24. geobox/aio/tile3d.py +339 -0
  25. geobox/aio/tileset.py +672 -0
  26. geobox/aio/usage.py +243 -0
  27. geobox/aio/user.py +507 -0
  28. geobox/aio/vectorlayer.py +1363 -0
  29. geobox/aio/version.py +273 -0
  30. geobox/aio/view.py +983 -0
  31. geobox/aio/workflow.py +341 -0
  32. geobox/api.py +14 -13
  33. geobox/apikey.py +28 -1
  34. geobox/attachment.py +27 -1
  35. geobox/base.py +4 -4
  36. geobox/basemap.py +30 -1
  37. geobox/dashboard.py +27 -0
  38. geobox/feature.py +33 -13
  39. geobox/field.py +33 -21
  40. geobox/file.py +40 -46
  41. geobox/layout.py +28 -1
  42. geobox/log.py +31 -7
  43. geobox/map.py +56 -5
  44. geobox/model3d.py +98 -19
  45. geobox/mosaic.py +47 -7
  46. geobox/plan.py +29 -3
  47. geobox/query.py +41 -5
  48. geobox/raster.py +45 -13
  49. geobox/scene.py +26 -0
  50. geobox/settings.py +30 -1
  51. geobox/task.py +28 -6
  52. geobox/tile3d.py +27 -1
  53. geobox/tileset.py +26 -5
  54. geobox/usage.py +32 -1
  55. geobox/user.py +62 -6
  56. geobox/utils.py +34 -0
  57. geobox/vectorlayer.py +59 -4
  58. geobox/version.py +25 -1
  59. geobox/view.py +54 -15
  60. geobox/workflow.py +27 -1
  61. {geobox-1.4.1.dist-info → geobox-2.0.0.dist-info}/METADATA +4 -1
  62. geobox-2.0.0.dist-info/RECORD +68 -0
  63. geobox-1.4.1.dist-info/RECORD +0 -38
  64. {geobox-1.4.1.dist-info → geobox-2.0.0.dist-info}/WHEEL +0 -0
  65. {geobox-1.4.1.dist-info → geobox-2.0.0.dist-info}/licenses/LICENSE +0 -0
  66. {geobox-1.4.1.dist-info → geobox-2.0.0.dist-info}/top_level.txt +0 -0
geobox/aio/user.py ADDED
@@ -0,0 +1,507 @@
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)