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)
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的execute调用可以通过您创建的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.
TL;DR:
在这里:
首先让我们看看初始化
BatchHttpRequest
:第二步让我们看看
^{pr2}$ETags
是如何使用的:现在让我们分析:
观察BatchHttpRequest示例的最后一行:
batch.execute(http=http)
,现在检查source code是否执行,它调用_refresh_and_apply_credentials
,它应用我们传递给它的http对象。在也就是说,接受http的execute调用可以通过您创建的ETag http来传递:
更新1:
也可以尝试使用自定义对象:
因为,这只是对http2 lib中评论的一种预感:
结论更新2:
在再次验证googleapi python源代码之后,我发现,} 。在
BatchHttpRequest
是用'POST'
请求修复的,内容类型是multipart/mixed;..
-^{给出了一个线索,即BatchHttpRequest对于
POST
数据是有用的,而这些数据随后被处理。在现在,记住这一点,观察^{}
request
方法使用的是:_updateCache
仅当满足以下条件时:["GET", "HEAD"]
或{redirect request
response.status in [200, 203] and method in ["GET", "HEAD"]
if response.status == 304 and method == "GET"
这意味着,
BatchHttpRequest
不能与缓存一起使用。相关问题 更多 >
编程相关推荐