-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_workbook.py
executable file
·125 lines (101 loc) · 3.98 KB
/
export_workbook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python3
import argparse
import json
import time
from utils import SigmaClient
def get_workbook_schema(client, workbook_id):
""" Gets the workbook's schema
:access_token: Generated access token
:workbook_id: ID of workbook
:returns: Dictionary of workbook's schema
"""
response = client.get(
f"v2/workbooks/{workbook_id}/schema",
)
return response.json()
def export_workbook(client, workbook_id, export_format='json', element_id=None, retries=5):
""" Exports workbook to file
:access_token: Generated access token
:workbook_id: ID of workbook
:element_id: Optional element ID
:returns: JSON of workbook data
"""
payload = {
"format": {
"type": export_format
}
}
if element_id:
payload["elementId"] = element_id
response = client.post(
f"v2/workbooks/{workbook_id}/export",
json=payload
)
try:
query_id = response.json()['queryId']
return query_id
except:
err = {'status_code': response.status_code, 'content': response.text, 'retries': retries}
print(f'error: {err}')
if retries < 0:
raise
return export_workbook(client, workbook_id, export_format, element_id, retries - 1)
def retrieve_results(client, query_id):
res = None
while res is None:
time.sleep(10)
response = client.get(
f'v2/query/{query_id}/download',
)
if response.status_code == 200:
res = response.content
elif response.status_code != 204:
print(f'status: {response.status_code}, content: {response.text}')
return res
def write_to_file(filename, content, export_format='json', element_id=None):
""" Writes export result to file
:filename: Filename to write to
:data: JSON data to write
:element_id: Optional element ID if multiple exports
"""
if element_id:
filename = f"{filename}_{element_id}"
with open(f"{filename}.{export_format}", 'wb') as f:
f.write(content)
def main():
parser = argparse.ArgumentParser(
description='Export a workbook from Sigma into JSON')
parser.add_argument(
'--env', type=str, required=True, help='env to use: [production | staging].')
parser.add_argument(
'--cloud', type=str, required=True, help='Cloud to use: [aws | gcp]')
parser.add_argument(
'--client_id', type=str, required=True, help='Client ID generated from Sigma')
parser.add_argument(
'--client_secret', type=str, required=True, help='Client secret generated from Sigma')
parser.add_argument(
'--workbook_id', type=str, required=True, help='ID of workbook to be exported')
parser.add_argument(
'--element_id', type=str, help='Optional workbook element')
parser.add_argument(
'--filename', type=str, help='Optional filename prefix')
parser.add_argument(
'--format', type=str, default='json', help='Optional format: [csv | json | pdf]')
args = parser.parse_args()
client = SigmaClient(args.env, args.cloud, args.client_id, args.client_secret)
if args.element_id:
query_id = export_workbook(client, args.workbook_id, args.format, args.element_id)
content = retrieve_results(client, query_id)
filename = args.filename if args.filename else args.workbook_id
write_to_file(filename, content, args.format)
else:
schema = get_workbook_schema(client, args.workbook_id)
elements = schema["elements"]
print(elements)
for element_id, _ in elements.items():
query_id = export_workbook(client, args.workbook_id, args.format, element_id)
content = retrieve_results(client, query_id)
filename = args.filename if args.filename else args.workbook_id
write_to_file(filename, content, args.format, element_id)
if __name__ == '__main__':
main()