Skip to content

Commit

Permalink
(feat) paginate requests
Browse files Browse the repository at this point in the history
  • Loading branch information
azai91 committed Dec 17, 2015
1 parent cb2f856 commit 9230406
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 8 additions & 2 deletions src/drive_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 9230406

Please sign in to comment.