1313# See the License for the specific language governing permissions and
1414# limitations under the License.
1515
16+ import json
1617import requests
1718import slumber
1819
@@ -35,23 +36,45 @@ def __init__(self, token):
3536 self .api = slumber .API (API_URL ,
3637 session = api_session , append_slash = False )
3738
39+ def handle_error (self , exception ):
40+ content = json .loads (exception .response ._content )
41+ return {'error' : content ['error' ],
42+ 'code' : content ['code' ],
43+ 'status_code' : exception .response .status_code ,
44+ 'url' : exception .response .url }
45+
3846 def list_platforms (self ):
3947 """Get a list of links to the platforms."""
4048
41- return self .api .storage .c14 .platform .get ()
49+ try :
50+ res = self .api .storage .c14 .platform .get ()
51+ except slumber .exceptions .HttpClientError as e :
52+ res = self .handle_error (e )
53+
54+ return res
4255
4356 def get_platform (self , id ):
4457 """Get information on a platform.
4558
4659 :param id: ID of the platform.
4760 """
4861
49- return self .api .storage .c14 .platform (id ).get ()
62+ try :
63+ res = self .api .storage .c14 .platform (id ).get ()
64+ except slumber .exceptions .HttpClientError as e :
65+ res = self .handle_error (e )
66+
67+ return res
5068
5169 def list_protocols (self ):
5270 """Get a list of available file transfer protocols."""
5371
54- return self .api .storage .c14 .protocol .get ()
72+ try :
73+ res = self .api .storage .c14 .protocol .get ()
74+ except slumber .exceptions .HttpClientError as e :
75+ res = self .handle_error (e )
76+
77+ return res
5578
5679 def create_safe (self , name , description = None ):
5780 """Create a safe.
@@ -60,16 +83,26 @@ def create_safe(self, name, description=None):
6083 :param description: Description of the safe.
6184 """
6285
63- return self .api .storage .c14 .safe .post ({'name' : name ,
64- 'description' : description })
86+ try :
87+ res = self .api .storage .c14 .safe .post ({'name' : name ,
88+ 'description' : description })
89+ except slumber .exceptions .HttpClientError as e :
90+ res = self .handle_error (e )
91+
92+ return res
6593
6694 def get_safe (self , uuid ):
6795 """Get information on a safe.
6896
6997 :param uuid: Id of the safe.
7098 """
7199
72- return self .api .storage .c14 .safe (uuid ).get ()
100+ try :
101+ res = self .api .storage .c14 .safe (uuid ).get ()
102+ except slumber .exceptions .HttpClientError as e :
103+ res = self .handle_error (e )
104+
105+ return res
73106
74107 def update_safe (self , uuid , name = None , description = None ):
75108 """Edit a safe.
@@ -78,21 +111,37 @@ def update_safe(self, uuid, name=None, description=None):
78111 :param name: Name of the safe.
79112 :param description: Description of the safe.
80113 """
81- return self .api .storage .c14 .safe (uuid ).patch (name = name ,
82- description = description )
114+
115+ try :
116+ res = (self .api .storage .c14 .safe (uuid )
117+ .patch (name = name , description = description ))
118+ except slumber .exceptions .HttpClientError as e :
119+ res = self .handle_error (e )
120+
121+ return res
83122
84123 def list_safes (self ):
85124 """Get a list of links to the user's safes."""
86125
87- return self .api .storage .c14 .safe .get ()
126+ try :
127+ res = self .api .storage .c14 .safe .get ()
128+ except slumber .exceptions .HttpClientError as e :
129+ res = self .handle_error (e )
130+
131+ return res
88132
89133 def delete_safe (self , uuid ):
90134 """Delete a safe.
91135
92136 :param uuid: Id of the safe.
93137 """
94138
95- return self .api .storage .c14 .safe (uuid ).delete ()
139+ try :
140+ res = self .api .storage .c14 .safe (uuid ).delete ()
141+ except slumber .exceptions .HttpClientError as e :
142+ res = self .handle_error (e )
143+
144+ return res
96145
97146 def create_archive (self , safe_id , name , description , protocols , platforms ,
98147 parity = None , ssh_keys = None , days = None ):
@@ -109,16 +158,21 @@ def create_archive(self, safe_id, name, description, protocols, platforms,
109158 (2, 5, or 7; default: 7).
110159 """
111160
112- data = {'name' : name ,
113- 'description' : description ,
114- 'parity' : parity ,
115- 'protocols' : protocols ,
116- 'ssh_keys' : ssh_keys ,
117- 'days' : days ,
118- 'platforms' : platforms }
161+ try :
162+ data = {'name' : name ,
163+ 'description' : description ,
164+ 'parity' : parity ,
165+ 'protocols' : protocols ,
166+ 'ssh_keys' : ssh_keys ,
167+ 'days' : days ,
168+ 'platforms' : platforms }
119169
120- data = dict ((k , v ) for k , v in data .iteritems () if v is not None )
121- return self .api .storage .c14 .safe (safe_id ).archive .post (data )
170+ data = dict ((k , v ) for k , v in data .iteritems () if v is not None )
171+ res = self .api .storage .c14 .safe (safe_id ).archive .post (data )
172+ except slumber .exceptions .HttpClientError as e :
173+ res = self .handle_error (e )
174+
175+ return res
122176
123177 def get_archive (self , safe_id , uuid ):
124178 """Get information on an Archive.
@@ -127,7 +181,12 @@ def get_archive(self, safe_id, uuid):
127181 :param uuid: Id of the archive.
128182 """
129183
130- return self .api .storage .c14 .safe (safe_id ).archive (uuid ).get ()
184+ try :
185+ res = self .api .storage .c14 .safe (safe_id ).archive (uuid ).get ()
186+ except slumber .exceptions .HttpClientError as e :
187+ res = self .handle_error (e )
188+
189+ return res
131190
132191 def update_archive (self , uuid , name = None , description = None ):
133192 """Edit an archive.
@@ -138,16 +197,26 @@ def update_archive(self, uuid, name=None, description=None):
138197 :param description: Description of the archive.
139198 """
140199
141- return self .api .storage .c14 .safe (uuid ).patch (name = name ,
142- description = description )
200+ try :
201+ res = (self .api .storage .c14 .safe (uuid )
202+ .patch (name = name , description = description ))
203+ except slumber .exceptions .HttpClientError as e :
204+ res = self .handle_error (e )
205+
206+ return res
143207
144208 def list_archives (self , safe_id ):
145209 """Get a list of archives in the user's safe.
146210
147211 :param safe_id: Id of the safe.
148212 """
149213
150- return self .api .storage .c14 .safe (safe_id ).archive .get ()
214+ try :
215+ res = self .api .storage .c14 .safe (safe_id ).archive .get ()
216+ except slumber .exceptions .HttpClientError as e :
217+ res = self .handle_error (e )
218+
219+ return res
151220
152221 def delete_archive (self , safe_id , uuid ):
153222 """Delete an archive.
@@ -156,7 +225,12 @@ def delete_archive(self, safe_id, uuid):
156225 :param uuid: Id of the archive.
157226 """
158227
159- return self .api .storage .c14 .safe (safe_id ).archive (uuid ).delete ()
228+ try :
229+ res = self .api .storage .c14 .safe (safe_id ).archive (uuid ).delete ()
230+ except slumber .exceptions .HttpClientError as e :
231+ res = self .handle_error (e )
232+
233+ return res
160234
161235 def archive_archive (self , safe_id , uuid ):
162236 """Archive files from temporary storage.
@@ -165,7 +239,13 @@ def archive_archive(self, safe_id, uuid):
165239 :param uuid: Id of the archive.
166240 """
167241
168- return self .api .storage .c14 .safe (safe_id ).archive (uuid ).archive .post ()
242+ try :
243+ res = (self .api .storage .c14 .safe (safe_id ).archive (uuid ).archive
244+ .post ())
245+ except slumber .exceptions .HttpClientError as e :
246+ res = self .handle_error (e )
247+
248+ return res
169249
170250 def archive_informations (self , safe_id , uuid ):
171251 """Get information on an archive's temporary storage.
@@ -174,7 +254,13 @@ def archive_informations(self, safe_id, uuid):
174254 :param uuid: Id of the archive.
175255 """
176256
177- return self .api .storage .c14 .safe (safe_id ).archive (uuid ).bucket .get ()
257+ try :
258+ res = (self .api .storage .c14 .safe (safe_id ).archive (uuid ).bucket
259+ .get ())
260+ except slumber .exceptions .HttpClientError as e :
261+ res = self .handle_error (e )
262+
263+ return res
178264
179265 def archive_list_jobs (self , safe_id , uuid ):
180266 """Get list of archive jobs.
@@ -183,7 +269,12 @@ def archive_list_jobs(self, safe_id, uuid):
183269 :param uuid: Id of the archive.
184270 """
185271
186- return self .api .storage .c14 .safe (safe_id ).archive (uuid ).job .get ()
272+ try :
273+ res = self .api .storage .c14 .safe (safe_id ).archive (uuid ).job .get ()
274+ except slumber .exceptions .HttpClientError as e :
275+ res = self .handle_error (e )
276+
277+ return res
187278
188279 def archive_get_job (self , safe_id , uuid , job_id ):
189280 """Get informations of a job.
@@ -193,8 +284,13 @@ def archive_get_job(self, safe_id, uuid, job_id):
193284 :param job_id: Id of the job.
194285 """
195286
196- return (self .api .storage .c14 .safe (safe_id ).archive (uuid ).job (job_id )
197- .get ())
287+ try :
288+ res = (self .api .storage .c14 .safe (safe_id ).archive (uuid )
289+ .job (job_id ).get ())
290+ except slumber .exceptions .HttpClientError as e :
291+ res = self .handle_error (e )
292+
293+ return res
198294
199295 def archive_get_encryption_key (self , safe_id , uuid ):
200296 """Get an archive's encryption key.
@@ -203,7 +299,12 @@ def archive_get_encryption_key(self, safe_id, uuid):
203299 :param uuid: Id of the archive.
204300 """
205301
206- return self .api .storage .c14 .safe (safe_id ).archive (uuid ).key .get ()
302+ try :
303+ res = self .api .storage .c14 .safe (safe_id ).archive (uuid ).key .get ()
304+ except slumber .exceptions .HttpClientError as e :
305+ res = self .handle_error (e )
306+
307+ return res
207308
208309 def archive_set_encryption_key (self , safe_id , uuid , key ):
209310 """Set an archive's encryption key.
@@ -213,8 +314,14 @@ def archive_set_encryption_key(self, safe_id, uuid, key):
213314 :param key: Encryption key.
214315 """
215316
216- data = {'key' : key }
217- return self .api .storage .c14 .safe (safe_id ).archive (uuid ).key .post (data )
317+ try :
318+ data = {'key' : key }
319+ res = (self .api .storage .c14 .safe (safe_id ).archive (uuid ).key
320+ .post (data ))
321+ except slumber .exceptions .HttpClientError as e :
322+ res = self .handle_error (e )
323+
324+ return res
218325
219326 def archive_delete_encryption_key (self , safe_id , uuid ):
220327 """Delete an archive's encryption key.
@@ -223,7 +330,12 @@ def archive_delete_encryption_key(self, safe_id, uuid):
223330 :param uuid: Id of the archive.
224331 """
225332
226- return self .api .storage .c14 .safe (safe_id ).archive (uuid ).key .delete ()
333+ try :
334+ res = self .api .storage .c14 .safe (safe_id ).archive (uuid ).key .delete ()
335+ except slumber .exceptions .HttpClientError as e :
336+ res = self .handle_error (e )
337+
338+ return res
227339
228340 def archive_list_locations (self , safe_id , uuid ):
229341 """Get a list of locations on the user's archive.
@@ -232,7 +344,13 @@ def archive_list_locations(self, safe_id, uuid):
232344 :param uuid: Id of the archive.
233345 """
234346
235- return self .api .storage .c14 .safe (safe_id ).archive (uuid ).location .get ()
347+ try :
348+ res = (self .api .storage .c14 .safe (safe_id ).archive (uuid ).location
349+ .get ())
350+ except slumber .exceptions .HttpClientError as e :
351+ res = self .handle_error (e )
352+
353+ return res
236354
237355 def archive_get_location (self , safe_id , uuid , location_id ):
238356 """Get information on an archive location.
@@ -241,8 +359,13 @@ def archive_get_location(self, safe_id, uuid, location_id):
241359 :param uuid: Id of the archive.
242360 """
243361
244- return (self .api .storage .c14 .safe (safe_id ).archive (uuid )
245- .location (location_id ).get ())
362+ try :
363+ res = (self .api .storage .c14 .safe (safe_id ).archive (uuid )
364+ .location (location_id ).get ())
365+ except slumber .exceptions .HttpClientError as e :
366+ res = self .handle_error (e )
367+
368+ return res
246369
247370 def verify_archive (self , safe_id , uuid , location_id ):
248371 """Verify the files on an archive's location.
@@ -252,8 +375,13 @@ def verify_archive(self, safe_id, uuid, location_id):
252375 :param location_id: Id of the location.
253376 """
254377
255- return (self .api .storage .c14 .safe (safe_id ).archive (uuid )
256- .location (location_id ).verify .post ())
378+ try :
379+ res = (self .api .storage .c14 .safe (safe_id ).archive (uuid )
380+ .location (location_id ).verify .post ())
381+ except slumber .exceptions .HttpClientError as e :
382+ res = self .handle_error (e )
383+
384+ return res
257385
258386 def unarchive (self , safe_id , uuid , location_id , protocols , rearchive = None ,
259387 key = None , ssh_keys = None ):
@@ -268,12 +396,17 @@ def unarchive(self, safe_id, uuid, location_id, protocols, rearchive=None,
268396 :param ssh_keys: UUIDs of SSH keys.
269397 """
270398
271- data = {'location_id' : location_id ,
272- 'protocols' : protocols ,
273- 'rearchive' : rearchive ,
274- 'key' : key ,
275- 'ssh_keys' : ssh_keys }
399+ try :
400+ data = {'location_id' : location_id ,
401+ 'protocols' : protocols ,
402+ 'rearchive' : rearchive ,
403+ 'key' : key ,
404+ 'ssh_keys' : ssh_keys }
405+
406+ data = dict ((k , v ) for k , v in data .iteritems () if v is not None )
407+ res = (self .api .storage .c14 .safe (safe_id ).archive (uuid ).unarchive
408+ .post (data ))
409+ except slumber .exceptions .HttpClientError as e :
410+ res = self .handle_error (e )
276411
277- data = dict ((k , v ) for k , v in data .iteritems () if v is not None )
278- return (self .api .storage .c14 .safe (safe_id ).archive (uuid ).unarchive
279- .post (data ))
412+ return res
0 commit comments