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


Python http.HttpRequest类代码示例

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


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

示例1: test_retry

  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,代码行数:26,代码来源:test_http.py

示例2: test_media_io_base_next_chunk_no_retry_403_not_configured

  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,代码行数:27,代码来源:test_http.py

示例3: test_http_request_to_from_json

  def test_http_request_to_from_json(self):

    def _postproc(*kwargs):
      pass

    http = httplib2.Http()
    media_upload = MediaFileUpload(
        datafile('small.png'), chunksize=500, resumable=True)
    req = HttpRequest(
        http,
        _postproc,
        'http://example.com',
        method='POST',
        body='{}',
        headers={'content-type': 'multipart/related; boundary="---flubber"'},
        methodId='foo',
        resumable=media_upload)

    json = req.to_json()
    new_req = HttpRequest.from_json(json, http, _postproc)

    self.assertEqual({'content-type':
                       'multipart/related; boundary="---flubber"'},
                       new_req.headers)
    self.assertEqual('http://example.com', new_req.uri)
    self.assertEqual('{}', new_req.body)
    self.assertEqual(http, new_req.http)
    self.assertEqual(media_upload.to_json(), new_req.resumable.to_json())

    self.assertEqual(random.random, new_req._rand)
    self.assertEqual(time.sleep, new_req._sleep)
开发者ID:Acidburn0zzz,项目名称:google-api-python-client,代码行数:31,代码来源:test_http.py

示例4: test_retry_connection_errors_non_resumable

 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,代码行数:10,代码来源:test_http.py

示例5: test_no_retry_connection_errors

 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,代码行数:10,代码来源:test_http.py

示例6: test_empty_content_type

 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'))
开发者ID:arush6705,项目名称:google-api-python-client,代码行数:13,代码来源:test_http.py

示例7: test_ensure_response_callback

 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))
开发者ID:Acidburn0zzz,项目名称:google-api-python-client,代码行数:16,代码来源:test_http.py

示例8: test_unicode

 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))
开发者ID:Acidburn0zzz,项目名称:google-api-python-client,代码行数:17,代码来源:test_http.py

示例9: test_retry_connection_errors_resumable

  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,代码行数:18,代码来源:test_http.py

示例10: test_media_io_base_next_chunk_retries

  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,代码行数:41,代码来源:test_http.py

示例11: test_no_retry_401_fails_fast

  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,代码行数:22,代码来源:test_http.py

示例12: test_turn_get_into_post

  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'])
开发者ID:Acidburn0zzz,项目名称:google-api-python-client,代码行数:36,代码来源:test_http.py

示例13: test_no_retry_fails_fast

  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,代码行数:24,代码来源:test_http.py


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