Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add maxResults param #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions simplegmail/gmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class Gmail(object):
# https://developers.google.com/gmail/api/quickstart/python
# Make sure the client secret file is in the root directory of your app.

# Max query results, per
# https://developers.google.com/gmail/api/reference/rest/v1/users.messages/list#query-parameters
MAX_RESULTS = 500

def __init__(
self,
client_secret_file: str = 'client_secret.json',
Expand All @@ -66,6 +70,7 @@ def __init__(
) -> None:
self.client_secret_file = client_secret_file
self.creds_file = creds_file
self._maxResults = 0

try:
# The file gmail_token.json stores the user's access and refresh
Expand Down Expand Up @@ -509,28 +514,40 @@ def get_messages(
]

try:
max_results = self._maxResults
if max_results == 0:
max_results = self.MAX_RESULTS
response = self.service.users().messages().list(
userId=user_id,
q=query,
labelIds=labels_ids,
includeSpamTrash=include_spam_trash
includeSpamTrash=include_spam_trash,
maxResults=max_results
).execute()

message_refs = []
if 'messages' in response: # ensure request was successful
message_refs.extend(response['messages'])

while 'nextPageToken' in response:
if self._maxResults != 0:
remaining = self._maxResults - len(message_refs)
if remaining <= 0:
break
max_results = remaining

page_token = response['nextPageToken']
response = self.service.users().messages().list(
userId=user_id,
q=query,
labelIds=labels_ids,
includeSpamTrash=include_spam_trash,
pageToken=page_token
pageToken=page_token,
maxResults=max_results
).execute()

message_refs.extend(response['messages'])
if 'messages' in response: # ensure request was successful
message_refs.extend(response['messages'])

return self._get_messages_from_refs(user_id, message_refs,
attachments)
Expand Down Expand Up @@ -988,3 +1005,11 @@ def _get_alias_info(

res = req.execute()
return res

@property
def maxResults(self):
return self._maxResults

@maxResults.setter
def maxResults(self, value):
self._maxResults = int(value)