From 92304063f0778096e63615a7a28304ad98a337bc Mon Sep 17 00:00:00 2001 From: Alex Zai Date: Wed, 16 Dec 2015 22:22:19 -0800 Subject: [PATCH] (feat) paginate requests --- src/config.py | 2 +- src/drive_api.py | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/config.py b/src/config.py index 3164168..eabb04e 100644 --- a/src/config.py +++ b/src/config.py @@ -9,7 +9,7 @@ AUTH_URL = 'https://accounts.google.com/o/oauth2/auth?scope=%(scope)s&redirect_uri=%(redirect_uri)s&response_type=code&client_id=%(client_id)s&access_type=offline&approval_prompt=force' % {'scope' : SCOPE, 'redirect_uri' : REDIRECT_URI, 'client_id' : CLIENT_ID} TOKEN_URL = 'https://www.googleapis.com/oauth2/v3/token' -FILES_URL = 'https://www.googleapis.com/drive/v2/files?maxResults=1000&q=mimeType%3D%22application%2Fvnd.google-apps.document%22+or+mimeType%3D%22application%2Fvnd.google-apps.spreadsheet%22+or+mimeType%3D%22application%2Fvnd.google-apps.presentation%22&fields=items' +FILES_URL = 'https://www.googleapis.com/drive/v2/files?maxResults=1000&q=mimeType%3D%22application%2Fvnd.google-apps.document%22+or+mimeType%3D%22application%2Fvnd.google-apps.spreadsheet%22+or+mimeType%3D%22application%2Fvnd.google-apps.presentation%22&fields=items,nextPageToken' CACHE_MAX_AGE = 60*60*24*30 # cache set to 1 month diff --git a/src/drive_api.py b/src/drive_api.py index 145436e..b4d1f66 100644 --- a/src/drive_api.py +++ b/src/drive_api.py @@ -82,8 +82,14 @@ def get_links(cls): # TODO: Log errors to alfred bar if 'error' in response and cls.refresh(): return cls.get_links() - else: - return response['items'] + + items = response['items'] + while 'nextPageToken' in response: + nextPageToken = response['nextPageToken'] + response = requests.get(FILES_URL + '&pageToken=' + nextPageToken, headers=headers).json() + items += response['items'] + + return items @classmethod def refresh_list(cls):