Get Gmail Theme Threads (Reasonably)

I am trying to use the Gmail API to retrieve all thread entities in a gmail account.

This is easy with threads.list, but basically gets the identifier of the stream, not the object.

The only way I found is to use threads.list, then for each thread, calling threads.getand retrieving the object from the headers in the payload metadata.

Obviously this causes a lot of API calls, i.e. 101 calls if there are 100 threads.

Is there a better way?

Here is the code I'm using now:

var getIndivThread = function(threads) {
  threads.threads.forEach(function(e) {
    indivThreadRequst.id = e.id, 
    gmail.api.users.threads.get(indivThreadRequst).execute(showThread);
  });
};
var indivThreadRequst= {
    format: 'metadata',
    metadataHeaders:['subject'], 
    userId: myUserId, 
    maxResults:1};
var showThread = function(thread) {
  console.log(thread.messages[0].payload.headers[0].value);
};
gmail.api.users.threads.list({userId:myUserId}).execute(getIndivThread);
+4
source share
1 answer

, API. , , :

  • API .

  • , . , 5 .

  • , , , , . , 1 , . - ; , , .

# 2 5, , , 5 :

var CONCURRENCY_LIMIT = 5;

function getThread(threadId, done) {
  threadRequest.id = e.id;
  gmail.api.users.threads.get(threadRequest).execute(function(thread) {
    showThread();
    done();
  });
}

gmail.api.users.threads.list({userId:myUserId}).execute(function(threads) { 
  function fetchNextThread() {
    var nextThread = threads.shift();
    nextThread.id && getThread(nextThread.id, fetchNextThread);
  }

  for (var i = 0; i < CONCURRENCY_LIMIT; i++) {
    fetchNextThread();
  }
});
+3

All Articles