本文整理汇总了Python中googleapiclient.http.BatchHttpRequest类的典型用法代码示例。如果您正苦于以下问题:Python BatchHttpRequest类的具体用法?Python BatchHttpRequest怎么用?Python BatchHttpRequest使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BatchHttpRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_label_list_info
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
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
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: test_execute_initial_refresh_oauth2
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)
示例5: delete_bucket_content
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: get_message_list_info
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
示例7: test_deserialize_response
def test_deserialize_response(self):
batch = BatchHttpRequest()
resp, content = batch._deserialize_response(RESPONSE)
self.assertEqual(200, resp.status)
self.assertEqual('OK', resp.reason)
self.assertEqual(11, resp.version)
self.assertEqual('{"answer": 42}', content)
示例8: test_add_fail_for_resumable
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)
示例9: get_mailslist
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: get_mailslist
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
示例11: test_serialize_request_no_body
def test_serialize_request_no_body(self):
batch = BatchHttpRequest()
request = HttpRequest(
None,
None,
'https://www.googleapis.com/someapi/v1/collection/?foo=bar',
method='POST',
body='',
headers={'content-type': 'application/json'},
methodId=None,
resumable=None)
s = batch._serialize_request(request).splitlines()
self.assertEqual(NO_BODY_EXPECTED.splitlines(), s)
示例12: test_execute_global_callback
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'])
示例13: test_serialize_request_media_body
def test_serialize_request_media_body(self):
batch = BatchHttpRequest()
f = open(datafile('small.png'))
body = f.read()
f.close()
request = HttpRequest(
None,
None,
'https://www.googleapis.com/someapi/v1/collection/?foo=bar',
method='POST',
body=body,
headers={'content-type': 'application/json'},
methodId=None,
resumable=None)
# Just testing it shouldn't raise an exception.
s = batch._serialize_request(request).splitlines()
示例14: test_execute_batch_http_error
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']))
示例15: test_execute_refresh_and_retry_on_401
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)