@@ -178,13 +178,20 @@ class _GdriveManager(object):
178
178
def __init__ (self ):
179
179
self .__auth = GdriveAuth ()
180
180
181
+ def __assert_response_kind (self , response , expected_kind ):
182
+ actual_kind = response [u'kind' ]
183
+ if actual_kind != unicode (expected_kind ):
184
+ raise ValueError ("Received response of type [%s] instead of "
185
+ "[%s]." % (actual_kind , expected_kind ))
186
+
181
187
@_marshall
182
188
def get_about_info (self ):
183
189
"""Return the 'about' information for the drive."""
184
190
185
191
client = self .__auth .get_client ()
186
192
response = client .about ().get ().execute ()
187
-
193
+ self .__assert_response_kind (response , 'drive#about' )
194
+
188
195
return response
189
196
190
197
@_marshall
@@ -205,6 +212,9 @@ def list_changes(self, start_change_id=None, page_token=None):
205
212
response = client .changes ().list (
206
213
pageToken = page_token ,
207
214
startChangeId = start_change_id ).execute ()
215
+
216
+ self .__assert_response_kind (response , 'drive#changeList' )
217
+
208
218
# TODO(dustin): Debugging. Sometimes largestChangeId is missing.
209
219
import pprint
210
220
pprint .pprint (response )
@@ -251,6 +261,7 @@ def get_parents_containing_id(self, child_id, max_results=None):
251
261
_logger .info ("Listing entries over child with ID [%s]." , child_id )
252
262
253
263
response = client .parents ().list (fileId = child_id ).execute ()
264
+ self .__assert_response_kind (response , 'drive#parentList' )
254
265
255
266
return [ entry [u'id' ] for entry in response [u'items' ] ]
256
267
@@ -287,6 +298,8 @@ def get_children_under_parent_id(self,
287
298
folderId = parent_id ,
288
299
maxResults = max_results ).execute ()
289
300
301
+ self .__assert_response_kind (response , 'drive#childList' )
302
+
290
303
return [ entry [u'id' ] for entry in response [u'items' ] ]
291
304
292
305
@_marshall
@@ -310,21 +323,10 @@ def get_entries(self, entry_ids):
310
323
def get_entry (self , entry_id ):
311
324
client = self .__auth .get_client ()
312
325
313
- try :
314
- entry_raw = client .files ().get (fileId = entry_id ).execute ()
315
- except :
316
- _logger .exception ("Could not get the file with ID [%s]." ,
317
- entry_id )
318
- raise
326
+ response = client .files ().get (fileId = entry_id ).execute ()
327
+ self .__assert_response_kind (response , 'drive#file' )
319
328
320
- try :
321
- entry = NormalEntry ('direct_read' , entry_raw )
322
- except :
323
- _logger .exception ("Could not normalize raw-data for entry with "
324
- "ID [%s]." , entry_id )
325
- raise
326
-
327
- return entry
329
+ return NormalEntry ('direct_read' , response )
328
330
329
331
@_marshall
330
332
def list_files (self , query_contains_string = None , query_is_string = None ,
@@ -375,7 +377,9 @@ def list_files(self, query_contains_string=None, query_is_string=None,
375
377
376
378
result = client .files ().list (q = query , pageToken = page_token ).\
377
379
execute ()
378
-
380
+
381
+ self .__assert_response_kind (result , 'drive#fileList' )
382
+
379
383
_logger .debug ("(%d) entries were presented for page-number "
380
384
"(%d)." , len (result [u'items' ]), page_num )
381
385
@@ -595,13 +599,14 @@ def __insert_entry(self, is_file, filename, parents, mime_type,
595
599
596
600
request = client .files ().insert (** args )
597
601
598
- result = self .__finish_upload (
602
+ response = self .__finish_upload (
599
603
filename ,
600
604
request ,
601
605
data_filepath is not None )
602
606
603
- normalized_entry = NormalEntry ('insert_entry' , result )
604
-
607
+ self .__assert_response_kind (response , 'drive#file' )
608
+
609
+ normalized_entry = NormalEntry ('insert_entry' , response )
605
610
_logger .info ("New entry created with ID [%s]." , normalized_entry .id )
606
611
607
612
return normalized_entry
@@ -624,11 +629,12 @@ def truncate(self, normalized_entry):
624
629
'media_body' : file_ ,
625
630
}
626
631
627
- result = client .files ().update (** args ).execute ()
632
+ response = client .files ().update (** args ).execute ()
633
+ self .__assert_response_kind (response , 'drive#file' )
628
634
629
635
_logger .debug ("Truncate complete: [%s]" , normalized_entry .id )
630
636
631
- return result
637
+ return response
632
638
633
639
@_marshall
634
640
def update_entry (self , normalized_entry , filename = None , data_filepath = None ,
0 commit comments