title: Python Gmail API : How to batch download of email attachments and emails?
body: <p>I have the below code, which downloads a Gmail email and its attachments. It returns its attachments.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>    def gmailAPIDownloadAttachments(self, messageID, userID="me"):
          try:
              service = self.gmailAPIService
              self.GLogger.info("Attempting to download attachments from messageID (" +str(messageID)+ ")")
              message = self.gmailAPIGetFullMessage(messageID, userID=userID)
              if message is False:
                  self.GLogger.error("Failed to extract message (" +str(messageID)+ ") for downloading attachments")
                  return False
              attachmentList = list()
              payload = message['payload']
              if 'parts' in payload:
                  parts = payload['parts']
                  for part in parts:
                      if part['filename']:
                          if 'data' in part['body']:
                              data = part['body']['data']
                          else:
                              att_id = part['body']['attachmentId']
                              att = service.users().messages().attachments().get(userId=userID, messageId=messageID, id=att_id).execute()
                              data = att['data']
                          file_data = base64.urlsafe_b64decode(data.encode('UTF-8'))
                          filename = part['filename']
                          extSearch = filename.find('.')
                          if extSearch == -1:
                              ext = ""
                              partFileName = filename[0:extSearch]
                          else:
                              ext = filename[extSearch+1:]
                              partFileName = filename[0:extSearch]
  
                          theAttachment = Attachment(filename,partFileName, ext, file_data)
                          attachmentList.append(theAttachment)
  
              self.GLogger.info("Successfully downloaded attachments from messageID (" +str(messageID)+ ")")
              return(attachmentList)
          except:
              self.GLogger.error("Encountered an error while attempting to download email attacments from messageID (" +str(messageID)+ ")")
              tb = traceback.format_exc()
              self.GLogger.exception(tb)
              return False</code></pre>
  </div>
  </div>
  </p>
  <p>I understand how to convert fetching messages into batching. For example, this is how one could batch-fetch messages:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>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'))
  batch.execute()
  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</code></pre>
  </div>
  </div>
  </p>
  <p>However, the attachments aspect seem to have logic and other possible fetches such as in this part:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>att_id = part['body']['attachmentId']
  att = service.users().messages().attachments().get(userId=userID, messageId=messageID, id=att_id).execute()
  data = att['data']</code></pre>
  </div>
  </div>
  </p>
  <p>How can I effectively batch both fetching the message and its attachments? I would like to be able to quickly fetch many emails at once.</p>