本文整理汇总了Python中googleapiclient.http.BatchHttpRequest.add方法的典型用法代码示例。如果您正苦于以下问题:Python BatchHttpRequest.add方法的具体用法?Python BatchHttpRequest.add怎么用?Python BatchHttpRequest.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类googleapiclient.http.BatchHttpRequest
的用法示例。
在下文中一共展示了BatchHttpRequest.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_label_list_info
# 需要导入模块: from googleapiclient.http import BatchHttpRequest [as 别名]
# 或者: from googleapiclient.http.BatchHttpRequest import add [as 别名]
def get_label_list_info(self, messages_ids):
"""
Batch fetch label info given message_ids
Args:
message_ids (list): of message_ids
Returns:
dict with label info
"""
label_info_dict = {}
# Callback function for every service request
def get_label_info(request_id, response, exception):
if response:
label_info_dict[response['id']] = response
if exception:
# If 404, message no longer exists, otherwise raise error
if exception.resp.status != 404:
raise exception
batch = BatchHttpRequest(callback=get_label_info)
for message_id in messages_ids:
# Temporary add snippet
# TODO: remove snippet
batch.add(self.service.users().messages().get(
userId='me',
id=message_id,
fields='labelIds,id,threadId,snippet'
))
self.execute_service_call(batch)
return label_info_dict
示例2: get_messages
# 需要导入模块: from googleapiclient.http import BatchHttpRequest [as 别名]
# 或者: from googleapiclient.http.BatchHttpRequest import add [as 别名]
def get_messages(contactId):
contact = session['seen'][contactId]
credentials = client.OAuth2Credentials.from_json(session['credentials'])
http = credentials.authorize(httplib2.Http(cache=".cache"))
# Build the Gmail service from discovery
gmail_service = discovery.build('gmail', 'v1', http=http)
def mailscallbackfunc(result, results, moreresults):
# in old Python versions:
# if seen.has_key(marker)
# but in new ones:
if 'UNREAD' in results['labelIds']:
Unread = 'true'
else:
Unread = 'false'
for header in results['payload']['headers']:
if header['name'] == 'Date':
Date = header['value']
if header['name'] == 'From':
From = header['value']
if From == "Anders Damsgaard <[email protected]>": #SKAL ÆNDRES
Sent = True
else:
Sent = False
if header['name'] == 'Subject':
Subject = header['value']
Contact = {
'messageId': results['id'],
'date': Date,
'subject': Subject,
'snippet': results['snippet'],
'unread': Unread,
'sent': Sent
}
session['mails'].append(Contact)
#for msg_id in message_ids['messages']:
# batchContacts.add(gmail_service.users().messages().get(userId='me',
# id=msg_id['id'], format='metadata',
# metadataHeaders=['from', 'date']))
#batchContacts.execute()
query = "\"to:'" + contact + "' AND from:me \" OR from:'" + contact + "'"
message_ids = gmail_service.users().messages().list(userId='me',
maxResults=10, labelIds='INBOX', q=query).execute()
batchMails = BatchHttpRequest(callback=mailscallbackfunc)
for msg_id in message_ids['messages']:
batchMails.add(gmail_service.users().messages().get(userId='me',
id=msg_id['id'], format='metadata',
metadataHeaders=['from', 'date', 'subject']))
batchMails.execute()
response = {'messages': session['mails']}
js = json.dumps(response)
resp = Response(js, status=200, mimetype='application/json')
return resp
示例3: test_http_errors_passed_to_callback
# 需要导入模块: from googleapiclient.http import BatchHttpRequest [as 别名]
# 或者: from googleapiclient.http.BatchHttpRequest import add [as 别名]
def test_http_errors_passed_to_callback(self):
batch = BatchHttpRequest()
callbacks = Callbacks()
cred_1 = MockCredentials('Foo')
cred_2 = MockCredentials('Bar')
http = HttpMockSequence([
({'status': '200',
'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'},
BATCH_RESPONSE_WITH_401),
({'status': '200',
'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'},
BATCH_RESPONSE_WITH_401),
])
creds_http_1 = HttpMockSequence([])
cred_1.authorize(creds_http_1)
creds_http_2 = HttpMockSequence([])
cred_2.authorize(creds_http_2)
self.request1.http = creds_http_1
self.request2.http = creds_http_2
batch.add(self.request1, callback=callbacks.f)
batch.add(self.request2, callback=callbacks.f)
batch.execute(http=http)
self.assertEqual(None, callbacks.responses['1'])
self.assertEqual(401, callbacks.exceptions['1'].resp.status)
self.assertEqual(
'Authorization Required', callbacks.exceptions['1'].resp.reason)
self.assertEqual({u'baz': u'qux'}, callbacks.responses['2'])
self.assertEqual(None, callbacks.exceptions['2'])
示例4: get_message_list_info
# 需要导入模块: from googleapiclient.http import BatchHttpRequest [as 别名]
# 或者: from googleapiclient.http.BatchHttpRequest import add [as 别名]
def get_message_list_info(self, message_ids):
"""
Batch fetch message info given message_ids
Args:
message_ids (list): of message_ids
Returns:
dict with messages info
"""
messages_info = {}
# Callback function for every service request
def get_message_info(request_id, response, exception):
if response:
messages_info[response['id']] = response
if exception:
# If 404, message no longer exists, otherwise raise error
if exception.resp.status != 404:
raise exception
else:
logger.error('404 error: %s' % exception)
# Setup batch
batch = BatchHttpRequest(callback=get_message_info)
for message_id in message_ids:
batch.add(self.service.users().messages().get(userId='me', id=message_id))
self.execute_service_call(batch)
return messages_info
示例5: delete_bucket_content
# 需要导入模块: from googleapiclient.http import BatchHttpRequest [as 别名]
# 或者: from googleapiclient.http.BatchHttpRequest import add [as 别名]
def delete_bucket_content(self, bucket):
'''Delete content of existing bucket.
:param bucket: Name of the bucket in google storage to access
:type bucket: str
:retunrs: Response JSON str
:rtype: json
'''
objects = self.list_bucket_content(bucket)
if objects:
logger.info("Createing batch for objects deletion...")
batch = BatchHttpRequest(callback=self.delete_object_cb)
for o in objects:
logger.debug(
"Adding 'delete request' to batch: %s" % o['name']
)
batch.add(self.service.objects().delete(
object=o['name'], bucket=bucket
))
logger.info("Executing batch...")
resp = batch.execute(http=self.http_auth)
logger.info("Response content from batch: %s" % resp)
return resp
return None
示例6: test_execute_initial_refresh_oauth2
# 需要导入模块: from googleapiclient.http import BatchHttpRequest [as 别名]
# 或者: from googleapiclient.http.BatchHttpRequest import add [as 别名]
def test_execute_initial_refresh_oauth2(self):
batch = BatchHttpRequest()
callbacks = Callbacks()
cred = MockCredentials('Foo')
# Pretend this is a OAuth2Credentials object
cred.access_token = None
http = HttpMockSequence([
({'status': '200',
'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'},
BATCH_SINGLE_RESPONSE),
])
cred.authorize(http)
batch.add(self.request1, callback=callbacks.f)
batch.execute(http=http)
self.assertEqual({'foo': 42}, callbacks.responses['1'])
self.assertIsNone(callbacks.exceptions['1'])
self.assertEqual(1, cred._refreshed)
self.assertEqual(1, cred._authorized)
self.assertEqual(1, cred._applied)
示例7: test_add_fail_for_resumable
# 需要导入模块: from googleapiclient.http import BatchHttpRequest [as 别名]
# 或者: from googleapiclient.http.BatchHttpRequest import add [as 别名]
def test_add_fail_for_resumable(self):
batch = BatchHttpRequest()
upload = MediaFileUpload(
datafile('small.png'), chunksize=500, resumable=True)
self.request1.resumable = upload
with self.assertRaises(BatchError) as batch_error:
batch.add(self.request1, request_id='1')
str(batch_error.exception)
示例8: get_mailslist
# 需要导入模块: from googleapiclient.http import BatchHttpRequest [as 别名]
# 或者: from googleapiclient.http.BatchHttpRequest import add [as 别名]
def get_mailslist(message_ids, result, index, credentials):
mails = []
def mailscallbackfunc(result, results, moreresults):
# in old Python versions:
# if seen.has_key(marker)
# but in new ones:
if "UNREAD" in results["labelIds"]:
Unread = "true"
else:
Unread = "false"
for header in results["payload"]["headers"]:
if header["name"] == "Date":
Date = header["value"]
if header["name"] == "From":
From = header["value"]
if From == "Anders Damsgaard <[email protected]>": # SKAL ÆNDRES
Sent = True
else:
Sent = False
if header["name"] == "Subject":
Subject = header["value"]
Contact = {
"id": result,
"messageId": results["id"],
"date": Date,
"subject": Subject,
"snippet": results["snippet"],
"unread": Unread,
"sent": Sent,
}
mails.append(Contact)
# for msg_id in message_ids['messages']:
# batchContacts.add(gmail_service.users().messages().get(userId='me',
# id=msg_id['id'], format='metadata',
# metadataHeaders=['from', 'date']))
# batchContacts.execute()
credentials = client.OAuth2Credentials.from_json(credentials)
http = credentials.authorize(httplib2.Http(cache=".cache"))
# Build the Gmail service from discovery
gmail_service = discovery.build("gmail", "v1", http=http)
batchMails = BatchHttpRequest(callback=mailscallbackfunc)
for msg_id in message_ids["messages"]:
batchMails.add(
gmail_service.users()
.messages()
.get(userId="me", id=msg_id["id"], format="metadata", metadataHeaders=["from", "date", "subject"])
)
batchMails.execute()
results[index] = mails
return True
示例9: get_mailslist
# 需要导入模块: from googleapiclient.http import BatchHttpRequest [as 别名]
# 或者: from googleapiclient.http.BatchHttpRequest import add [as 别名]
def get_mailslist(message_ids, result, index, credentials):
mails = []
def mailscallbackfunc(result, results, moreresults):
# in old Python versions:
# if seen.has_key(marker)
# but in new ones:
if 'UNREAD' in results['labelIds']:
Unread = 'true'
else:
Unread = 'false'
for header in results['payload']['headers']:
if header['name'] == 'Date':
Date = header['value']
if header['name'] == 'From':
From = header['value']
if From == "Anders Damsgaard <[email protected]>": #SKAL ÆNDRES
Sent = True
else:
Sent = False
if header['name'] == 'Subject':
Subject = header['value']
Contact = {
'id': result,
'messageId': results['id'],
'date': Date,
'subject': Subject,
'snippet': results['snippet'],
'unread': Unread,
'sent': Sent
}
mails.append(Contact)
#for msg_id in message_ids['messages']:
# batchContacts.add(gmail_service.users().messages().get(userId='me',
# id=msg_id['id'], format='metadata',
# metadataHeaders=['from', 'date']))
#batchContacts.execute()
credentials = client.OAuth2Credentials.from_json(credentials)
http = credentials.authorize(httplib2.Http(cache=".cache"))
# Build the Gmail service from discovery
gmail_service = discovery.build('gmail', 'v1', http=http)
batchMails = BatchHttpRequest(callback=mailscallbackfunc)
for msg_id in message_ids['messages']:
batchMails.add(gmail_service.users().messages().get(userId='me',
id=msg_id['id'], format='metadata',
metadataHeaders=['from', 'date', 'subject']))
batchMails.execute()
results[index] = mails
return True
示例10: test_new_id
# 需要导入模块: from googleapiclient.http import BatchHttpRequest [as 别名]
# 或者: from googleapiclient.http.BatchHttpRequest import add [as 别名]
def test_new_id(self):
batch = BatchHttpRequest()
id_ = batch._new_id()
self.assertEqual('1', id_)
id_ = batch._new_id()
self.assertEqual('2', id_)
batch.add(self.request1, request_id='3')
id_ = batch._new_id()
self.assertEqual('4', id_)
示例11: test_execute_global_callback
# 需要导入模块: from googleapiclient.http import BatchHttpRequest [as 别名]
# 或者: from googleapiclient.http.BatchHttpRequest import add [as 别名]
def test_execute_global_callback(self):
callbacks = Callbacks()
batch = BatchHttpRequest(callback=callbacks.f)
batch.add(self.request1)
batch.add(self.request2)
http = HttpMockSequence([
({'status': '200',
'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'},
BATCH_RESPONSE),
])
batch.execute(http=http)
self.assertEqual({'foo': 42}, callbacks.responses['1'])
self.assertEqual({'baz': 'qux'}, callbacks.responses['2'])
示例12: test_execute_batch_http_error
# 需要导入模块: from googleapiclient.http import BatchHttpRequest [as 别名]
# 或者: from googleapiclient.http.BatchHttpRequest import add [as 别名]
def test_execute_batch_http_error(self):
callbacks = Callbacks()
batch = BatchHttpRequest(callback=callbacks.f)
batch.add(self.request1)
batch.add(self.request2)
http = HttpMockSequence([
({'status': '200',
'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'},
BATCH_ERROR_RESPONSE),
])
batch.execute(http=http)
self.assertEqual({'foo': 42}, callbacks.responses['1'])
expected = ('<HttpError 403 when requesting '
'https://www.googleapis.com/someapi/v1/collection/?foo=bar returned '
'"Access Not Configured">')
self.assertEqual(expected, str(callbacks.exceptions['2']))
示例13: test_execute_refresh_and_retry_on_401
# 需要导入模块: from googleapiclient.http import BatchHttpRequest [as 别名]
# 或者: from googleapiclient.http.BatchHttpRequest import add [as 别名]
def test_execute_refresh_and_retry_on_401(self):
batch = BatchHttpRequest()
callbacks = Callbacks()
cred_1 = MockCredentials('Foo')
cred_2 = MockCredentials('Bar')
http = HttpMockSequence([
({'status': '200',
'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'},
BATCH_RESPONSE_WITH_401),
({'status': '200',
'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'},
BATCH_SINGLE_RESPONSE),
])
creds_http_1 = HttpMockSequence([])
cred_1.authorize(creds_http_1)
creds_http_2 = HttpMockSequence([])
cred_2.authorize(creds_http_2)
self.request1.http = creds_http_1
self.request2.http = creds_http_2
batch.add(self.request1, callback=callbacks.f)
batch.add(self.request2, callback=callbacks.f)
batch.execute(http=http)
self.assertEqual({'foo': 42}, callbacks.responses['1'])
self.assertEqual(None, callbacks.exceptions['1'])
self.assertEqual({'baz': 'qux'}, callbacks.responses['2'])
self.assertEqual(None, callbacks.exceptions['2'])
self.assertEqual(1, cred_1._refreshed)
self.assertEqual(0, cred_2._refreshed)
self.assertEqual(1, cred_1._authorized)
self.assertEqual(1, cred_2._authorized)
self.assertEqual(1, cred_2._applied)
self.assertEqual(2, cred_1._applied)
示例14: test_execute_request_body
# 需要导入模块: from googleapiclient.http import BatchHttpRequest [as 别名]
# 或者: from googleapiclient.http.BatchHttpRequest import add [as 别名]
def test_execute_request_body(self):
batch = BatchHttpRequest()
batch.add(self.request1)
batch.add(self.request2)
http = HttpMockSequence([
({'status': '200',
'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'},
'echo_request_body'),
])
try:
batch.execute(http=http)
self.fail('Should raise exception')
except BatchError, e:
boundary, _ = e.content.split(None, 1)
self.assertEqual('--', boundary[:2])
parts = e.content.split(boundary)
self.assertEqual(4, len(parts))
self.assertEqual('', parts[0])
self.assertEqual('--', parts[3].rstrip())
header = parts[1].splitlines()[1]
self.assertEqual('Content-Type: application/http', header)
示例15: summarize_mail
# 需要导入模块: from googleapiclient.http import BatchHttpRequest [as 别名]
# 或者: from googleapiclient.http.BatchHttpRequest import add [as 别名]
def summarize_mail():
service, http = authorize_me()
batch = BatchHttpRequest()
response = service.users().messages().list(userId=constants.USER_ID, q=constants.DEFAULT_QUERY).execute()
messages = response.get('messages', [])
if messages:
# TODO think this only gets the first few, might need to page through results
for message in messages:
request = service.users().messages().get(userId=constants.USER_ID, id=message['id'], format=constants.RESPONSE_FORMAT, metadataHeaders=[constants.FROM_HEADER, constants.TO_HEADER])
batch.add(request, callback=parse_mail)
batch.execute(http=http)
print "There are %s unread messages in your inbox." % len(summary.messages)
print "%s of them are marked as important and %s are addressed to you." % (summary.important, summary.addressed_to_me)
print "%s of them are scary." % summary.flagged
response = raw_input("\nYou can do the thing! Do you want to see a preview of your messages? ")
if response.lower().startswith('y'):
print "\n"
for i, msg in enumerate(summary.messages):
print "Message #%s" % i
print "=" * 30
print "From: %s\nTo: %s\nSubject: %s" % (msg.sender, msg.to, msg.snippet)
print "=" * 30
print "\n"
else:
print "No messages! You are free. :)"