brynq-sdk-brynq 4.2.6.dev0__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 brynq-sdk-brynq might be problematic. Click here for more details.

@@ -0,0 +1,272 @@
1
+ from typing import Dict, List, Any, Optional
2
+ import requests
3
+ from .schemas.roles import RoleSchema, CreateRoleRequest, RoleUser, DashboardRight, QlikDashboardRight
4
+ from brynq_sdk_functions import Functions
5
+
6
+ class Roles:
7
+ """
8
+ Handles all role-related operations for BrynQ SDK.
9
+ """
10
+ def __init__(self, brynq_instance):
11
+ """
12
+ Initialize Roles manager.
13
+
14
+ Args:
15
+ brynq_instance: The parent BrynQ instance
16
+ """
17
+ self._brynq = brynq_instance
18
+
19
+ def get(self) -> List[Dict[str, Any]]:
20
+ """
21
+ Get all roles from BrynQ
22
+
23
+ :return: A list of validated role dictionaries
24
+ :raises:
25
+ requests.HTTPError: If the API request fails
26
+ ValueError: If the role data is invalid
27
+ """
28
+ response = self._brynq.brynq_session.get(
29
+ url=f'{self._brynq.url}roles',
30
+ timeout=self._brynq.timeout
31
+ )
32
+ response.raise_for_status()
33
+
34
+ # Get the raw data
35
+ roles_data = response.json()
36
+
37
+ # Validate each role
38
+ try:
39
+ valid_data, _ = Functions.validate_pydantic_data(roles_data, schema=RoleSchema)
40
+ return valid_data # Return first item since it's a list
41
+ except ValueError as e:
42
+ raise ValueError(f"Invalid role data received from API: {str(e)}")
43
+
44
+ def get_by_id(self, role_id: int) -> Dict[str, Any]:
45
+ """Get a specific role by ID.
46
+
47
+ Args:
48
+ role_id (int): ID of the role to retrieve.
49
+
50
+ Returns:
51
+ Dict[str, Any]: Role data including id, name, permissions, dashboards, and qlik_dashboards.
52
+
53
+ Raises:
54
+ ValueError: If the response data is invalid.
55
+ requests.exceptions.RequestException: If the API request fails.
56
+ """
57
+ response = self._brynq.brynq_session.get(
58
+ f"{self._brynq.url}roles/{role_id}",
59
+ timeout=self._brynq.timeout
60
+ )
61
+ response.raise_for_status()
62
+
63
+ try:
64
+ role_data = response.json()
65
+ valid_data, _ = Functions.validate_pydantic_data(role_data, schema=RoleSchema)
66
+ return valid_data[0]
67
+ except ValueError as e:
68
+ raise ValueError(f"Invalid role data received from API: {str(e)}")
69
+
70
+ def create(self, data: Dict[str, Any]) -> None:
71
+ """Create a new role.
72
+
73
+ Args:
74
+ data: Dictionary containing role data:
75
+ {
76
+ "name": str, # Name of the role
77
+ "dashboards": List[Dict], # Optional list of dashboard rights
78
+ "qlikDashboards": List[Dict] # Optional list of Qlik dashboard rights
79
+ }
80
+
81
+ Raises:
82
+ ValueError: If the request data is invalid.
83
+ requests.exceptions.RequestException: If the API request fails.
84
+ """
85
+ try:
86
+ valid_data, _ = Functions.validate_pydantic_data(data, schema=CreateRoleRequest)
87
+ if valid_data:
88
+ response = self._brynq.brynq_session.post(
89
+ f"{self._brynq.url}roles",
90
+ json=valid_data[0],
91
+ timeout=self._brynq.timeout
92
+ )
93
+ response.raise_for_status()
94
+ return response
95
+ else:
96
+ raise ValueError(f"Invalid role creation data")
97
+ except ValueError as e:
98
+ raise ValueError(f"Invalid role creation data: {str(e)}")
99
+
100
+ def update(self, data: Dict[str, Any]) -> requests.Response:
101
+ """Update an existing role.
102
+
103
+ Args:
104
+ data: Dictionary containing role data:
105
+ {
106
+ "id": int, # ID of the role to update
107
+ "name": str, # New name for the role
108
+ "dashboards": List[Dict], # Optional list of dashboard rights
109
+ "qlikDashboards": List[Dict] # Optional list of Qlik dashboard rights
110
+ }
111
+
112
+ Raises:
113
+ ValueError: If the request data is invalid.
114
+ requests.exceptions.RequestException: If the API request fails.
115
+ """
116
+ try:
117
+ valid_data, _ = Functions.validate_pydantic_data(data, schema=RoleSchema)
118
+ if valid_data:
119
+ response = self._brynq.brynq_session.put(
120
+ f"{self._brynq.url}roles/{data['id']}",
121
+ json=valid_data[0],
122
+ timeout=self._brynq.timeout
123
+ )
124
+ response.raise_for_status()
125
+ return response
126
+ else:
127
+ raise ValueError(f"Invalid role update data")
128
+ except ValueError as e:
129
+ raise ValueError(f"Invalid role update data: {str(e)}")
130
+
131
+ def delete(self, role_id: int, force: bool = False) -> None:
132
+ """Delete a role by ID.
133
+
134
+ Args:
135
+ role_id (int): ID of the role to delete
136
+ force (bool, optional): Whether to force delete even if role is in use. Defaults to False.
137
+
138
+ Raises:
139
+ ValueError: If role_id is not a positive integer.
140
+ requests.exceptions.RequestException: If the API request fails.
141
+ """
142
+ # Basic validation
143
+ if not isinstance(role_id, int) or role_id <= 0:
144
+ raise ValueError("role_id must be a positive integer")
145
+
146
+ params = {"force": "true" if force else "false"}
147
+ response = self._brynq.brynq_session.delete(
148
+ f"{self._brynq.url}roles/{role_id}",
149
+ params=params,
150
+ timeout=self._brynq.timeout
151
+ )
152
+ response.raise_for_status()
153
+ return response
154
+
155
+ def get_users(self, role_id: int) -> List[Dict[str, Any]]:
156
+ """Get list of users assigned to a role.
157
+
158
+ Args:
159
+ role_id (int): ID of the role to get users for
160
+
161
+ Returns:
162
+ List[Dict[str, Any]]: List of users with their details (id, name, email, active status)
163
+
164
+ Raises:
165
+ ValueError: If role_id is not a positive integer or if the response data is invalid.
166
+ requests.exceptions.RequestException: If the API request fails.
167
+ """
168
+ # Basic validation
169
+ if not isinstance(role_id, int) or role_id <= 0:
170
+ raise ValueError("role_id must be a positive integer")
171
+
172
+ response = self._brynq.brynq_session.get(
173
+ f"{self._brynq.url}roles/{role_id}/users",
174
+ timeout=self._brynq.timeout
175
+ )
176
+ response.raise_for_status()
177
+
178
+ try:
179
+ users_data = response.json()
180
+ valid_data, _ = Functions.validate_pydantic_data(users_data, schema=RoleUser)
181
+ return valid_data
182
+ except ValueError as e:
183
+ raise ValueError(f"Invalid user data received from API: {str(e)}")
184
+
185
+ def assign_dashboard_rights(self, role_id: int, dashboard_rights: List[Dict[str, Any]]) -> None:
186
+ """Assign or update dashboard rights for a role.
187
+
188
+ Args:
189
+ role_id (int): ID of the role
190
+ dashboard_rights: List of dashboard rights, each containing:
191
+ - dashboardId (int): ID of the dashboard
192
+ - editable (bool): Whether the dashboard is editable
193
+ - entities (List[int]): List of entity IDs
194
+
195
+ Raises:
196
+ ValueError: If role_id is not a positive integer or if dashboard_rights data is invalid.
197
+ requests.exceptions.RequestException: If the API request fails.
198
+ """
199
+ # Basic validation
200
+ if not isinstance(role_id, int) or role_id <= 0:
201
+ raise ValueError("role_id must be a positive integer")
202
+
203
+ try:
204
+ valid_data, _ = Functions.validate_pydantic_data(dashboard_rights, schema=DashboardRight)
205
+ response = self._brynq.brynq_session.post(
206
+ f"{self._brynq.url}roles/{role_id}/dashboards",
207
+ json=valid_data[0],
208
+ timeout=self._brynq.timeout
209
+ )
210
+ response.raise_for_status()
211
+ return response
212
+ except ValueError as e:
213
+ raise ValueError(f"Invalid dashboard rights data: {str(e)}")
214
+
215
+ def assign_qlik_dashboard_rights(self, role_id: int, qlik_dashboard_rights: List[Dict[str, Any]]) -> None:
216
+ """Assign or update Qlik dashboard rights for a role.
217
+
218
+ Args:
219
+ role_id (int): ID of the role
220
+ qlik_dashboard_rights: List of Qlik dashboard rights, each containing:
221
+ - guid (str): Dashboard GUID
222
+ - dataModelEditable (bool): Whether the data model is editable
223
+ - editable (bool): Whether the dashboard is editable
224
+ - entities (List[int]): List of entity IDs
225
+
226
+ Raises:
227
+ ValueError: If role_id is not a positive integer or if qlik_dashboard_rights data is invalid.
228
+ requests.exceptions.RequestException: If the API request fails.
229
+ """
230
+ # Basic validation
231
+ if not isinstance(role_id, int) or role_id <= 0:
232
+ raise ValueError("role_id must be a positive integer")
233
+
234
+ try:
235
+ valid_data, _ = Functions.validate_pydantic_data(qlik_dashboard_rights, schema=QlikDashboardRight)
236
+ response = self._brynq.brynq_session.post(
237
+ f"{self._brynq.url}roles/{role_id}/dashboards/qlik",
238
+ json=valid_data[0],
239
+ timeout=self._brynq.timeout
240
+ )
241
+ response.raise_for_status()
242
+ return response
243
+ except ValueError as e:
244
+ raise ValueError(f"Invalid Qlik dashboard rights data: {str(e)}")
245
+
246
+ def assign_dashboard_qlik(self, role_id: int, qlik_dashboards: list) -> dict:
247
+ """Assign or update role Qlik dashboard rights.
248
+
249
+ Args:
250
+ role_id (int): ID of the role
251
+ qlik_dashboards (list): List of Qlik dashboard rights objects
252
+
253
+ Returns:
254
+ dict: Response from the API
255
+
256
+ Raises:
257
+ ValueError: If role_id is not a positive integer or if input data is invalid
258
+ requests.exceptions.RequestException: If the API request fails
259
+ """
260
+ if not isinstance(role_id, int) or role_id <= 0:
261
+ raise ValueError("role_id must be a positive integer")
262
+
263
+ payload = {"qlikDashboards": qlik_dashboards}
264
+ valid_data, _ = Functions.validate_pydantic_data(payload, schema=QlikDashboardRight)
265
+
266
+ response = self._brynq.brynq_session.post(
267
+ f"{self._brynq.url}roles/{role_id}/dashboards/qlik",
268
+ json=valid_data[0],
269
+ timeout=self._brynq.timeout
270
+ )
271
+ response.raise_for_status()
272
+ return response.json()