Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gmail API batch get support?

I am doing WEB HTTP calls using Gmail API. Is there a way to batch get message content?

It seems that messages.list only returns messageIds, and messages.get only support single message query.

LIST API: https://www.googleapis.com/gmail/v1/users/userId/messages GET API: https://www.googleapis.com/gmail/v1/users/userId/messages/id

Help me guys~ Thank you!

like image 672
Jun Zhou Avatar asked Feb 20 '15 19:02

Jun Zhou


2 Answers

You can definitely do batched messages.get(), quite a few questions covering it already: https://stackoverflow.com/search?q=%5Bgmail-api%5D+batch

like image 176
Eric D Avatar answered Sep 25 '22 07:09

Eric D


The gmail API returns only messageIds first to prevent heavy load. With those Ids you can get individual full messages or send a batch request for getting a bunch of messages.

After getting the partialMessages(message ids) use this :

List<Messages> fullMessages = getFullyQualifiedMessages(partialMessages);

private List<Message> getFullyQualifiedMessages(List<Message> partialMessages) {
    try {

        final JsonBatchCallback<Message> callback = new JsonBatchCallback<Message>() {
            public void onSuccess(Message message, HttpHeaders responseHeaders) {
                fullyQualifiedMessageList.add(message);
            }

            public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {
                // do what you want if error occurs
            }
        };

        BatchRequest batch = mService.batch();
        for (Message message : partialMessages) {
            mService.users().messages().get("me", message.getId()).setFormat("full").queue(batch, callback);
        }
        batch.execute();


    } catch (IOException e) {
        e.printStackTrace();
    }

    Log.d(TAG, "Message" + fullyQualifiedMessageList.size());


    return fullyQualifiedMessageList;
}
like image 26
Ajay Gowtam Avatar answered Sep 24 '22 07:09

Ajay Gowtam