-
Notifications
You must be signed in to change notification settings - Fork 5
/
netfabb.py
170 lines (121 loc) · 4.16 KB
/
netfabb.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import os
import hashlib
import requests
import xmltodict
'''
Exceptions
'''
class NetfabbCantConnectToAPI(Exception): pass
class NetfabbErrorNoError(Exception): pass
class NetfabbResultMissing(Exception): pass
class NetfabbXMLResponseError(Exception): pass
'''
Netfabb API client
'''
class Netfabb(object):
def __init__(self, login, password):
self._login = login
self._password = password
self._base_url = 'https://netfabb.azurewebsites.net/api/'
def _get_base_parameters(self):
parameters = {}
parameters['login'] = self._login
parameters['password'] = self._password
parameters['protocol'] = 100
return parameters
def _call_method(self, method_name, parameters=None):
if parameters == None:
parameters = {}
parameters.update(self._get_base_parameters())
parameters['methodname'] = method_name
if 'filename' in parameters:
files = {
'filedata': (os.path.basename(parameters['filename']), open(parameters['filename'], 'rb'),
'application/octet-stream')
}
parameters.pop('filename', None)
response = requests.post(url=self._base_url, data=parameters, files=files, verify=True)
else:
response = requests.post(url=self._base_url, params=parameters, verify=True)
if response.status_code != 200:
raise NetfabbCantConnectToAPI(response)
output_obj = (xmltodict.parse(response.content))['netfabbapi']
if int(output_obj['success']) == 1:
return output_obj
elif not output_obj['errorcode'] or not output_obj['errormessage']:
raise NetfabbXMLResponseError
else:
raise NetfabbErrorNoError
def new_project(self):
output = self._call_method('newproject')
if int(output['success']) == 1:
if not output['projectuuid']:
raise NetfabbResultMissing
return output['projectuuid']
raise NetfabbErrorNoError
def file_upload(self, project_id, file_name, description=None):
if description == None:
description = 'No description provided'
file_name = os.path.realpath(file_name)
if not os.path.isfile(file_name):
raise NetfabbFileError
file_size = os.stat(file_name).st_size
if file_size <= 0 or file_size > 104857600:
raise NetfabbFileSizeError
file_md5 = hashlib.md5(open(file_name).read()).hexdigest()
if not file_md5:
raise NetfabbFileError
parameters = {
'projectuuid': project_id,
'description': description,
'filesize': file_size,
'filemd5': file_md5,
'filename': file_name
}
output = self._call_method('uploadfile', parameters)
if int(output['success']) == 1:
if not output['fileuuid']:
raise NetfabbResultMissing
return output['fileuuid']
raise NetfabbErrorNoError
def new_job(self, project_id, job_type, job_parameters):
parameters = {
'projectuuid': project_id,
'jobtype': job_type,
'jobparameter': job_parameters
}
output = self._call_method('newjob', parameters)
if int(output['success']) == 1:
if not output['jobuuid']:
raise NetfabbResultMissing
return output['jobuuid']
raise NetfabbErrorNoError
def retrieve_job_status(self, job_id):
parameters = {
'jobuuid': job_id
}
output = self._call_method('retrievejobstatus', parameters)
if int(output['success']) == 1:
if not output['jobstatus']:
raise NetfabbResultMissing
return output['jobstatus']
raise NetfabbErrorNoError
def retrieve_job_results(self, job_id):
parameters = {
'jobuuid': job_id
}
output = self._call_method('retrievejobresults', parameters)
return output
def file_download(self, file_id, file_name):
parameters = self._get_base_parameters()
parameters['methodname'] = 'downloadfile'
parameters['fileuuid'] = file_id
response = requests.get(self._base_url, params=parameters, verify=True, stream=True)
if response.status_code != 200:
raise NetfabbCantConnectToAPI(response)
with open(file_name, 'wb') as f:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
f.flush()
return True