gpt4 book ai didi

python - 谷歌 API 客户端(Python): is it possible to use BatchHttpRequest with ETag caching

转载 作者:太空狗 更新时间:2023-10-30 02:57:14 25 4
gpt4 key购买 nike

我使用的是 YouTube 数据 API v3。

是否有可能制作一个大的BatchHttpRequest(例如,参见 here )并在 httplib2 级别使用 ETag 进行本地缓存(例如,参见 here )?

ETags 对于单个查询工作得很好,我不知道它们是否对批量请求也有用。

最佳答案

长话短说:

  • BatchHttpRequest 不能与缓存一起使用

在这里:

先看看BatchHttpRequest的初始化方式:

from apiclient.http import BatchHttpRequest

def list_animals(request_id, response, exception):
if exception is not None:
# Do something with the exception
pass
else:
# Do something with the response
pass

def list_farmers(request_id, response):
"""Do something with the farmers list response."""
pass

service = build('farm', 'v2')

batch = service.new_batch_http_request()

batch.add(service.animals().list(), callback=list_animals)
batch.add(service.farmers().list(), callback=list_farmers)


batch.execute(http=http)

其次让我们看看如何使用 ETag:

from google.appengine.api import memcache
http = httplib2.Http(cache=memcache)

现在让我们分析一下:

观察 BatchHttpRequest 示例的最后一行:batch.execute(http=http),现在检查 source code对于执行,它调用 _refresh_and_apply_credentials,它应用我们传递给它的 http 对象。

def _refresh_and_apply_credentials(self, request, http):
"""Refresh the credentials and apply to the request.
Args:
request: HttpRequest, the request.
http: httplib2.Http, the global http object for the batch.
"""
# For the credentials to refresh, but only once per refresh_token
# If there is no http per the request then refresh the http passed in
# via execute()

这意味着,接受 http 的执行调用可以传递给您创建的 ETag http:

http = httplib2.Http(cache=memcache)
# This would mean we would get the ETags cached http
batch.execute(http=http)

更新 1:

也可以尝试使用自定义对象:

from googleapiclient.discovery_cache import DISCOVERY_DOC_MAX_AGE
from googleapiclient.discovery_cache.base import Cache
from googleapiclient.discovery_cache.file_cache import Cache as FileCache

custCache = FileCache(max_age=DISCOVERY_DOC_MAX_AGE)
http = httplib2.Http(cache=custCache)
# This would mean we would get the ETags cached http
batch.execute(http=http)

因为,这只是对 http2 库中注释的预感:

"""If 'cache' is a string then it is used as a directory name for
a disk cache. Otherwise it must be an object that supports the
same interface as FileCache.

结论更新 2:

再次验证 google-api-python 源代码后,我看到,BatchHttpRequest 已修复为 'POST' 请求,并且内容类型为 多部分/混合;.. - source code .

提供有关以下事实的线索,BatchHttpRequest 可用于POST 数据,然后在后面进行处理。

现在,牢记这一点,观察什么 httplib2 request 方法使用:_updateCache 仅当满足以下条件时:

  1. 请求在 ["GET", "HEAD"]response.status == 303 或者是 redirect request<
  2. ElSE -- response.status in [200, 203] and method in ["GET", "HEAD"]
  3. 或 -- if response.status == 304 and method == "GET"

这意味着,BatchHttpRequest 不能与缓存一起使用。

关于python - 谷歌 API 客户端(Python): is it possible to use BatchHttpRequest with ETag caching,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37797374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com