2

I am currently writing a small python script that works with the Gmail-API. I am trying to send a batch request to the gmail server. As the native method of gmail for sending batch request got depreciated in August 2020 I have to construct one myself. It has to be of the form 'multipart/mixed'.

According to the documentation it has to look like this (https://developers.google.com/gmail/api/guides/batch): Example batch request according to the gmail api documentation

I tried to use the python request library, but it seems it does only support requests of the form 'multipart/form'. But I am not sure.

# A list with all the message-id's.
messages = tqdm(gmail.list_messages('me'))

gmailUrl = "https://gmail.googleapis.com/batch/gmail/v1"

request_header = {"Host": "www.googleapis.com", "Content-Type": "multipart/mixed", "boundary"="bound"}

request_body = ""

# I want to bundle 80 GET requests in one batch.
# I don't know how to proceed from here.
for n in range(0,80):


response = req.post(url=gmailUrl, auth=creds, headers=request_header, files=request_body)

print(response)

So my question is pretty straightforward:

How can I send a http request with python to the Gmail-API with a 'multipart/mixed'-form.

Thanks in advance!

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Mxngls
  • 437
  • 1
  • 5
  • 16
  • Why are you batching requests? You understand that all you will be doing is saving your self some HTTP traffic right? in exchange for the frustration of trying to get batching to work with a Google API. – Linda Lawton - DaImTo May 23 '21 at 09:44
  • 1
    I am working with the api for other functions. But to send out that many http requests (and receive them again) seems to take longer than necessary. – Mxngls May 23 '21 at 09:47
  • I wish you luck this might help but it might not as batching endpoint has changed since this answer was posted but it was the only thing I could find https://stackoverflow.com/q/27650927/1841839. Its a free API you get what you pay for. FYI batching isn't going to speed anything up or improve your quota at all its the same number of requests that have to be processed. – Linda Lawton - DaImTo May 23 '21 at 09:49
  • Thanks. I am searching for hours now (and found quite a lot of threads where you commented as well). Basically the problem as it seems is just that I do not know how to correctly send the request. – Mxngls May 23 '21 at 09:50
  • I have been working with the Google APIs for ten years, I gave up on batching about seven years ago. I found it not to be worth the frustration it caused trying to get it to work. I am not a Python dev so I cant help much. Try to get batching working with a get request first then try with your send mail. I do think you may have issues with send as it may look like spaming to Google make sure you are testing with a dummy account incase you get locked out. – Linda Lawton - DaImTo May 23 '21 at 09:55
  • I managed to get batching to work and understand the gmail API way more than a few days before. Contrary to what you said batching is improving the performance quite a lot, especially when combined with just requesting a partial response. If you would like me to explain I am glad to help! – Mxngls May 26 '21 at 18:23

1 Answers1

4

You can add each request to the batch, then execute the batch. For example:

list_response = service.users().threads().list(userId='me').execute()
threads = list_response['threads']
bt = service.new_batch_http_request()
for thread in threads:
    bt.add(service.users().threads().get(userId='me', id=thread['id']))
bt.execute()

The bt object will now have fields _requests and _responses, which you can now access - this will require some string parsing (I used ast).

fjarlq
  • 2,419
  • 1
  • 16
  • 10
GregarityNow
  • 707
  • 1
  • 11
  • 25