本文整理汇总了Python中apiclient.http.BatchHttpRequest方法的典型用法代码示例。如果您正苦于以下问题:Python http.BatchHttpRequest方法的具体用法?Python http.BatchHttpRequest怎么用?Python http.BatchHttpRequest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类apiclient.http
的用法示例。
在下文中一共展示了http.BatchHttpRequest方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from apiclient import http [as 别名]
# 或者: from apiclient.http import BatchHttpRequest [as 别名]
def __init__(self, callback=None, batch_uri=None):
"""Constructor for a BatchHttpRequest.
Args:
callback: callable, A callback to be called for each response, of the
form callback(id, response, exception). The first parameter is the
request id, and the second is the deserialized response object. The
third is an apiclient.errors.HttpError exception object if an HTTP error
occurred while processing the request, or None if no error occurred.
batch_uri: string, URI to send batch requests to.
"""
if batch_uri is None:
batch_uri = 'https://www.googleapis.com/batch'
self._batch_uri = batch_uri
# Global callback to be called for each individual response in the batch.
self._callback = callback
# A map from id to request.
self._requests = {}
# A map from id to callback.
self._callbacks = {}
# List of request ids, in the order in which they were added.
self._order = []
# The last auto generated id.
self._last_auto_id = 0
# Unique ID on which to base the Content-ID headers.
self._base_id = None
# A map from request id to (httplib2.Response, content) response pairs
self._responses = {}
# A map of id(Credentials) that have been refreshed.
self._refreshed_credentials = {}
示例2: execute
# 需要导入模块: from apiclient import http [as 别名]
# 或者: from apiclient.http import BatchHttpRequest [as 别名]
def execute(self):
"""Executes requests in the queue.
Removes items from the queue, and adds them to a BatchHttpRequest object.
Only removes up to set quota. and then calls the BatchHttPRequest object's
execute method.
"""
batch = BatchHttpRequest(callback=self.call_back)
for _ in range(self.quota):
if self.queue.qsize() == 0:
break
request, request_id = self.queue.get()
batch.add(request, request_id=request_id)
batch.execute(http=httplib2.Http())
示例3: add_users
# 需要导入模块: from apiclient import http [as 别名]
# 或者: from apiclient.http import BatchHttpRequest [as 别名]
def add_users(users, permissions):
"""Adds users to every view (profile) with the given permissions.
Args:
users: A list of user email addresses.
permissions: A list of user permissions.
Note: this code assumes you have MANAGE_USERS level permissions
to each profile and an authorized Google Analytics service object.
"""
# Get the a full set of account summaries.
account_summaries = analytics.management().accountSummaries().list().execute()
# Loop through each account.
for account in account_summaries.get('items', []):
account_id = account.get('id')
# Loop through each user.
for user in users:
# Create the BatchHttpRequest object.
batch = BatchHttpRequest(callback=call_back)
# Loop through each property.
for property_summary in account.get('webProperties', []):
property_id = property_summary.get('id')
# Loop through each view (profile).
for view in property_summary.get('profiles', []):
view_id = view.get('id')
# Construct the Profile User Link.
link = analytics.management().profileUserLinks().insert(
accountId=account_id,
webPropertyId=property_id,
profileId=view_id,
body={
'permissions': {
'local': permissions
},
'userRef': {
'email': user
}
}
)
batch.add(link)
# Execute the batch request for each user.
batch.execute()