personal_knowledge_library 3.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.
Potentially problematic release.
This version of personal_knowledge_library might be problematic. Click here for more details.
- knowledge/__init__.py +91 -0
- knowledge/base/__init__.py +22 -0
- knowledge/base/access.py +167 -0
- knowledge/base/entity.py +267 -0
- knowledge/base/language.py +27 -0
- knowledge/base/ontology.py +2734 -0
- knowledge/base/search.py +473 -0
- knowledge/base/tenant.py +192 -0
- knowledge/nel/__init__.py +11 -0
- knowledge/nel/base.py +495 -0
- knowledge/nel/engine.py +123 -0
- knowledge/ontomapping/__init__.py +667 -0
- knowledge/ontomapping/manager.py +320 -0
- knowledge/public/__init__.py +27 -0
- knowledge/public/cache.py +115 -0
- knowledge/public/helper.py +373 -0
- knowledge/public/relations.py +128 -0
- knowledge/public/wikidata.py +1324 -0
- knowledge/services/__init__.py +128 -0
- knowledge/services/asyncio/__init__.py +7 -0
- knowledge/services/asyncio/base.py +458 -0
- knowledge/services/asyncio/graph.py +1420 -0
- knowledge/services/asyncio/group.py +450 -0
- knowledge/services/asyncio/search.py +439 -0
- knowledge/services/asyncio/users.py +270 -0
- knowledge/services/base.py +533 -0
- knowledge/services/graph.py +1897 -0
- knowledge/services/group.py +819 -0
- knowledge/services/helper.py +142 -0
- knowledge/services/ontology.py +1234 -0
- knowledge/services/search.py +488 -0
- knowledge/services/session.py +444 -0
- knowledge/services/tenant.py +281 -0
- knowledge/services/users.py +445 -0
- knowledge/utils/__init__.py +10 -0
- knowledge/utils/graph.py +417 -0
- knowledge/utils/wikidata.py +197 -0
- knowledge/utils/wikipedia.py +175 -0
- personal_knowledge_library-3.0.0.dist-info/LICENSE +201 -0
- personal_knowledge_library-3.0.0.dist-info/METADATA +1163 -0
- personal_knowledge_library-3.0.0.dist-info/RECORD +42 -0
- personal_knowledge_library-3.0.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,819 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Copyright © 2021-present Wacom. All rights reserved.
|
|
3
|
+
import urllib.parse
|
|
4
|
+
from typing import List, Any, Optional, Dict
|
|
5
|
+
|
|
6
|
+
import requests
|
|
7
|
+
from requests import Response
|
|
8
|
+
from requests.adapters import HTTPAdapter
|
|
9
|
+
from urllib3 import Retry
|
|
10
|
+
|
|
11
|
+
from knowledge.base.access import GroupAccessRight
|
|
12
|
+
from knowledge.base.ontology import NAME_TAG
|
|
13
|
+
from knowledge.services import (
|
|
14
|
+
GROUP_USER_RIGHTS_TAG,
|
|
15
|
+
DEFAULT_TIMEOUT,
|
|
16
|
+
JOIN_KEY_PARAM,
|
|
17
|
+
USER_TO_ADD_PARAM,
|
|
18
|
+
USER_TO_REMOVE_PARAM,
|
|
19
|
+
FORCE_PARAM,
|
|
20
|
+
DEFAULT_MAX_RETRIES,
|
|
21
|
+
DEFAULT_BACKOFF_FACTOR,
|
|
22
|
+
USER_AGENT_HEADER_FLAG,
|
|
23
|
+
)
|
|
24
|
+
from knowledge.services.base import WacomServiceAPIClient, handle_error
|
|
25
|
+
from knowledge.services.graph import AUTHORIZATION_HEADER_FLAG
|
|
26
|
+
|
|
27
|
+
# -------------------------------------- Constant flags ----------------------------------------------------------------
|
|
28
|
+
from knowledge.services.users import User, FORCE_TAG, LIMIT_TAG, OFFSET_TAG
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class Group:
|
|
32
|
+
"""
|
|
33
|
+
Group
|
|
34
|
+
-----
|
|
35
|
+
In Personal Knowledge backend users can be logically grouped.
|
|
36
|
+
|
|
37
|
+
Parameters
|
|
38
|
+
----------
|
|
39
|
+
tenant_id: str
|
|
40
|
+
Tenant id
|
|
41
|
+
group_id: str
|
|
42
|
+
Group id
|
|
43
|
+
owner: str
|
|
44
|
+
User id who has created the group.
|
|
45
|
+
name: str
|
|
46
|
+
Name of the group.
|
|
47
|
+
join_key: str
|
|
48
|
+
Key which is required to join the group
|
|
49
|
+
rights: GroupAccessRight
|
|
50
|
+
Access right for group
|
|
51
|
+
|
|
52
|
+
Attributes
|
|
53
|
+
----------
|
|
54
|
+
id: str
|
|
55
|
+
Group identifier
|
|
56
|
+
tenant_id: str
|
|
57
|
+
Tenant identifier
|
|
58
|
+
owner_id: str
|
|
59
|
+
Owner identifier
|
|
60
|
+
name: str
|
|
61
|
+
Name of the group
|
|
62
|
+
join_key: str
|
|
63
|
+
Key which is required to join the group
|
|
64
|
+
group_access_rights: GroupAccessRight
|
|
65
|
+
Access rights for the group
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
def __init__(self, tenant_id: str, group_id: str, owner: str, name: str, join_key: str, rights: GroupAccessRight):
|
|
69
|
+
self.__tenant_id: str = tenant_id
|
|
70
|
+
self.__group_id: str = group_id
|
|
71
|
+
self.__owner_id: str = owner
|
|
72
|
+
self.__name: str = name
|
|
73
|
+
self.__join_key: str = join_key
|
|
74
|
+
self.__rights: GroupAccessRight = rights
|
|
75
|
+
|
|
76
|
+
@property
|
|
77
|
+
def id(self) -> str:
|
|
78
|
+
"""Group id."""
|
|
79
|
+
return self.__group_id
|
|
80
|
+
|
|
81
|
+
@property
|
|
82
|
+
def tenant_id(self) -> str:
|
|
83
|
+
"""Tenant ID."""
|
|
84
|
+
return self.__tenant_id
|
|
85
|
+
|
|
86
|
+
@property
|
|
87
|
+
def owner_id(self) -> Optional[str]:
|
|
88
|
+
"""Owner id (internal id) of the user, who owns the group."""
|
|
89
|
+
return self.__owner_id
|
|
90
|
+
|
|
91
|
+
@property
|
|
92
|
+
def name(self) -> str:
|
|
93
|
+
"""Name of the group."""
|
|
94
|
+
return self.__name
|
|
95
|
+
|
|
96
|
+
@property
|
|
97
|
+
def join_key(self) -> str:
|
|
98
|
+
"""Key for joining the group."""
|
|
99
|
+
return self.__join_key
|
|
100
|
+
|
|
101
|
+
@property
|
|
102
|
+
def group_access_rights(self) -> GroupAccessRight:
|
|
103
|
+
"""Rights for group."""
|
|
104
|
+
return self.__rights
|
|
105
|
+
|
|
106
|
+
@classmethod
|
|
107
|
+
def parse(cls, param: Dict[str, Any]) -> "Group":
|
|
108
|
+
"""Parse group from dictionary.
|
|
109
|
+
|
|
110
|
+
Arguments
|
|
111
|
+
---------
|
|
112
|
+
param: Dict[str, Any]
|
|
113
|
+
Dictionary containing group information.
|
|
114
|
+
|
|
115
|
+
Returns
|
|
116
|
+
-------
|
|
117
|
+
instance: Group
|
|
118
|
+
The group object
|
|
119
|
+
"""
|
|
120
|
+
tenant_id: str = param.get("tenantId")
|
|
121
|
+
owner_id: str = param.get("ownerId")
|
|
122
|
+
join_key: str = param.get("joinKey")
|
|
123
|
+
group_id: str = param.get("id")
|
|
124
|
+
name: str = param.get("name")
|
|
125
|
+
rights: GroupAccessRight = GroupAccessRight.parse(param.get("groupUserRights", ["Read"]))
|
|
126
|
+
return Group(
|
|
127
|
+
tenant_id=tenant_id, group_id=group_id, owner=owner_id, join_key=join_key, name=name, rights=rights
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
def __repr__(self):
|
|
131
|
+
return f"<Group: id:={self.id}, name:={self.name}, group access right:={self.group_access_rights}]>"
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
class GroupInfo(Group):
|
|
135
|
+
"""
|
|
136
|
+
Group Information
|
|
137
|
+
-----------------
|
|
138
|
+
Provides additional information on the group.
|
|
139
|
+
Users within the group are listed.
|
|
140
|
+
|
|
141
|
+
Parameters
|
|
142
|
+
----------
|
|
143
|
+
tenant_id: str
|
|
144
|
+
Tenant id
|
|
145
|
+
group_id: str
|
|
146
|
+
Group id
|
|
147
|
+
owner: str
|
|
148
|
+
User id who has created the group.
|
|
149
|
+
name: str
|
|
150
|
+
Name of the group.
|
|
151
|
+
join_key: str
|
|
152
|
+
Key which is required to join the group
|
|
153
|
+
rights: GroupAccessRight
|
|
154
|
+
Access right for group
|
|
155
|
+
group_users: List[User]
|
|
156
|
+
List of users within the group.
|
|
157
|
+
|
|
158
|
+
Attributes
|
|
159
|
+
----------
|
|
160
|
+
group_users: List[User]
|
|
161
|
+
List of all users that are part of the group.
|
|
162
|
+
|
|
163
|
+
"""
|
|
164
|
+
|
|
165
|
+
def __init__(
|
|
166
|
+
self,
|
|
167
|
+
tenant_id: str,
|
|
168
|
+
group_id: str,
|
|
169
|
+
owner: str,
|
|
170
|
+
name: str,
|
|
171
|
+
join_key: str,
|
|
172
|
+
rights: GroupAccessRight,
|
|
173
|
+
group_users: List[User],
|
|
174
|
+
):
|
|
175
|
+
self.__users: List[User] = group_users
|
|
176
|
+
super().__init__(tenant_id, group_id, owner, name, join_key, rights)
|
|
177
|
+
|
|
178
|
+
@property
|
|
179
|
+
def group_users(self) -> List:
|
|
180
|
+
"""List of all users that are part of the group."""
|
|
181
|
+
return self.__users
|
|
182
|
+
|
|
183
|
+
@classmethod
|
|
184
|
+
def parse(cls, param: Dict[str, Any]) -> "GroupInfo":
|
|
185
|
+
tenant_id: str = param.get("tenantId")
|
|
186
|
+
owner_id: str = param.get("ownerId")
|
|
187
|
+
join_key: str = param.get("joinKey")
|
|
188
|
+
group_id: str = param.get("id")
|
|
189
|
+
name: str = param.get("name")
|
|
190
|
+
rights: GroupAccessRight = GroupAccessRight.parse(param.get("groupUserRights", ["Read"]))
|
|
191
|
+
return GroupInfo(
|
|
192
|
+
tenant_id=tenant_id,
|
|
193
|
+
group_id=group_id,
|
|
194
|
+
owner=owner_id,
|
|
195
|
+
join_key=join_key,
|
|
196
|
+
name=name,
|
|
197
|
+
rights=rights,
|
|
198
|
+
group_users=[User.parse(u) for u in param.get("users", [])],
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
def __repr__(self):
|
|
202
|
+
return (
|
|
203
|
+
f"<GroupInfo: id:={self.id}, name:={self.name}, group access right:={self.group_access_rights}, "
|
|
204
|
+
f"number of users:={len(self.group_users)}]>"
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
class GroupManagementService(WacomServiceAPIClient):
|
|
209
|
+
"""
|
|
210
|
+
Group Management Service API
|
|
211
|
+
-----------------------------
|
|
212
|
+
The service is managing groups.
|
|
213
|
+
|
|
214
|
+
Functionality:
|
|
215
|
+
- List all groups
|
|
216
|
+
- Create group
|
|
217
|
+
- Assign users to group
|
|
218
|
+
- Share entities with group
|
|
219
|
+
|
|
220
|
+
Parameters
|
|
221
|
+
----------
|
|
222
|
+
service_url: str
|
|
223
|
+
URL of the service
|
|
224
|
+
service_endpoint: str
|
|
225
|
+
Base endpoint
|
|
226
|
+
"""
|
|
227
|
+
|
|
228
|
+
GROUP_ENDPOINT: str = "group"
|
|
229
|
+
""""Endpoint for all group related functionality."""
|
|
230
|
+
|
|
231
|
+
def __init__(self, service_url: str = WacomServiceAPIClient.SERVICE_URL, service_endpoint: str = "graph/v1"):
|
|
232
|
+
super().__init__("GroupManagementService", service_url=service_url, service_endpoint=service_endpoint)
|
|
233
|
+
|
|
234
|
+
# ------------------------------------------ Groups handling ------------------------------------------------------
|
|
235
|
+
|
|
236
|
+
def create_group(
|
|
237
|
+
self,
|
|
238
|
+
name: str,
|
|
239
|
+
rights: GroupAccessRight = GroupAccessRight(read=True),
|
|
240
|
+
auth_key: Optional[str] = None,
|
|
241
|
+
timeout: int = DEFAULT_TIMEOUT,
|
|
242
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
243
|
+
backoff_factor: float = DEFAULT_BACKOFF_FACTOR,
|
|
244
|
+
) -> Group:
|
|
245
|
+
"""
|
|
246
|
+
Creates a group.
|
|
247
|
+
|
|
248
|
+
Parameters
|
|
249
|
+
----------
|
|
250
|
+
name: str
|
|
251
|
+
Name of the tenant
|
|
252
|
+
rights: GroupAccessRight
|
|
253
|
+
Access rights
|
|
254
|
+
auth_key: Optional[str]
|
|
255
|
+
If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
|
|
256
|
+
timeout: int
|
|
257
|
+
Timeout for the request (default: 60 seconds)
|
|
258
|
+
max_retries: int
|
|
259
|
+
Maximum number of retries
|
|
260
|
+
backoff_factor: float
|
|
261
|
+
A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a
|
|
262
|
+
second try without a delay)
|
|
263
|
+
Returns
|
|
264
|
+
-------
|
|
265
|
+
group: Group
|
|
266
|
+
Instance of the group.
|
|
267
|
+
|
|
268
|
+
Raises
|
|
269
|
+
------
|
|
270
|
+
WacomServiceException
|
|
271
|
+
If the tenant service returns an error code.
|
|
272
|
+
"""
|
|
273
|
+
if auth_key is None:
|
|
274
|
+
auth_key, _ = self.handle_token()
|
|
275
|
+
url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}"
|
|
276
|
+
headers: Dict[str, str] = {
|
|
277
|
+
AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
|
|
278
|
+
USER_AGENT_HEADER_FLAG: self.user_agent,
|
|
279
|
+
}
|
|
280
|
+
payload: Dict[str, str] = {NAME_TAG: name, GROUP_USER_RIGHTS_TAG: rights.to_list()}
|
|
281
|
+
mount_point: str = "https://" if self.service_url.startswith("https") else "http://"
|
|
282
|
+
with requests.Session() as session:
|
|
283
|
+
retries: Retry = Retry(
|
|
284
|
+
total=max_retries, backoff_factor=backoff_factor, status_forcelist=[500, 502, 503, 504]
|
|
285
|
+
)
|
|
286
|
+
session.mount(mount_point, HTTPAdapter(max_retries=retries))
|
|
287
|
+
response: Response = session.post(
|
|
288
|
+
url, headers=headers, json=payload, verify=self.verify_calls, timeout=timeout
|
|
289
|
+
)
|
|
290
|
+
if response.ok:
|
|
291
|
+
return Group.parse(response.json())
|
|
292
|
+
raise handle_error("Creating of group failed.", response, payload=payload, headers=headers)
|
|
293
|
+
|
|
294
|
+
def update_group(
|
|
295
|
+
self,
|
|
296
|
+
group_id: str,
|
|
297
|
+
name: str,
|
|
298
|
+
rights: GroupAccessRight = GroupAccessRight,
|
|
299
|
+
auth_key: Optional[str] = None,
|
|
300
|
+
timeout: int = DEFAULT_TIMEOUT,
|
|
301
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
302
|
+
backoff_factor: float = DEFAULT_BACKOFF_FACTOR,
|
|
303
|
+
):
|
|
304
|
+
"""
|
|
305
|
+
Updates a group.
|
|
306
|
+
|
|
307
|
+
Parameters
|
|
308
|
+
----------
|
|
309
|
+
group_id: str
|
|
310
|
+
ID of the group.
|
|
311
|
+
name: str
|
|
312
|
+
Name of the tenant
|
|
313
|
+
rights: GroupAccessRight
|
|
314
|
+
Access rights
|
|
315
|
+
auth_key: Optional[str]
|
|
316
|
+
If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
|
|
317
|
+
timeout: int
|
|
318
|
+
Timeout for the request (default: 60 seconds)
|
|
319
|
+
max_retries: int
|
|
320
|
+
Maximum number of retries
|
|
321
|
+
backoff_factor: float
|
|
322
|
+
A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a
|
|
323
|
+
second try without a delay)
|
|
324
|
+
|
|
325
|
+
Raises
|
|
326
|
+
------
|
|
327
|
+
WacomServiceException
|
|
328
|
+
If the tenant service returns an error code.
|
|
329
|
+
"""
|
|
330
|
+
if auth_key is None:
|
|
331
|
+
auth_key, _ = self.handle_token()
|
|
332
|
+
url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}/{group_id}"
|
|
333
|
+
headers: Dict[str, str] = {
|
|
334
|
+
AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
|
|
335
|
+
USER_AGENT_HEADER_FLAG: self.user_agent,
|
|
336
|
+
}
|
|
337
|
+
payload: Dict[str, str] = {NAME_TAG: name, GROUP_USER_RIGHTS_TAG: rights.to_list()}
|
|
338
|
+
mount_point: str = "https://" if self.service_url.startswith("https") else "http://"
|
|
339
|
+
with requests.Session() as session:
|
|
340
|
+
retries: Retry = Retry(
|
|
341
|
+
total=max_retries, backoff_factor=backoff_factor, status_forcelist=[500, 502, 503, 504]
|
|
342
|
+
)
|
|
343
|
+
session.mount(mount_point, HTTPAdapter(max_retries=retries))
|
|
344
|
+
response: Response = session.patch(
|
|
345
|
+
url, headers=headers, json=payload, verify=self.verify_calls, timeout=timeout
|
|
346
|
+
)
|
|
347
|
+
if not response.ok:
|
|
348
|
+
raise handle_error("Update of group failed.", response, payload=payload, headers=headers)
|
|
349
|
+
|
|
350
|
+
def delete_group(
|
|
351
|
+
self,
|
|
352
|
+
group_id: str,
|
|
353
|
+
force: bool = False,
|
|
354
|
+
auth_key: Optional[str] = None,
|
|
355
|
+
timeout: int = DEFAULT_TIMEOUT,
|
|
356
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
357
|
+
backoff_factor: float = DEFAULT_BACKOFF_FACTOR,
|
|
358
|
+
):
|
|
359
|
+
"""
|
|
360
|
+
Delete a group.
|
|
361
|
+
|
|
362
|
+
Parameters
|
|
363
|
+
----------
|
|
364
|
+
group_id: str
|
|
365
|
+
ID of the group.
|
|
366
|
+
force: bool (Default = False)
|
|
367
|
+
If True, the group will be deleted even if it is not empty.
|
|
368
|
+
auth_key: Optional[str]
|
|
369
|
+
If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
|
|
370
|
+
timeout: int
|
|
371
|
+
Timeout for the request (default: 60 seconds)
|
|
372
|
+
max_retries: int
|
|
373
|
+
Maximum number of retries
|
|
374
|
+
backoff_factor: float
|
|
375
|
+
A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a
|
|
376
|
+
second try without a delay)
|
|
377
|
+
|
|
378
|
+
Raises
|
|
379
|
+
------
|
|
380
|
+
WacomServiceException
|
|
381
|
+
If the tenant service returns an error code.
|
|
382
|
+
"""
|
|
383
|
+
if auth_key is None:
|
|
384
|
+
auth_key, _ = self.handle_token()
|
|
385
|
+
url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}/{group_id}"
|
|
386
|
+
headers: Dict[str, str] = {
|
|
387
|
+
AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
|
|
388
|
+
USER_AGENT_HEADER_FLAG: self.user_agent,
|
|
389
|
+
}
|
|
390
|
+
params: Dict[str, str] = {FORCE_TAG: str(force).lower()}
|
|
391
|
+
mount_point: str = "https://" if self.service_url.startswith("https") else "http://"
|
|
392
|
+
with requests.Session() as session:
|
|
393
|
+
retries: Retry = Retry(
|
|
394
|
+
total=max_retries, backoff_factor=backoff_factor, status_forcelist=[500, 502, 503, 504]
|
|
395
|
+
)
|
|
396
|
+
session.mount(mount_point, HTTPAdapter(max_retries=retries))
|
|
397
|
+
response: Response = session.delete(
|
|
398
|
+
url, headers=headers, params=params, verify=self.verify_calls, timeout=timeout
|
|
399
|
+
)
|
|
400
|
+
if not response.ok:
|
|
401
|
+
raise handle_error("Deletion of group failed.", response, parameters=params, headers=headers)
|
|
402
|
+
|
|
403
|
+
def listing_groups(
|
|
404
|
+
self,
|
|
405
|
+
admin: bool = False,
|
|
406
|
+
limit: int = 20,
|
|
407
|
+
offset: int = 0,
|
|
408
|
+
auth_key: Optional[str] = None,
|
|
409
|
+
timeout: int = DEFAULT_TIMEOUT,
|
|
410
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
411
|
+
backoff_factor: float = DEFAULT_BACKOFF_FACTOR,
|
|
412
|
+
) -> List[Group]:
|
|
413
|
+
"""
|
|
414
|
+
Listing all groups configured for this instance.
|
|
415
|
+
|
|
416
|
+
Parameters
|
|
417
|
+
----------
|
|
418
|
+
admin: bool (default:= False)
|
|
419
|
+
Uses admin privilege to show all groups of the tenant.
|
|
420
|
+
Requires user to have the role: TenantAdmin
|
|
421
|
+
limit: int (default:= 20)
|
|
422
|
+
Maximum number of groups to return.
|
|
423
|
+
offset: int (default:= 0)
|
|
424
|
+
Offset of the first group to return.
|
|
425
|
+
auth_key: Optional[str]
|
|
426
|
+
If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
|
|
427
|
+
timeout: int
|
|
428
|
+
Timeout for the request (default: 60 seconds)
|
|
429
|
+
max_retries: int
|
|
430
|
+
Maximum number of retries
|
|
431
|
+
backoff_factor: float
|
|
432
|
+
A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a
|
|
433
|
+
second try without a delay)
|
|
434
|
+
Returns
|
|
435
|
+
-------
|
|
436
|
+
user: List[Groups]
|
|
437
|
+
List of groups.
|
|
438
|
+
"""
|
|
439
|
+
if auth_key is None:
|
|
440
|
+
auth_key, _ = self.handle_token()
|
|
441
|
+
url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}"
|
|
442
|
+
params: Dict[str, int] = {}
|
|
443
|
+
if admin:
|
|
444
|
+
url += "/admin"
|
|
445
|
+
params[LIMIT_TAG] = limit
|
|
446
|
+
params[OFFSET_TAG] = offset
|
|
447
|
+
headers: Dict[str, str] = {
|
|
448
|
+
AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
|
|
449
|
+
USER_AGENT_HEADER_FLAG: self.user_agent,
|
|
450
|
+
}
|
|
451
|
+
mount_point: str = "https://" if self.service_url.startswith("https") else "http://"
|
|
452
|
+
with requests.Session() as session:
|
|
453
|
+
retries: Retry = Retry(
|
|
454
|
+
total=max_retries, backoff_factor=backoff_factor, status_forcelist=[500, 502, 503, 504]
|
|
455
|
+
)
|
|
456
|
+
session.mount(mount_point, HTTPAdapter(max_retries=retries))
|
|
457
|
+
response: Response = session.get(
|
|
458
|
+
url, headers=headers, params=params, verify=self.verify_calls, timeout=timeout
|
|
459
|
+
)
|
|
460
|
+
if response.ok:
|
|
461
|
+
groups: List[Dict[str, Any]] = response.json()
|
|
462
|
+
return [Group.parse(g) for g in groups]
|
|
463
|
+
raise handle_error("Listing of groups failed.", response, parameters=params, headers=headers)
|
|
464
|
+
|
|
465
|
+
def group(
|
|
466
|
+
self,
|
|
467
|
+
group_id: str,
|
|
468
|
+
auth_key: Optional[str] = None,
|
|
469
|
+
timeout: int = DEFAULT_TIMEOUT,
|
|
470
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
471
|
+
backoff_factor: float = DEFAULT_BACKOFF_FACTOR,
|
|
472
|
+
) -> GroupInfo:
|
|
473
|
+
"""Get a group.
|
|
474
|
+
|
|
475
|
+
Parameters
|
|
476
|
+
----------
|
|
477
|
+
group_id: str
|
|
478
|
+
Group ID
|
|
479
|
+
auth_key: Optional[str]
|
|
480
|
+
If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
|
|
481
|
+
timeout: int
|
|
482
|
+
Timeout for the request (default: 60 seconds)
|
|
483
|
+
max_retries: int
|
|
484
|
+
Maximum number of retries
|
|
485
|
+
backoff_factor: float
|
|
486
|
+
A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a
|
|
487
|
+
second try without a delay)
|
|
488
|
+
Returns
|
|
489
|
+
-------
|
|
490
|
+
group: GroupInfo
|
|
491
|
+
Instance of the group
|
|
492
|
+
|
|
493
|
+
Raises
|
|
494
|
+
------
|
|
495
|
+
WacomServiceException
|
|
496
|
+
If the tenant service returns an error code.
|
|
497
|
+
"""
|
|
498
|
+
if auth_key is None:
|
|
499
|
+
auth_key, _ = self.handle_token()
|
|
500
|
+
url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}/{group_id}"
|
|
501
|
+
headers: Dict[str, str] = {
|
|
502
|
+
AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
|
|
503
|
+
USER_AGENT_HEADER_FLAG: self.user_agent,
|
|
504
|
+
}
|
|
505
|
+
mount_point: str = "https://" if self.service_url.startswith("https") else "http://"
|
|
506
|
+
with requests.Session() as session:
|
|
507
|
+
retries: Retry = Retry(
|
|
508
|
+
total=max_retries, backoff_factor=backoff_factor, status_forcelist=[500, 502, 503, 504]
|
|
509
|
+
)
|
|
510
|
+
session.mount(mount_point, HTTPAdapter(max_retries=retries))
|
|
511
|
+
|
|
512
|
+
response: Response = session.get(url, headers=headers, verify=self.verify_calls, timeout=timeout)
|
|
513
|
+
if response.ok:
|
|
514
|
+
group: Dict[str, Any] = response.json()
|
|
515
|
+
return GroupInfo.parse(group)
|
|
516
|
+
raise handle_error("Getting of group information failed.", response, headers=headers)
|
|
517
|
+
|
|
518
|
+
def join_group(
|
|
519
|
+
self,
|
|
520
|
+
group_id: str,
|
|
521
|
+
join_key: str,
|
|
522
|
+
auth_key: Optional[str] = None,
|
|
523
|
+
timeout: int = DEFAULT_TIMEOUT,
|
|
524
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
525
|
+
backoff_factor: float = DEFAULT_BACKOFF_FACTOR,
|
|
526
|
+
):
|
|
527
|
+
"""User joining a group with his auth token.
|
|
528
|
+
|
|
529
|
+
Parameters
|
|
530
|
+
----------
|
|
531
|
+
group_id: str
|
|
532
|
+
Group ID
|
|
533
|
+
join_key: str
|
|
534
|
+
Key which is used to join the group.
|
|
535
|
+
auth_key: Optional[str]
|
|
536
|
+
If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
|
|
537
|
+
timeout: int
|
|
538
|
+
Timeout for the request (default: 60 seconds)
|
|
539
|
+
max_retries: int
|
|
540
|
+
Maximum number of retries
|
|
541
|
+
backoff_factor: float
|
|
542
|
+
A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a
|
|
543
|
+
second try without a delay)
|
|
544
|
+
Raises
|
|
545
|
+
------
|
|
546
|
+
WacomServiceException
|
|
547
|
+
If the tenant service returns an error code.
|
|
548
|
+
"""
|
|
549
|
+
if auth_key is None:
|
|
550
|
+
auth_key, _ = self.handle_token()
|
|
551
|
+
url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}/{group_id}/join"
|
|
552
|
+
headers: Dict[str, str] = {
|
|
553
|
+
AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
|
|
554
|
+
USER_AGENT_HEADER_FLAG: self.user_agent,
|
|
555
|
+
}
|
|
556
|
+
params: Dict[str, str] = {
|
|
557
|
+
JOIN_KEY_PARAM: join_key,
|
|
558
|
+
}
|
|
559
|
+
mount_point: str = "https://" if self.service_url.startswith("https") else "http://"
|
|
560
|
+
with requests.Session() as session:
|
|
561
|
+
retries: Retry = Retry(
|
|
562
|
+
total=max_retries, backoff_factor=backoff_factor, status_forcelist=[500, 502, 503, 504]
|
|
563
|
+
)
|
|
564
|
+
session.mount(mount_point, HTTPAdapter(max_retries=retries))
|
|
565
|
+
response: Response = session.post(
|
|
566
|
+
url, headers=headers, params=params, verify=self.verify_calls, timeout=timeout
|
|
567
|
+
)
|
|
568
|
+
if not response.ok:
|
|
569
|
+
raise handle_error("Joining of group failed.", response, parameters=params, headers=headers)
|
|
570
|
+
|
|
571
|
+
def leave_group(
|
|
572
|
+
self,
|
|
573
|
+
group_id: str,
|
|
574
|
+
auth_key: Optional[str] = None,
|
|
575
|
+
timeout: int = DEFAULT_TIMEOUT,
|
|
576
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
577
|
+
backoff_factor: float = DEFAULT_BACKOFF_FACTOR,
|
|
578
|
+
):
|
|
579
|
+
"""User leaving a group with his auth token.
|
|
580
|
+
|
|
581
|
+
Parameters
|
|
582
|
+
----------
|
|
583
|
+
group_id: str
|
|
584
|
+
Group ID
|
|
585
|
+
auth_key: Optional[str]
|
|
586
|
+
If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
|
|
587
|
+
timeout: int
|
|
588
|
+
Timeout for the request (default: 60 seconds)
|
|
589
|
+
max_retries: int
|
|
590
|
+
Maximum number of retries
|
|
591
|
+
backoff_factor: float
|
|
592
|
+
A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a
|
|
593
|
+
second try without a delay)
|
|
594
|
+
Raises
|
|
595
|
+
------
|
|
596
|
+
WacomServiceException
|
|
597
|
+
If the tenant service returns an error code.
|
|
598
|
+
"""
|
|
599
|
+
if auth_key is None:
|
|
600
|
+
auth_key, _ = self.handle_token()
|
|
601
|
+
url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}/{group_id}/leave"
|
|
602
|
+
headers: Dict[str, str] = {
|
|
603
|
+
AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
|
|
604
|
+
USER_AGENT_HEADER_FLAG: self.user_agent,
|
|
605
|
+
}
|
|
606
|
+
mount_point: str = "https://" if self.service_url.startswith("https") else "http://"
|
|
607
|
+
with requests.Session() as session:
|
|
608
|
+
retries: Retry = Retry(
|
|
609
|
+
total=max_retries, backoff_factor=backoff_factor, status_forcelist=[500, 502, 503, 504]
|
|
610
|
+
)
|
|
611
|
+
session.mount(mount_point, HTTPAdapter(max_retries=retries))
|
|
612
|
+
response: Response = session.post(url, headers=headers, verify=self.verify_calls, timeout=timeout)
|
|
613
|
+
if not response.ok:
|
|
614
|
+
raise handle_error("Leaving of group failed.", response, headers=headers)
|
|
615
|
+
|
|
616
|
+
def add_user_to_group(
|
|
617
|
+
self,
|
|
618
|
+
group_id: str,
|
|
619
|
+
user_id: str,
|
|
620
|
+
auth_key: Optional[str] = None,
|
|
621
|
+
timeout: int = DEFAULT_TIMEOUT,
|
|
622
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
623
|
+
backoff_factor: float = DEFAULT_BACKOFF_FACTOR,
|
|
624
|
+
):
|
|
625
|
+
"""Adding a user to group.
|
|
626
|
+
|
|
627
|
+
Parameters
|
|
628
|
+
----------
|
|
629
|
+
group_id: str
|
|
630
|
+
Group ID
|
|
631
|
+
user_id: str
|
|
632
|
+
User who is added to the group
|
|
633
|
+
auth_key: Optional[str]
|
|
634
|
+
If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
|
|
635
|
+
timeout: int
|
|
636
|
+
Timeout for the request (default: 60 seconds)
|
|
637
|
+
max_retries: int
|
|
638
|
+
Maximum number of retries
|
|
639
|
+
backoff_factor: float
|
|
640
|
+
A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a
|
|
641
|
+
second try without a delay)
|
|
642
|
+
Raises
|
|
643
|
+
------
|
|
644
|
+
WacomServiceException
|
|
645
|
+
If the tenant service returns an error code.
|
|
646
|
+
"""
|
|
647
|
+
if auth_key is None:
|
|
648
|
+
auth_key, _ = self.handle_token()
|
|
649
|
+
url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}/{group_id}/user/add"
|
|
650
|
+
headers: Dict[str, str] = {
|
|
651
|
+
AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
|
|
652
|
+
USER_AGENT_HEADER_FLAG: self.user_agent,
|
|
653
|
+
}
|
|
654
|
+
params: Dict[str, str] = {
|
|
655
|
+
USER_TO_ADD_PARAM: user_id,
|
|
656
|
+
}
|
|
657
|
+
mount_point: str = "https://" if self.service_url.startswith("https") else "http://"
|
|
658
|
+
with requests.Session() as session:
|
|
659
|
+
retries: Retry = Retry(
|
|
660
|
+
total=max_retries, backoff_factor=backoff_factor, status_forcelist=[500, 502, 503, 504]
|
|
661
|
+
)
|
|
662
|
+
session.mount(mount_point, HTTPAdapter(max_retries=retries))
|
|
663
|
+
response: Response = requests.post(
|
|
664
|
+
url, headers=headers, params=params, verify=self.verify_calls, timeout=timeout
|
|
665
|
+
)
|
|
666
|
+
if not response.ok:
|
|
667
|
+
raise handle_error("Adding of user to group failed.", response, parameters=params, headers=headers)
|
|
668
|
+
|
|
669
|
+
def remove_user_from_group(
|
|
670
|
+
self,
|
|
671
|
+
group_id: str,
|
|
672
|
+
user_id: str,
|
|
673
|
+
force: bool = False,
|
|
674
|
+
auth_key: Optional[str] = None,
|
|
675
|
+
timeout: int = DEFAULT_TIMEOUT,
|
|
676
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
677
|
+
backoff_factor: float = DEFAULT_BACKOFF_FACTOR,
|
|
678
|
+
):
|
|
679
|
+
"""Remove a user from group.
|
|
680
|
+
|
|
681
|
+
Parameters
|
|
682
|
+
----------
|
|
683
|
+
group_id: str
|
|
684
|
+
Group ID
|
|
685
|
+
user_id: str
|
|
686
|
+
User who is remove from the group
|
|
687
|
+
force: bool
|
|
688
|
+
If true remove user and entities owned by the user if any
|
|
689
|
+
auth_key: Optional[str]
|
|
690
|
+
If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
|
|
691
|
+
timeout: int
|
|
692
|
+
Timeout for the request (default: 60 seconds)
|
|
693
|
+
max_retries: int
|
|
694
|
+
Maximum number of retries
|
|
695
|
+
backoff_factor: float
|
|
696
|
+
A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a
|
|
697
|
+
second try without a delay)
|
|
698
|
+
Raises
|
|
699
|
+
------
|
|
700
|
+
WacomServiceException
|
|
701
|
+
If the tenant service returns an error code.
|
|
702
|
+
"""
|
|
703
|
+
if auth_key is None:
|
|
704
|
+
auth_key, _ = self.handle_token()
|
|
705
|
+
url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}/{group_id}/user/remove"
|
|
706
|
+
headers: Dict[str, str] = {
|
|
707
|
+
AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
|
|
708
|
+
USER_AGENT_HEADER_FLAG: self.user_agent,
|
|
709
|
+
}
|
|
710
|
+
params: Dict[str, str] = {USER_TO_REMOVE_PARAM: user_id, FORCE_PARAM: force}
|
|
711
|
+
mount_point: str = "https://" if self.service_url.startswith("https") else "http://"
|
|
712
|
+
with requests.Session() as session:
|
|
713
|
+
retries: Retry = Retry(
|
|
714
|
+
total=max_retries, backoff_factor=backoff_factor, status_forcelist=[500, 502, 503, 504]
|
|
715
|
+
)
|
|
716
|
+
session.mount(mount_point, HTTPAdapter(max_retries=retries))
|
|
717
|
+
response: Response = session.post(
|
|
718
|
+
url, headers=headers, params=params, verify=self.verify_calls, timeout=timeout
|
|
719
|
+
)
|
|
720
|
+
if not response.ok:
|
|
721
|
+
raise handle_error("Removing of user from group failed.", response, parameters=params, headers=headers)
|
|
722
|
+
|
|
723
|
+
def add_entity_to_group(
|
|
724
|
+
self,
|
|
725
|
+
group_id: str,
|
|
726
|
+
entity_uri: str,
|
|
727
|
+
auth_key: Optional[str] = None,
|
|
728
|
+
timeout: int = DEFAULT_TIMEOUT,
|
|
729
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
730
|
+
backoff_factor: float = DEFAULT_BACKOFF_FACTOR,
|
|
731
|
+
):
|
|
732
|
+
"""Adding an entity to group.
|
|
733
|
+
|
|
734
|
+
Parameters
|
|
735
|
+
----------
|
|
736
|
+
group_id: str
|
|
737
|
+
Group ID
|
|
738
|
+
entity_uri: str
|
|
739
|
+
Entity URI
|
|
740
|
+
auth_key: Optional[str]
|
|
741
|
+
If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
|
|
742
|
+
timeout: int
|
|
743
|
+
Timeout for the request (default: 60 seconds)
|
|
744
|
+
max_retries: int
|
|
745
|
+
Maximum number of retries
|
|
746
|
+
backoff_factor: float
|
|
747
|
+
A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a
|
|
748
|
+
second try without a delay)
|
|
749
|
+
Raises
|
|
750
|
+
------
|
|
751
|
+
WacomServiceException
|
|
752
|
+
If the tenant service returns an error code.
|
|
753
|
+
"""
|
|
754
|
+
if auth_key is None:
|
|
755
|
+
auth_key, _ = self.handle_token()
|
|
756
|
+
uri: str = urllib.parse.quote(entity_uri)
|
|
757
|
+
url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}/{group_id}/entity/{uri}/add"
|
|
758
|
+
headers: Dict[str, str] = {
|
|
759
|
+
AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
|
|
760
|
+
USER_AGENT_HEADER_FLAG: self.user_agent,
|
|
761
|
+
}
|
|
762
|
+
mount_point: str = "https://" if self.service_url.startswith("https") else "http://"
|
|
763
|
+
with requests.Session() as session:
|
|
764
|
+
retries: Retry = Retry(
|
|
765
|
+
total=max_retries, backoff_factor=backoff_factor, status_forcelist=[500, 502, 503, 504]
|
|
766
|
+
)
|
|
767
|
+
session.mount(mount_point, HTTPAdapter(max_retries=retries))
|
|
768
|
+
response: Response = session.post(url, headers=headers, verify=self.verify_calls, timeout=timeout)
|
|
769
|
+
if not response.ok:
|
|
770
|
+
raise handle_error("Adding of entity to group failed.", response, headers=headers)
|
|
771
|
+
|
|
772
|
+
def remove_entity_to_group(
|
|
773
|
+
self,
|
|
774
|
+
group_id: str,
|
|
775
|
+
entity_uri: str,
|
|
776
|
+
auth_key: Optional[str] = None,
|
|
777
|
+
timeout: int = DEFAULT_TIMEOUT,
|
|
778
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
779
|
+
backoff_factor: float = DEFAULT_BACKOFF_FACTOR,
|
|
780
|
+
):
|
|
781
|
+
"""Remove an entity from group.
|
|
782
|
+
|
|
783
|
+
Parameters
|
|
784
|
+
----------
|
|
785
|
+
group_id: str
|
|
786
|
+
Group ID
|
|
787
|
+
entity_uri: str
|
|
788
|
+
URI of entity
|
|
789
|
+
auth_key: Optional[str]
|
|
790
|
+
If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
|
|
791
|
+
timeout: int
|
|
792
|
+
Timeout for the request (default: 60 seconds)
|
|
793
|
+
max_retries: int
|
|
794
|
+
Maximum number of retries
|
|
795
|
+
backoff_factor: float
|
|
796
|
+
A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a
|
|
797
|
+
second try without a delay)
|
|
798
|
+
Raises
|
|
799
|
+
------
|
|
800
|
+
WacomServiceException
|
|
801
|
+
If the tenant service returns an error code.
|
|
802
|
+
"""
|
|
803
|
+
if auth_key is None:
|
|
804
|
+
auth_key, _ = self.handle_token()
|
|
805
|
+
uri: str = urllib.parse.quote(entity_uri)
|
|
806
|
+
url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}/{group_id}/entity/{uri}/remove"
|
|
807
|
+
headers: Dict[str, str] = {
|
|
808
|
+
AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
|
|
809
|
+
USER_AGENT_HEADER_FLAG: self.user_agent,
|
|
810
|
+
}
|
|
811
|
+
mount_point: str = "https://" if self.service_url.startswith("https") else "http://"
|
|
812
|
+
with requests.Session() as session:
|
|
813
|
+
retries: Retry = Retry(
|
|
814
|
+
total=max_retries, backoff_factor=backoff_factor, status_forcelist=[500, 502, 503, 504]
|
|
815
|
+
)
|
|
816
|
+
session.mount(mount_point, HTTPAdapter(max_retries=retries))
|
|
817
|
+
response: Response = session.post(url, headers=headers, verify=self.verify_calls, timeout=timeout)
|
|
818
|
+
if not response.ok:
|
|
819
|
+
raise handle_error("Removing of entity from group failed.", response, headers=headers)
|