如何使用 Gmail 发送的批量请求 API

How to use batch requests for Gmail Send API

我目前使用 Gmail 一次发送多封电子邮件 API。我按顺序执行此操作:

  1. 通过 Gmail 发送电子邮件API
  2. 等待 Gmail 的响应API。
  3. 收到响应后,使用 Gmail 返回的 ThreadID 更新记录API。
  4. 对其他电子邮件重复步骤 1-3。

我正在阅读有关批处理您的 sendEmail API 请求的信息,以便我们调用 API 并处理响应。 虽然我可以批量发送我的所有请求,

我不确定如何处理回复。如果我在批处理请求中有 30 个发送电子邮件请求,当收到批处理的响应时,我如何确定哪个响应是针对哪个电子邮件?

这是我的实现

BatchRequest batch  = gmailService.batch();
gmailService.users().messages().send("me", message).queue(batch, callback);
batch.execute();

    final List<Message> messages = new ArrayList<Message>();
        JsonBatchCallback<Message> callback = new JsonBatchCallback<Message>() {
            public void onSuccess(Message message, HttpHeaders  responseHeaders) {
                System.out.println("MessageThreadID:"+ message.getThreadId());
                System.out.println("MessageID:"+ message.getId());
                synchronized (messages) {
                    messages.add(message);  
                }
            }

            @Override
            public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders)
                    throws IOException {
            }
        };

I am not sure about how to handle Responses. If i have 30 Send Email Requests in the batch request, when the response is received for the batch, how do i figure out which response is for which Email?

根据这个 Batch Request Response 文档:

Response to a batch request

The server's response is a single standard HTTP response with a multipart/mixed content type; each part is the response to one of the requests in the batched request, in the same order as the requests.

Like the parts in the request, each response part contains a complete HTTP response, including a status code, headers, and body. And like the parts in the request, each response part is preceded by a Content-Type header that marks the beginning of the part.

您可以获得完整的 HTTP 响应、它的状态代码、它的 headers 和它的 body(按照您发出请求的顺序)。所以响应 1 是针对第一个请求,响应 2 是针对第二个请求,依此类推。这样,您可能会计算出哪个电子邮件的响应。