本文整理汇总了Python中googleapiclient.http.HttpRequest.execute方法的典型用法代码示例。如果您正苦于以下问题:Python HttpRequest.execute方法的具体用法?Python HttpRequest.execute怎么用?Python HttpRequest.execute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类googleapiclient.http.HttpRequest
的用法示例。
在下文中一共展示了HttpRequest.execute方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_media_io_base_next_chunk_no_retry_403_not_configured
# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import execute [as 别名]
def test_media_io_base_next_chunk_no_retry_403_not_configured(self):
fd = BytesIO(b"i am png")
upload = MediaIoBaseUpload(
fd=fd, mimetype='image/png', chunksize=500, resumable=True)
http = HttpMockSequence([
({'status': '403'}, NOT_CONFIGURED_RESPONSE),
({'status': '200'}, '{}')
])
model = JsonModel()
uri = u'https://www.googleapis.com/someapi/v1/upload/?foo=bar'
method = u'POST'
request = HttpRequest(
http,
model.response,
uri,
method=method,
headers={},
resumable=upload)
request._rand = lambda: 1.0
request._sleep = mock.MagicMock()
with self.assertRaises(HttpError):
request.execute(num_retries=3)
request._sleep.assert_not_called()
示例2: test_retry
# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import execute [as 别名]
def test_retry(self):
num_retries = 5
resp_seq = [({'status': '500'}, '')] * num_retries
resp_seq.append(({'status': '200'}, '{}'))
http = HttpMockSequence(resp_seq)
model = JsonModel()
uri = u'https://www.googleapis.com/someapi/v1/collection/?foo=bar'
method = u'POST'
request = HttpRequest(
http,
model.response,
uri,
method=method,
body=u'{}',
headers={'content-type': 'application/json'})
sleeptimes = []
request._sleep = lambda x: sleeptimes.append(x)
request._rand = lambda: 10
request.execute(num_retries=num_retries)
self.assertEqual(num_retries, len(sleeptimes))
for retry_num in xrange(num_retries):
self.assertEqual(10 * 2**(retry_num + 1), sleeptimes[retry_num])
示例3: test_empty_content_type
# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import execute [as 别名]
def test_empty_content_type(self):
"""Test for #284"""
http = HttpMock(None, headers={'status': 200})
uri = u'https://www.googleapis.com/someapi/v1/upload/?foo=bar'
method = u'POST'
request = HttpRequest(
http,
_postproc_none,
uri,
method=method,
headers={'content-type': ''})
request.execute()
self.assertEqual('', http.headers.get('content-type'))
示例4: test_ensure_response_callback
# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import execute [as 别名]
def test_ensure_response_callback(self):
m = JsonModel()
request = HttpRequest(
None,
m.response,
'https://www.googleapis.com/someapi/v1/collection/?foo=bar',
method='POST',
body='{}',
headers={'content-type': 'application/json'})
h = HttpMockSequence([ ({'status': 200}, '{}')])
responses = []
def _on_response(resp, responses=responses):
responses.append(resp)
request.add_response_callback(_on_response)
request.execute(http=h)
self.assertEqual(1, len(responses))
示例5: test_unicode
# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import execute [as 别名]
def test_unicode(self):
http = HttpMock(datafile('zoo.json'), headers={'status': '200'})
model = JsonModel()
uri = u'https://www.googleapis.com/someapi/v1/collection/?foo=bar'
method = u'POST'
request = HttpRequest(
http,
model.response,
uri,
method=method,
body=u'{}',
headers={'content-type': 'application/json'})
request.execute()
self.assertEqual(uri, http.uri)
self.assertEqual(str, type(http.uri))
self.assertEqual(method, http.method)
self.assertEqual(str, type(http.method))
示例6: test_retry_connection_errors_non_resumable
# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import execute [as 别名]
def test_retry_connection_errors_non_resumable(self):
model = JsonModel()
request = HttpRequest(
HttpMockWithErrors(3, {'status': '200'}, '{"foo": "bar"}'),
model.response,
u'https://www.example.com/json_api_endpoint')
request._sleep = lambda _x: 0 # do nothing
request._rand = lambda: 10
response = request.execute(num_retries=3)
self.assertEqual({u'foo': u'bar'}, response)
示例7: test_no_retry_connection_errors
# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import execute [as 别名]
def test_no_retry_connection_errors(self):
model = JsonModel()
request = HttpRequest(
HttpMockWithNonRetriableErrors(1, {'status': '200'}, '{"foo": "bar"}'),
model.response,
u'https://www.example.com/json_api_endpoint')
request._sleep = lambda _x: 0 # do nothing
request._rand = lambda: 10
with self.assertRaises(socket.error):
response = request.execute(num_retries=3)
示例8: test_media_io_base_next_chunk_retries
# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import execute [as 别名]
def test_media_io_base_next_chunk_retries(self):
try:
import io
except ImportError:
return
f = open(datafile('small.png'), 'r')
fd = io.BytesIO(f.read())
upload = MediaIoBaseUpload(
fd=fd, mimetype='image/png', chunksize=500, resumable=True)
# Simulate 5XXs for both the request that creates the resumable upload and
# the upload itself.
http = HttpMockSequence([
({'status': '500'}, ''),
({'status': '500'}, ''),
({'status': '503'}, ''),
({'status': '200', 'location': 'location'}, ''),
({'status': '500'}, ''),
({'status': '500'}, ''),
({'status': '503'}, ''),
({'status': '200'}, '{}'),
])
model = JsonModel()
uri = u'https://www.googleapis.com/someapi/v1/upload/?foo=bar'
method = u'POST'
request = HttpRequest(
http,
model.response,
uri,
method=method,
headers={},
resumable=upload)
sleeptimes = []
request._sleep = lambda x: sleeptimes.append(x)
request._rand = lambda: 10
request.execute(num_retries=3)
self.assertEqual([20, 40, 80, 20, 40, 80], sleeptimes)
示例9: test_no_retry_401_fails_fast
# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import execute [as 别名]
def test_no_retry_401_fails_fast(self):
http = HttpMockSequence([
({'status': '401'}, ''),
({'status': '200'}, '{}')
])
model = JsonModel()
uri = u'https://www.googleapis.com/someapi/v1/collection/?foo=bar'
method = u'POST'
request = HttpRequest(
http,
model.response,
uri,
method=method,
body=u'{}',
headers={'content-type': 'application/json'})
request._rand = lambda: 1.0
request._sleep = mock.MagicMock()
with self.assertRaises(HttpError):
request.execute()
request._sleep.assert_not_called()
示例10: test_turn_get_into_post
# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import execute [as 别名]
def test_turn_get_into_post(self):
def _postproc(resp, content):
return content
http = HttpMockSequence([
({'status': '200'},
'echo_request_body'),
({'status': '200'},
'echo_request_headers'),
])
# Send a long query parameter.
query = {
'q': 'a' * MAX_URI_LENGTH + '?&'
}
req = HttpRequest(
http,
_postproc,
'http://example.com?' + urllib.urlencode(query),
method='GET',
body=None,
headers={},
methodId='foo',
resumable=None)
# Query parameters should be sent in the body.
response = req.execute()
self.assertEqual('q=' + 'a' * MAX_URI_LENGTH + '%3F%26', response)
# Extra headers should be set.
response = req.execute()
self.assertEqual('GET', response['x-http-method-override'])
self.assertEqual(str(MAX_URI_LENGTH + 8), response['content-length'])
self.assertEqual(
'application/x-www-form-urlencoded', response['content-type'])
示例11: test_no_retry_fails_fast
# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import execute [as 别名]
def test_no_retry_fails_fast(self):
http = HttpMockSequence([
({'status': '500'}, ''),
({'status': '200'}, '{}')
])
model = JsonModel()
uri = u'https://www.googleapis.com/someapi/v1/collection/?foo=bar'
method = u'POST'
request = HttpRequest(
http,
model.response,
uri,
method=method,
body=u'{}',
headers={'content-type': 'application/json'})
request._rand = lambda: 1.0
request._sleep = lambda _: self.fail('sleep should not have been called.')
try:
request.execute()
self.fail('Should have raised an exception.')
except HttpError:
pass
示例12: test_retry_connection_errors_resumable
# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import execute [as 别名]
def test_retry_connection_errors_resumable(self):
with open(datafile('small.png'), 'rb') as small_png_file:
small_png_fd = BytesIO(small_png_file.read())
upload = MediaIoBaseUpload(fd=small_png_fd, mimetype='image/png',
chunksize=500, resumable=True)
model = JsonModel()
request = HttpRequest(
HttpMockWithErrors(
3, {'status': '200', 'location': 'location'}, '{"foo": "bar"}'),
model.response,
u'https://www.example.com/file_upload',
method='POST',
resumable=upload)
request._sleep = lambda _x: 0 # do nothing
request._rand = lambda: 10
response = request.execute(num_retries=3)
self.assertEqual({u'foo': u'bar'}, response)