当前位置: 首页>>代码示例>>Python>>正文


Python HttpRequest._rand方法代码示例

本文整理汇总了Python中googleapiclient.http.HttpRequest._rand方法的典型用法代码示例。如果您正苦于以下问题:Python HttpRequest._rand方法的具体用法?Python HttpRequest._rand怎么用?Python HttpRequest._rand使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在googleapiclient.http.HttpRequest的用法示例。


在下文中一共展示了HttpRequest._rand方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_media_io_base_next_chunk_no_retry_403_not_configured

# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import _rand [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()
开发者ID:CleverChuk,项目名称:google-api-python-client,代码行数:29,代码来源:test_http.py

示例2: test_retry

# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import _rand [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])
开发者ID:Acidburn0zzz,项目名称:google-api-python-client,代码行数:28,代码来源:test_http.py

示例3: test_retry_connection_errors_non_resumable

# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import _rand [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)
开发者ID:CleverChuk,项目名称:google-api-python-client,代码行数:12,代码来源:test_http.py

示例4: test_no_retry_connection_errors

# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import _rand [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)
开发者ID:CleverChuk,项目名称:google-api-python-client,代码行数:12,代码来源:test_http.py

示例5: test_retry_connection_errors_resumable

# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import _rand [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)
开发者ID:CleverChuk,项目名称:google-api-python-client,代码行数:20,代码来源:test_http.py

示例6: test_media_io_base_next_chunk_retries

# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import _rand [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)
开发者ID:Acidburn0zzz,项目名称:google-api-python-client,代码行数:43,代码来源:test_http.py

示例7: test_no_retry_401_fails_fast

# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import _rand [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()
开发者ID:CleverChuk,项目名称:google-api-python-client,代码行数:24,代码来源:test_http.py

示例8: test_no_retry_fails_fast

# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import _rand [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
开发者ID:Acidburn0zzz,项目名称:google-api-python-client,代码行数:26,代码来源:test_http.py


注:本文中的googleapiclient.http.HttpRequest._rand方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。