1

I'm actually working with the GMail API for Python and I successfully retrieve all the messages from all the threads for a given label and receiver and it looks like this :

credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('gmail', 'v1', http=http)

response = service.users().threads().list(userId="me",
                                          labelIds=["SENT"],
                                          q="to:"+to_address).execute()

threads = []
if 'threads' in response:
    threads.extend(response['threads'])

for thread in threads:

    thread = service.users().threads().get(userId="me", id=thread['id']).execute()

    messages = thread['messages']
    for message in messages:
        message = service.users().messages().get(userId="me", id=message['id'], format="raw").execute()

        # [...] Work on data, code detail not shown here

As you can see, I retrieve every thread from sent email to a specific address and I make a lot of api accesses to retrieve the data of each messages.
I feel like there is a better way with optimized API access because it's actually quite slow.

For approximatly 15 threads and 40 emails in total, it takes around 15 seconds, there must be a better way.

My main problem here are snippets. When executing this line:

response = service.users().threads().list(userId="me",
                                      labelIds=["SENT"],
                                      q="to:"+to_address).execute()

I don't get the messages directly but snippets instead, so I'm forced to do this line:

thread = service.users().threads().get(userId="me", id=thread['id']).execute()

for each thread, which is my main source of slow code.

Qrom
  • 487
  • 5
  • 20
  • 1
    You have to get each thread, sadly. You could use [**batch requests**](http://stackoverflow.com/questions/24562981/bulk-fetching-emails-in-the-new-gmail-api) to bring it down from 101 total requests to 2. – Tholle Apr 25 '17 at 14:01

0 Answers0