26

I'm using the python version of the newly released Gmail API by Google.

The following call returns just a list of message ids:

service.users().messages().list(userId = 'me').execute()

But then I just have a list of message ids and need to iterate over them and fetch them one-by-one.

Is there a way to get the whole message content for a list of ids, in a single call ? (Similar to how it's done in the Google Calendar API) ?

And if not supported yet, is this something that Google would like to consider adding in the API ?

Update

Here is the solution that worked for me:
batch = BatchHttpRequest() for msg_id in message_ids: batch.add(service.users().messages().get(userId = 'me', id = msg_id['id']), callback = mycallbackfunc) batch.execute()

Baruch Oxman
  • 1,616
  • 14
  • 24

2 Answers2

20

Here is an example of batch request in Java where I get all the threads using threads ids. This can be easily adapted for your need.

BatchRequest b = service.batch();
//callback function. (Can also define different callbacks for each request, as required)
JsonBatchCallback<Thread> bc = new JsonBatchCallback<Thread>() {

    @Override
    public void onSuccess(Thread t, HttpHeaders responseHeaders)
            throws IOException {
        System.out.println(t.getMessages().get(0).getPayload().getBody().getData());
    }

    @Override
    public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders)
            throws IOException {

    }
};

// queuing requests on the batch requests
for (Thread thread : threads) {
    service.users().threads().get("me", threads.getId()).queue(b, bc);
}


b.execute();
gitter
  • 1,706
  • 1
  • 20
  • 32
14

Here is the solution that worked for me:

batch = BatchHttpRequest()
for msg_id in message_ids:
    batch.add(service.users().messages().get(userId='me', id=msg_id['id']), callback=mycallbackfunc)
batch.execute()
Rafa Viotti
  • 9,998
  • 4
  • 42
  • 62
Baruch Oxman
  • 1,616
  • 14
  • 24
  • can you please let me know how can I render whole response to HTML from the batch request.... I am dealing with gmail api and I am getting response in callback function but I am not able to render it.... I am bit confused in this – Rajat Modi Apr 28 '15 at 08:38
  • Rajat, as your question is not directly related to this one, I would recommend starting a new discussion on your question, to get the best attention and most replies. – Baruch Oxman Apr 28 '15 at 20:48
  • I already did it here is the URL http://stackoverflow.com/questions/29916079/render-response-in-html-of-batch-request-in-gmail-api-python thanks – Rajat Modi Apr 29 '15 at 05:13
  • I could not find about it anywhere.. but just to sum up, the callback function will receive 3 params.. From what I have seen on debug, the first one is the message index and the second one is the message itself. Not sure about the third one – 37dev Jan 07 '20 at 17:26