批量提取邮件性能 [英] Batch fetching messages performance

查看:49
本文介绍了批量提取邮件性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在INBOX中获取最后100条消息(仅标题).为此,我目前正在使用IMAP扩展名进行搜索,然后获取消息.这是通过两个请求(SEARCH,然后是UID FETCH)完成的.
什么是在一个请求中提取多封邮件的Gmail API?
我所能找到的只是一个批处理API,它看起来比较麻烦(组成一长串用纯HTTP代码包装的messages:get请求).

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 requests (SEARCH and then UID FETCH).
What's the Gmail API equivalent to fetching multiple messages in one request?
All I could find is a batch API, which seems way more cumbersome (composing a long list of messages:get requests wrapped in plain HTTP code).

推荐答案

在Gmail API中,与在IMAP中几乎相同.有两个请求:第一个是messages.list,用于获取消息ID.然后(批处理)message.get检索您想要的消息.取决于您使用的是哪种语言,客户端库可能有助于批量请求的构建.

It's pretty much the same in the Gmail API as in IMAP. Two requests: first is messages.list to get the message ids. Then a (batched) message.get to retrieve the ones you want. Depending on what language you're using the client libraries may help with the batch request construction.

批处理请求是单个标准HTTP请求,其中包含使用多部分/混合内容类型的多个Google Cloud Storage JSON API调用.在该主要HTTP请求中,每个部分都包含一个嵌套的HTTP请求.

A batch request is a single standard HTTP request containing multiple Google Cloud Storage JSON API calls, using the multipart/mixed content type. Within that main HTTP request, each of the parts contains a nested HTTP request.

来自: https://developers.google.com/storage/docs/json_api/v1/how-tos/batch

这真的并不难,即使没有python客户端库(仅使用httplib和mimelib),我也花了一个小时才用python弄清楚.

It's really not that hard, took me about an hour to figure it out in python even without the python client libraries (just using httplib and mimelib).

这是执行此操作的部分代码段,再次使用直接python.希望可以很清楚地表明涉及的不是太多:

Here's a partial code snippet of doing it, again with direct python. Hopefully it makes it clear that's there's not too much involved:

msg_ids = [msg['id'] for msg in body['messages']]
headers['Content-Type'] = 'multipart/mixed; boundary=%s' % self.BOUNDARY

post_body = []
for msg_id in msg_ids:
  post_body.append(
    "--%s\n"
    "Content-Type: application/http\n\n"
    "GET /gmail/v1/users/me/messages/%s?format=raw\n"
    % (self.BOUNDARY, msg_id))
post_body.append("--%s--\n" % self.BOUNDARY)
post = '\n'.join(post_body)
(headers, body) = _conn.request(
    SERVER_URL + '/batch',
    method='POST', body=post, headers=headers)

这篇关于批量提取邮件性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆