Batch fetching messages performance

前端 未结 4 1625
你的背包
你的背包 2021-02-10 09:27

I need to get the last 100 messages in the INBOX (headers only). For that I\'m currently using the IMAP extension to search and then fetch the messages. This is done with two re

4条回答
  •  眼角桃花
    2021-02-10 10:04

    here is the python version, using the official google api client. Note that I did not use the callback here, because I need to handle the responses in a synchronous way.

    from apiclient.http import BatchHttpRequest
    import json
    
    batch = BatchHttpRequest()
    
    #assume we got messages from Gmail query API
    for message in messages:
        batch.add(service.users().messages().get(userId='me', id=message['id'],
                                                 format='raw'))
    for request_id in batch._order:
        resp, content = batch._responses[request_id]
        message = json.loads(content)
        #handle your message here, like a regular email object
    

提交回复
热议问题