brynq-sdk-brynq 1.0.0__tar.gz

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,10 @@
1
+ Metadata-Version: 1.0
2
+ Name: brynq_sdk_brynq
3
+ Version: 1.0.0
4
+ Summary: BrynQ SDK for the BrynQ.com platform
5
+ Home-page: UNKNOWN
6
+ Author: BrynQ
7
+ Author-email: support@brynq.com
8
+ License: BrynQ License
9
+ Description: BrynQ SDK for the BrynQ.com platform
10
+ Platform: UNKNOWN
@@ -0,0 +1 @@
1
+ from brynq_sdk.brynq.brynq import BrynQ
@@ -0,0 +1,267 @@
1
+ import os
2
+ import requests
3
+ import json
4
+ import pandas as pd
5
+ from typing import Union
6
+
7
+
8
+ class Brynq:
9
+ def __init__(self, subdomain: str = None, api_token: str = None, staging: str = 'prod'):
10
+ self.subdomain = os.getenv("BRYNQ_SUBDOMAIN", subdomain)
11
+ self.api_token = os.getenv("BRYNQ_API_TOKEN", api_token)
12
+ self.environment = os.getenv("BRYNQ_ENVIRONMENT", staging)
13
+
14
+ if any([self.subdomain is None, self.api_token is None]):
15
+ raise ValueError("Set the subdomain, api_token either in your .env file or provide the subdomain and api_token parameters")
16
+
17
+ possible_environments = ['dev', 'prod']
18
+ if self.environment not in possible_environments:
19
+ raise ValueError(f"Environment should be in {','.join(possible_environments)}")
20
+
21
+ self.url = 'https://app.brynq-staging.com/api/v2/' if self.environment == 'dev' else 'https://app.brynq.com/api/v2/'
22
+
23
+ def _get_headers(self):
24
+ return {
25
+ 'Authorization': f'Bearer {self.api_token}',
26
+ 'Domain': self.subdomain
27
+ }
28
+
29
+ def _get_mappings(self, task_id: int) -> dict:
30
+ """
31
+ Get the mappings from the task in BrynQ
32
+ :param task_id: The id of the task in BrynQ. this does not have to be the task id of the current task
33
+ :return: A dictionary with the following structure: {mapping_title: {tuple(input): output}}
34
+ """
35
+ response = requests.get(url=f'{self.url}interfaces/{task_id}/data-mapping', headers=self._get_headers())
36
+
37
+ if response.status_code != 200:
38
+ raise ValueError(f'Error occurred while fetching mappings: {response.text}. Please always check if you have added BRYNQ_SUBDOMAIN to your .env file')
39
+
40
+ return response.json()
41
+
42
+ def get_mapping(self, task_id: int, mapping: str, multiple_values: str = 'raise'):
43
+ """
44
+ Get the mapping json from the mappings
45
+ :param task_id: The id of the task in BrynQ. this does not have to be the task id of the current task
46
+ :param mapping: The name of the mapping
47
+ :param multiple_values: A string to indicate how to handle multiple input or output values. Options are 'raise' and 'concat'
48
+ :return: The json of the mapping
49
+ """
50
+ # Find the mapping for the given sheet name
51
+ mappings = self._get_mappings(task_id=task_id)
52
+ mapping_data = next((item for item in mappings if item['name'] == mapping), None)
53
+ if not mapping_data:
54
+ raise ValueError(f"Mapping named '{mapping}' not found")
55
+
56
+ result_mapping = {}
57
+ for value in mapping_data['values']:
58
+ if multiple_values == 'raise' and (len(value['input'].items()) > 1 or len(value['output'].items()) > 1):
59
+ raise ValueError("Multiple input or output keys found when 'raise' strategy is specified.")
60
+
61
+ input_values = []
62
+ output_values = []
63
+ for _, val in value['input'].items():
64
+ input_values.append(val)
65
+ for _, val in value['output'].items():
66
+ output_values.append(val)
67
+
68
+ if multiple_values == 'concat':
69
+ concatenated_input = ','.join(input_values)
70
+ concatenated_output = ','.join(output_values)
71
+ result_mapping[concatenated_input] = concatenated_output
72
+ else: # Default to assuming there's only one key-value pair if not concatenating
73
+ result_mapping[input_values[0]] = output_values[0]
74
+
75
+ return result_mapping
76
+
77
+ def get_mapping_as_dataframe(self, task_id: int, mapping: str, prefix: bool = False):
78
+ """
79
+ Get the mapping dataframe from the mappings
80
+ :param mapping: The name of the mapping
81
+ :param prefix: A boolean to indicate if the keys should be prefixed with 'input.' and 'output.'
82
+ :return: The dataframe of the mapping
83
+ """
84
+ # Find the mapping for the given sheet name
85
+ mappings = self._get_mappings(task_id=task_id)
86
+ mapping_data = next((item for item in mappings if item['name'] == mapping), None)
87
+ if not mapping_data:
88
+ raise ValueError(f"Mapping named '{mapping}' not found")
89
+
90
+ # Extract the values which contain the input-output mappings
91
+ values = mapping_data['values']
92
+
93
+ # Create a list to hold all row data
94
+ rows = []
95
+ for value in values:
96
+ # Check if prefix is needed and adjust keys accordingly
97
+ if prefix:
98
+ input_data = {f'input.{key}': val for key, val in value['input'].items()}
99
+ output_data = {f'output.{key}': val for key, val in value['output'].items()}
100
+ else:
101
+ input_data = value['input']
102
+ output_data = value['output']
103
+
104
+ # Combine 'input' and 'output' dictionaries
105
+ row_data = {**input_data, **output_data}
106
+ rows.append(row_data)
107
+
108
+ # Create DataFrame from rows
109
+ df = pd.DataFrame(rows)
110
+
111
+ return df
112
+
113
+ def get_system_credential(self, system: str, label: Union[str, list], test_environment: bool = False) -> json:
114
+ """
115
+ This method retrieves authentication credentials from BrynQ.
116
+ It returns the json data if the request does not return an error code
117
+ :param system: specifies which token is used. (lowercase)
118
+ :param label: reference to the used label
119
+ :param test_environment: boolean if the test environment is used
120
+ :return json response from BrynQ
121
+ """
122
+ response = requests.get(url=f'{self.url}apps/{system}', headers=self._get_headers())
123
+ response.raise_for_status()
124
+ credentials = response.json()
125
+ # rename parameter for readability
126
+ if isinstance(label, str):
127
+ labels = [label]
128
+ else:
129
+ labels = label
130
+ # filter credentials based on label. All labels specified in label parameter should be present in the credential object
131
+ credentials = [credential for credential in credentials if all(label in credential['labels'] for label in labels)]
132
+ if system == 'profit':
133
+ credentials = [credential for credential in credentials if credential['isTestEnvironment'] is test_environment]
134
+
135
+ if len(credentials) == 0:
136
+ raise ValueError(f'No credentials found for {system}')
137
+ if len(credentials) != 1:
138
+ raise ValueError(f'Multiple credentials found for {system} with the specified labels')
139
+
140
+ return credentials[0]
141
+
142
+ def refresh_system_credential(self, system: str, system_id: int) -> json:
143
+ """
144
+ This method refreshes Oauth authentication credentials in BrynQ.
145
+ It returns the json data if the request does not return an error code
146
+ :param system: specifies which token is used. (lowercase)
147
+ :param system_id: system id in BrynQ
148
+ :return json response from BrynQ
149
+ """
150
+ response = requests.post(url=f'{self.url}apps/{system}/{system_id}/refresh', headers=self._get_headers())
151
+ response.raise_for_status()
152
+ credentials = response.json()
153
+
154
+ return credentials
155
+
156
+ def get_user_data(self):
157
+ """
158
+ Get all users from BrynQ
159
+ :return: A list of users
160
+ """
161
+ return requests.get(url=f'{self.url}users', headers=self._get_headers())
162
+
163
+ def get_user_authorization_qlik_app(self,dashboard_id):
164
+ """
165
+ Get all users from BrynQ who have access to a qlik dashboard with their entities
166
+ :return: A list of users and entities
167
+ """
168
+ return requests.get(url=f'{self.url}/qlik/{dashboard_id}/users', headers=self._get_headers())
169
+
170
+ def get_role_data(self):
171
+ """
172
+ Get all roles from BrynQ
173
+ :return: A list of roles
174
+ """
175
+ return requests.get(url=f'{self.url}roles', headers=self._get_headers())
176
+
177
+ def create_user(self, user_data: dict) -> requests.Response:
178
+ """
179
+ Create a user in BrynQ
180
+ :param user_data: A dictionary with the following structure:
181
+ {
182
+ "name": "string",
183
+ "username": "string",
184
+ "email": "string",
185
+ "language": "string",
186
+ "salure_connect": bool,
187
+ "qlik_sense_analyzer": bool,
188
+ "qlik_sense_professional": bool
189
+ }
190
+ :return: A response object
191
+ """
192
+ data = {
193
+ "name": user_data['name'],
194
+ "username": user_data['username'],
195
+ "email": user_data['email'],
196
+ "language": user_data['language'],
197
+ "products": {
198
+ "qlikSenseAnalyzer": user_data['qlik_sense_analyzer'],
199
+ "qlikSenseProfessional": user_data['qlik_sense_professional']
200
+ }
201
+ }
202
+
203
+ return requests.post(url=f'{self.url}users', headers=self._get_headers(), json=data)
204
+
205
+ def update_user(self, user_id: str, user_data: dict) -> requests.Response:
206
+ """
207
+ Update a user in BrynQ
208
+ :param user_id: The id of the user in BrynQ
209
+ :param user_data: A dictionary with the following structure:
210
+ {
211
+ "id": "string",
212
+ "name": "string",
213
+ "language": "string",
214
+ "salureconnect": bool,
215
+ "qlik sense analyser": bool,
216
+ "qlik sense professional": false
217
+ }
218
+ :return: A response object
219
+ """
220
+ data = {
221
+ "name": user_data['name'],
222
+ "username": user_data['username'],
223
+ "email": user_data['email'],
224
+ "language": user_data['language'],
225
+ "products": {
226
+ "qlikSenseAnalyzer": user_data['qlik_sense_analyzer'],
227
+ "qlikSenseProfessional": user_data['qlik_sense_professional']
228
+ }
229
+ }
230
+
231
+ return requests.put(url=f'{self.url}users/{user_id}', headers=self._get_headers(), json=data)
232
+
233
+ def delete_user(self, user_id: str) -> requests.Response:
234
+ """
235
+ Delete a user in BrynQ
236
+ :param user_id: The id of the user in BrynQ
237
+ :return: A response object
238
+ """
239
+ return requests.delete(url=f'{self.url}users/{user_id}', headers=self._get_headers())
240
+
241
+ def overwrite_user_roles(self, user_id: int, roles: list) -> requests.Response:
242
+ """
243
+ Overwrite the roles of a user in BrynQ
244
+ :param user_id: The id of the user in BrynQ
245
+ :param roles: A list of role ids
246
+ :return: A response object
247
+ """
248
+ data = {
249
+ "roles": roles
250
+ }
251
+
252
+ return requests.put(url=f'{self.url}users/{user_id}/roles', headers=self._get_headers(), json=data)
253
+
254
+ def get_source_system_entities(self, system: str) -> requests.Response:
255
+ """
256
+ Get all entities from a source system in BrynQ
257
+ :param system: The name of the source system
258
+ :return: A response object
259
+ """
260
+ return requests.get(url=f'{self.url}source-systems/{system}/entities', headers=self._get_headers())
261
+
262
+ def get_layers(self) -> requests.Response:
263
+ """
264
+ Get all layers from a source system in BrynQ
265
+ :return: A response object
266
+ """
267
+ return requests.get(url=f'{self.url}organization-chart/layers', headers=self._get_headers())
@@ -0,0 +1,10 @@
1
+ Metadata-Version: 1.0
2
+ Name: brynq-sdk-brynq
3
+ Version: 1.0.0
4
+ Summary: BrynQ SDK for the BrynQ.com platform
5
+ Home-page: UNKNOWN
6
+ Author: BrynQ
7
+ Author-email: support@brynq.com
8
+ License: BrynQ License
9
+ Description: BrynQ SDK for the BrynQ.com platform
10
+ Platform: UNKNOWN
@@ -0,0 +1,9 @@
1
+ setup.py
2
+ brynq_sdk/brynq/__init__.py
3
+ brynq_sdk/brynq/brynq.py
4
+ brynq_sdk_brynq.egg-info/PKG-INFO
5
+ brynq_sdk_brynq.egg-info/SOURCES.txt
6
+ brynq_sdk_brynq.egg-info/dependency_links.txt
7
+ brynq_sdk_brynq.egg-info/not-zip-safe
8
+ brynq_sdk_brynq.egg-info/requires.txt
9
+ brynq_sdk_brynq.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ requests<=3,>=2
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,17 @@
1
+ from setuptools import setup
2
+
3
+
4
+ setup(
5
+ name='brynq_sdk_brynq',
6
+ version='1.0.0',
7
+ description='BrynQ SDK for the BrynQ.com platform',
8
+ long_description='BrynQ SDK for the BrynQ.com platform',
9
+ author='BrynQ',
10
+ author_email='support@brynq.com',
11
+ packages=["brynq_sdk.brynq"],
12
+ license='BrynQ License',
13
+ install_requires=[
14
+ 'requests>=2,<=3'
15
+ ],
16
+ zip_safe=False,
17
+ )