""" Utility functions which should have been part of the Google API client. """ def list_all(list_cb, *, page_size=500, items_key='items', **kwargs): """ Simple wrapper for Google Client SDK list()-style callables. Repeatedly fetches pages of results merging all the responses together. Returns the merged "items" arrays from the responses. The key used to get the "items" array from the response may be overridden via the items_key argument. """ # Loop while we wait for nextPageToken to be "none" page_token = None resources = [] while True: list_response = list_cb(pageToken=page_token, maxResults=page_size, **kwargs).execute() resources.extend(list_response.get(items_key, [])) # Get the token for the next page page_token = list_response.get('nextPageToken') if page_token is None: break return resources