ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Google API客户端(Python):是否可以将BatchHttpRequest与ETag缓存一起使用

2019-06-28 02:42:31  阅读:282  来源: 互联网

标签:python youtube-data-api httplib2 google-api-client youtube-data-api-v3


我正在使用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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有