loci-api 0.1.0__py2.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.
loci_api/__init__.py ADDED
@@ -0,0 +1,2 @@
1
+ __version__ = "0.1.0"
2
+ __credits__ = 'AuroraLabs'
loci_api/api_helper.py ADDED
@@ -0,0 +1,242 @@
1
+ import time
2
+
3
+ import requests
4
+ import os
5
+ import json
6
+
7
+ backend_host_url = os.environ['LOCI_BACKEND_URL']
8
+ x_api_key = os.environ['LOCI_API_KEY']
9
+
10
+
11
+ def upload_binary(file_path, version_name, compare_version_id, project_id, platform):
12
+ """
13
+ Uploads a file via POST request
14
+
15
+ Args:
16
+ url (str): The API endpoint URL
17
+ file_path (str): Path to the file to upload
18
+ field_name (str): The form field name for the file (default: 'file')
19
+ """
20
+ # Check if file exists
21
+ if not os.path.isfile(file_path):
22
+ print(f"Error: File '{file_path}' does not exist")
23
+ return None
24
+
25
+ try:
26
+ url = backend_host_url + '/api/v1/reports/xapi-upload'
27
+ print(f"Uploading file: {file_path}")
28
+ print(f"To URL: {url}")
29
+
30
+ # Open the file in binary mode and send the request
31
+
32
+ files = {'binaryFile': (file_path, open(file_path, 'rb'), 'application/octet-stream')}
33
+ values = {'versionName': version_name,
34
+ 'compareVersionId': compare_version_id,
35
+ 'projectId': project_id,
36
+ 'platform': platform}
37
+ headers = {"X-Api-Key": x_api_key}
38
+
39
+ response = requests.post(url, files=files, headers=headers, data=values)
40
+
41
+
42
+ # Check if request was successful
43
+ response.raise_for_status()
44
+
45
+ print("\nServer Response:")
46
+
47
+ # Try to parse JSON response
48
+ try:
49
+ json_response = response.json()
50
+ print(json.dumps(json_response, indent=2))
51
+ return json_response['eventDetails']['reportId']
52
+ except ValueError:
53
+ print(response.text)
54
+ return response.text
55
+
56
+ except requests.exceptions.RequestException as e:
57
+ print(f"Error uploading file: {e}")
58
+ return None
59
+ except Exception as e:
60
+ print(f"Unexpected error: {e}")
61
+ return None
62
+
63
+
64
+ def get_last_version_id(project_id):
65
+ try:
66
+ url = backend_host_url + '/api/v1/graph/xapi-project-versions'
67
+ print(f"To URL: {url}")
68
+
69
+ headers = {"X-Api-Key": x_api_key}
70
+ values = {'projectId': project_id,
71
+ 'app': 'diag_poc'}
72
+
73
+ response = requests.post(url, headers=headers, data=values)
74
+
75
+
76
+ # Check if request was successful
77
+ response.raise_for_status()
78
+
79
+ print("\nServer Response:")
80
+
81
+ # Try to parse JSON response
82
+ version_id = ''
83
+ version_date = '0000-00-00'
84
+ try:
85
+ json_response = response.json()
86
+ print(json.dumps(json_response, indent=2))
87
+ for version in json_response['message']:
88
+ if version[0]['properties']['status'] == 0:
89
+ if version[0]['properties']['end_dt'] > version_date:
90
+ version_id = version[0]['properties']['version_id']
91
+ version_date = version[0]['properties']['end_dt']
92
+ return version_id
93
+ except ValueError:
94
+ print(response.text)
95
+ return ""
96
+
97
+ except requests.exceptions.RequestException as e:
98
+ print(f"Error error getting latest version id: {e}")
99
+ return ""
100
+ except Exception as e:
101
+ print(f"Unexpected error: {e}")
102
+ return ""
103
+
104
+ def get_versions(project_id):
105
+ try:
106
+ url = backend_host_url + '/api/v1/graph/xapi-project-versions'
107
+ print(f"To URL: {url}")
108
+
109
+ headers = {"X-Api-Key": x_api_key}
110
+ values = {'projectId': project_id,
111
+ 'app': 'diag_poc'}
112
+
113
+ response = requests.post(url, headers=headers, data=values)
114
+
115
+
116
+ # Check if request was successful
117
+ response.raise_for_status()
118
+
119
+ print("\nServer Response:")
120
+
121
+ # Try to parse JSON response
122
+ try:
123
+ json_response = response.json()
124
+ print(json.dumps(json_response, indent=2))
125
+ versions = []
126
+ for version in json_response['message']:
127
+ versions.append(version[0])
128
+ return versions
129
+ except ValueError:
130
+ print(response.text)
131
+ return []
132
+
133
+ except requests.exceptions.RequestException as e:
134
+ print(f"Error error getting latest version id: {e}")
135
+ return []
136
+ except Exception as e:
137
+ print(f"Unexpected error: {e}")
138
+ return []
139
+
140
+ def get_project_id(project_name):
141
+ try:
142
+ url = backend_host_url + '/api/v1/projects/xapi-list-all'
143
+ print(f"To URL: {url}")
144
+
145
+ headers = {"X-Api-Key": x_api_key}
146
+
147
+ response = requests.get(url, headers=headers)
148
+
149
+ # Check if request was successful
150
+ response.raise_for_status()
151
+
152
+ print("\nServer Response:")
153
+
154
+ # Try to parse JSON response
155
+ try:
156
+ json_response = response.json()
157
+ print(json.dumps(json_response, indent=2))
158
+ for project in json_response:
159
+ if project['name'] == project_name:
160
+ return project['id']
161
+ return None
162
+ except ValueError:
163
+ print(response.text)
164
+ return None
165
+
166
+ except requests.exceptions.RequestException as e:
167
+ print(f"Error getting project id: {e}")
168
+ return None
169
+ except Exception as e:
170
+ print(f"Unexpected error: {e}")
171
+ return None
172
+
173
+
174
+ def upload_finished(project_id, report_id):
175
+ try:
176
+ url = backend_host_url + '/api/v1/reports/xapi-progress'
177
+ print(f"To URL: {url}")
178
+
179
+ values = {'projectId': project_id,
180
+ 'reportId': report_id}
181
+
182
+ response = requests.post(url, data=values)
183
+
184
+ # Check if request was successful
185
+ response.raise_for_status()
186
+
187
+ print("Server Response:")
188
+
189
+ # Try to parse JSON respons
190
+ try:
191
+ json_response = response.json()
192
+ print(json.dumps(json_response, indent=2))
193
+ status = json_response['progress']['status']
194
+
195
+ if status == 1 or status == 0:
196
+ return (True, status)
197
+ else:
198
+ return (False, None)
199
+
200
+ except ValueError:
201
+ print(response.text)
202
+ return (True, 0)
203
+
204
+ except requests.exceptions.RequestException as e:
205
+ print(f"Error error getting latest version id: {e}")
206
+ return (True, 0)
207
+ except Exception as e:
208
+ print(f"Unexpected error: {e}")
209
+ return (True, 0)
210
+
211
+
212
+ def full_upload(file_path, version_name, platform, project_name, use_latest=True, compare_version_id=''):
213
+
214
+ project_id = get_project_id(project_name)
215
+ if project_id is None:
216
+ print("Uploading failed, Project does not exist.")
217
+ exit(-1)
218
+
219
+ if use_latest:
220
+ compare_version_id = get_last_version_id(project_id)
221
+
222
+ reportId = upload_binary(file_path, version_name, compare_version_id, project_id, platform)
223
+
224
+ if reportId != '':
225
+ print(
226
+ f"Uploaded binary Report ID: {reportId}, Compare Version ID: {compare_version_id}, Project ID: {project_id}"
227
+ )
228
+ else:
229
+ print("Uploading failed, See previous message for more details.")
230
+ exit(-1)
231
+
232
+ finished = False
233
+ status = 0
234
+
235
+ print("Waiting for processing to finish")
236
+ while not finished:
237
+ finished, status = upload_finished(project_id, reportId)
238
+ time.sleep(10)
239
+
240
+ return status
241
+
242
+
@@ -0,0 +1,22 @@
1
+ Metadata-Version: 2.1
2
+ Name: loci-api
3
+ Version: 0.1.0
4
+ Summary: Aurora Labs API helper for automated upload to Loci Binary Analysis tool
5
+ Home-page: https://loci-dev.net
6
+ Author: Aurora Labs
7
+ Author-email: info@auroralabs.com
8
+ License: Apache License 2.0
9
+ Classifier: Development Status :: 5 - Production/Stable
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Natural Language :: English
12
+ Classifier: License :: OSI Approved :: Apache Software License
13
+ Classifier: Programming Language :: Python
14
+ Classifier: Programming Language :: Python :: 2
15
+ Classifier: Programming Language :: Python :: 2.7
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.6
18
+ Classifier: Programming Language :: Python :: 3.7
19
+ Classifier: Programming Language :: Python :: 3.8
20
+ Requires-Python: >= 2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*
21
+ Requires-Dist: requests
22
+
@@ -0,0 +1,8 @@
1
+ loci_api/__init__.py,sha256=ol2YxoOsv-9IdnA3aXLQ7nryZ4z_3ymZywoSAs3W3gI,48
2
+ loci_api/api_helper.py,sha256=KyaIk8EbQ6-QwWF5uuYTaKEJVjRK3PDPMGtcuM1D3MY,7200
3
+ loci_cli/__init__.py,sha256=JoO1NsIqiIh0_6-AxNCkfvUSk_12RsHrYKoDz41Hfn8,48
4
+ loci_cli/api_helper.py,sha256=KyaIk8EbQ6-QwWF5uuYTaKEJVjRK3PDPMGtcuM1D3MY,7200
5
+ loci_api-0.1.0.dist-info/METADATA,sha256=vtYAEXOjfhBj5aDi2uYCN8v2vCT9uDUIFGz2MxnUeRQ,893
6
+ loci_api-0.1.0.dist-info/WHEEL,sha256=bb2Ot9scclHKMOLDEHY6B2sicWOgugjFKaJsT7vwMQo,110
7
+ loci_api-0.1.0.dist-info/top_level.txt,sha256=N2SAPuyRvnsKx14MYVQrqhi75nvjFm7oGltpB1HXujA,9
8
+ loci_api-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,6 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.38.4)
3
+ Root-Is-Purelib: true
4
+ Tag: py2-none-any
5
+ Tag: py3-none-any
6
+
@@ -0,0 +1 @@
1
+ loci_api
loci_cli/__init__.py ADDED
@@ -0,0 +1,2 @@
1
+ __version__ = "0.1.1"
2
+ __credits__ = 'AuroraLabs'
loci_cli/api_helper.py ADDED
@@ -0,0 +1,242 @@
1
+ import time
2
+
3
+ import requests
4
+ import os
5
+ import json
6
+
7
+ backend_host_url = os.environ['LOCI_BACKEND_URL']
8
+ x_api_key = os.environ['LOCI_API_KEY']
9
+
10
+
11
+ def upload_binary(file_path, version_name, compare_version_id, project_id, platform):
12
+ """
13
+ Uploads a file via POST request
14
+
15
+ Args:
16
+ url (str): The API endpoint URL
17
+ file_path (str): Path to the file to upload
18
+ field_name (str): The form field name for the file (default: 'file')
19
+ """
20
+ # Check if file exists
21
+ if not os.path.isfile(file_path):
22
+ print(f"Error: File '{file_path}' does not exist")
23
+ return None
24
+
25
+ try:
26
+ url = backend_host_url + '/api/v1/reports/xapi-upload'
27
+ print(f"Uploading file: {file_path}")
28
+ print(f"To URL: {url}")
29
+
30
+ # Open the file in binary mode and send the request
31
+
32
+ files = {'binaryFile': (file_path, open(file_path, 'rb'), 'application/octet-stream')}
33
+ values = {'versionName': version_name,
34
+ 'compareVersionId': compare_version_id,
35
+ 'projectId': project_id,
36
+ 'platform': platform}
37
+ headers = {"X-Api-Key": x_api_key}
38
+
39
+ response = requests.post(url, files=files, headers=headers, data=values)
40
+
41
+
42
+ # Check if request was successful
43
+ response.raise_for_status()
44
+
45
+ print("\nServer Response:")
46
+
47
+ # Try to parse JSON response
48
+ try:
49
+ json_response = response.json()
50
+ print(json.dumps(json_response, indent=2))
51
+ return json_response['eventDetails']['reportId']
52
+ except ValueError:
53
+ print(response.text)
54
+ return response.text
55
+
56
+ except requests.exceptions.RequestException as e:
57
+ print(f"Error uploading file: {e}")
58
+ return None
59
+ except Exception as e:
60
+ print(f"Unexpected error: {e}")
61
+ return None
62
+
63
+
64
+ def get_last_version_id(project_id):
65
+ try:
66
+ url = backend_host_url + '/api/v1/graph/xapi-project-versions'
67
+ print(f"To URL: {url}")
68
+
69
+ headers = {"X-Api-Key": x_api_key}
70
+ values = {'projectId': project_id,
71
+ 'app': 'diag_poc'}
72
+
73
+ response = requests.post(url, headers=headers, data=values)
74
+
75
+
76
+ # Check if request was successful
77
+ response.raise_for_status()
78
+
79
+ print("\nServer Response:")
80
+
81
+ # Try to parse JSON response
82
+ version_id = ''
83
+ version_date = '0000-00-00'
84
+ try:
85
+ json_response = response.json()
86
+ print(json.dumps(json_response, indent=2))
87
+ for version in json_response['message']:
88
+ if version[0]['properties']['status'] == 0:
89
+ if version[0]['properties']['end_dt'] > version_date:
90
+ version_id = version[0]['properties']['version_id']
91
+ version_date = version[0]['properties']['end_dt']
92
+ return version_id
93
+ except ValueError:
94
+ print(response.text)
95
+ return ""
96
+
97
+ except requests.exceptions.RequestException as e:
98
+ print(f"Error error getting latest version id: {e}")
99
+ return ""
100
+ except Exception as e:
101
+ print(f"Unexpected error: {e}")
102
+ return ""
103
+
104
+ def get_versions(project_id):
105
+ try:
106
+ url = backend_host_url + '/api/v1/graph/xapi-project-versions'
107
+ print(f"To URL: {url}")
108
+
109
+ headers = {"X-Api-Key": x_api_key}
110
+ values = {'projectId': project_id,
111
+ 'app': 'diag_poc'}
112
+
113
+ response = requests.post(url, headers=headers, data=values)
114
+
115
+
116
+ # Check if request was successful
117
+ response.raise_for_status()
118
+
119
+ print("\nServer Response:")
120
+
121
+ # Try to parse JSON response
122
+ try:
123
+ json_response = response.json()
124
+ print(json.dumps(json_response, indent=2))
125
+ versions = []
126
+ for version in json_response['message']:
127
+ versions.append(version[0])
128
+ return versions
129
+ except ValueError:
130
+ print(response.text)
131
+ return []
132
+
133
+ except requests.exceptions.RequestException as e:
134
+ print(f"Error error getting latest version id: {e}")
135
+ return []
136
+ except Exception as e:
137
+ print(f"Unexpected error: {e}")
138
+ return []
139
+
140
+ def get_project_id(project_name):
141
+ try:
142
+ url = backend_host_url + '/api/v1/projects/xapi-list-all'
143
+ print(f"To URL: {url}")
144
+
145
+ headers = {"X-Api-Key": x_api_key}
146
+
147
+ response = requests.get(url, headers=headers)
148
+
149
+ # Check if request was successful
150
+ response.raise_for_status()
151
+
152
+ print("\nServer Response:")
153
+
154
+ # Try to parse JSON response
155
+ try:
156
+ json_response = response.json()
157
+ print(json.dumps(json_response, indent=2))
158
+ for project in json_response:
159
+ if project['name'] == project_name:
160
+ return project['id']
161
+ return None
162
+ except ValueError:
163
+ print(response.text)
164
+ return None
165
+
166
+ except requests.exceptions.RequestException as e:
167
+ print(f"Error getting project id: {e}")
168
+ return None
169
+ except Exception as e:
170
+ print(f"Unexpected error: {e}")
171
+ return None
172
+
173
+
174
+ def upload_finished(project_id, report_id):
175
+ try:
176
+ url = backend_host_url + '/api/v1/reports/xapi-progress'
177
+ print(f"To URL: {url}")
178
+
179
+ values = {'projectId': project_id,
180
+ 'reportId': report_id}
181
+
182
+ response = requests.post(url, data=values)
183
+
184
+ # Check if request was successful
185
+ response.raise_for_status()
186
+
187
+ print("Server Response:")
188
+
189
+ # Try to parse JSON respons
190
+ try:
191
+ json_response = response.json()
192
+ print(json.dumps(json_response, indent=2))
193
+ status = json_response['progress']['status']
194
+
195
+ if status == 1 or status == 0:
196
+ return (True, status)
197
+ else:
198
+ return (False, None)
199
+
200
+ except ValueError:
201
+ print(response.text)
202
+ return (True, 0)
203
+
204
+ except requests.exceptions.RequestException as e:
205
+ print(f"Error error getting latest version id: {e}")
206
+ return (True, 0)
207
+ except Exception as e:
208
+ print(f"Unexpected error: {e}")
209
+ return (True, 0)
210
+
211
+
212
+ def full_upload(file_path, version_name, platform, project_name, use_latest=True, compare_version_id=''):
213
+
214
+ project_id = get_project_id(project_name)
215
+ if project_id is None:
216
+ print("Uploading failed, Project does not exist.")
217
+ exit(-1)
218
+
219
+ if use_latest:
220
+ compare_version_id = get_last_version_id(project_id)
221
+
222
+ reportId = upload_binary(file_path, version_name, compare_version_id, project_id, platform)
223
+
224
+ if reportId != '':
225
+ print(
226
+ f"Uploaded binary Report ID: {reportId}, Compare Version ID: {compare_version_id}, Project ID: {project_id}"
227
+ )
228
+ else:
229
+ print("Uploading failed, See previous message for more details.")
230
+ exit(-1)
231
+
232
+ finished = False
233
+ status = 0
234
+
235
+ print("Waiting for processing to finish")
236
+ while not finished:
237
+ finished, status = upload_finished(project_id, reportId)
238
+ time.sleep(10)
239
+
240
+ return status
241
+
242
+