Skip to content

Commit 5e11655

Browse files
committed
We now assert that what we receive from GD is the right 'kind'."
1 parent c21c070 commit 5e11655

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

gdrivefs/gdtool/drive.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,20 @@ class _GdriveManager(object):
178178
def __init__(self):
179179
self.__auth = GdriveAuth()
180180

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+
181187
@_marshall
182188
def get_about_info(self):
183189
"""Return the 'about' information for the drive."""
184190

185191
client = self.__auth.get_client()
186192
response = client.about().get().execute()
187-
193+
self.__assert_response_kind(response, 'drive#about')
194+
188195
return response
189196

190197
@_marshall
@@ -205,6 +212,9 @@ def list_changes(self, start_change_id=None, page_token=None):
205212
response = client.changes().list(
206213
pageToken=page_token,
207214
startChangeId=start_change_id).execute()
215+
216+
self.__assert_response_kind(response, 'drive#changeList')
217+
208218
# TODO(dustin): Debugging. Sometimes largestChangeId is missing.
209219
import pprint
210220
pprint.pprint(response)
@@ -251,6 +261,7 @@ def get_parents_containing_id(self, child_id, max_results=None):
251261
_logger.info("Listing entries over child with ID [%s].", child_id)
252262

253263
response = client.parents().list(fileId=child_id).execute()
264+
self.__assert_response_kind(response, 'drive#parentList')
254265

255266
return [ entry[u'id'] for entry in response[u'items'] ]
256267

@@ -287,6 +298,8 @@ def get_children_under_parent_id(self,
287298
folderId=parent_id,
288299
maxResults=max_results).execute()
289300

301+
self.__assert_response_kind(response, 'drive#childList')
302+
290303
return [ entry[u'id'] for entry in response[u'items'] ]
291304

292305
@_marshall
@@ -310,21 +323,10 @@ def get_entries(self, entry_ids):
310323
def get_entry(self, entry_id):
311324
client = self.__auth.get_client()
312325

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')
319328

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)
328330

329331
@_marshall
330332
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,
375377

376378
result = client.files().list(q=query, pageToken=page_token).\
377379
execute()
378-
380+
381+
self.__assert_response_kind(result, 'drive#fileList')
382+
379383
_logger.debug("(%d) entries were presented for page-number "
380384
"(%d).", len(result[u'items']), page_num)
381385

@@ -595,13 +599,14 @@ def __insert_entry(self, is_file, filename, parents, mime_type,
595599

596600
request = client.files().insert(**args)
597601

598-
result = self.__finish_upload(
602+
response = self.__finish_upload(
599603
filename,
600604
request,
601605
data_filepath is not None)
602606

603-
normalized_entry = NormalEntry('insert_entry', result)
604-
607+
self.__assert_response_kind(response, 'drive#file')
608+
609+
normalized_entry = NormalEntry('insert_entry', response)
605610
_logger.info("New entry created with ID [%s].", normalized_entry.id)
606611

607612
return normalized_entry
@@ -624,11 +629,12 @@ def truncate(self, normalized_entry):
624629
'media_body': file_,
625630
}
626631

627-
result = client.files().update(**args).execute()
632+
response = client.files().update(**args).execute()
633+
self.__assert_response_kind(response, 'drive#file')
628634

629635
_logger.debug("Truncate complete: [%s]", normalized_entry.id)
630636

631-
return result
637+
return response
632638

633639
@_marshall
634640
def update_entry(self, normalized_entry, filename=None, data_filepath=None,

0 commit comments

Comments
 (0)