Google API客户端(Python):是否可以将BatchHttpRequest与ETag缓存一起使用
作者:互联网
我正在使用YouTube数据API v3.
是否可以制作一个大的BatchHttpRequest(例如,参见here),并在httplib2级别使用ETag进行本地缓存(例如,参见here)?
ETag适用于单个查询,我不明白它们是否也适用于批处理请求.
解决方法:
TL; DR:
> 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 lib中评论的预感:
"""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’请求修复,并且内容类型为multipart / mixed; .. – source code
.
给出一个事实的线索,BatchHttpRequest对POST数据很有用,然后将数据处理掉.
现在,记住这一点,观察httplib2
请求方法使用的内容:_updateCache仅在满足以下条件时:
>请求在[“GET”,“HEAD”]或response.status == 303或是重定向请求
> ElSE – [200,203]中的response.status和[“GET”,“HEAD”]中的方法
> OR – 如果response.status == 304和method ==“GET”
这意味着,BatchHttpRequest不能与缓存一起使用.
标签:python,youtube-data-api,httplib2,google-api-client,youtube-data-api-v3 来源: https://codeday.me/bug/20190628/1311118.html