示例#1
0
def CheckResponse(response):
  if response is None:
    # Caller shouldn't call us if the response is None, but handle anyway.
    raise exceptions.RequestError('Request to url %s did not return a response.'
                                  % response.request_url)
  elif (response.status_code >= 500 or
        response.status_code == TOO_MANY_REQUESTS):
    raise exceptions.BadStatusCodeError.FromResponse(response)
  elif response.status_code == httplib.UNAUTHORIZED:
    # Sometimes we get a 401 after a connection break.
    # TODO: this shouldn't be a retryable exception, but for now we retry.
    raise exceptions.BadStatusCodeError.FromResponse(response)
  elif response.retry_after:
    raise exceptions.RetryAfterError.FromResponse(response)
示例#2
0
def _MakeRequestNoRetry(http,
                        http_request,
                        redirections=5,
                        check_response_func=CheckResponse):
    """Send http_request via the given http.

  This wrapper exists to handle translation between the plain httplib2
  request/response types and the Request and Response types above.

  Args:
    http: An httplib2.Http instance, or a http multiplexer that delegates to
        an underlying http, for example, HTTPMultiplexer.
    http_request: A Request to send.
    redirections: (int, default 5) Number of redirects to follow.
    check_response_func: Function to validate the HTTP response. Arguments are
                         (Response, response content, url).

  Returns:
    Response object.

  Raises:
    RequestError if no response could be parsed.
  """
    connection_type = None
    if getattr(http, 'connections', None):
        url_scheme = urlparse.urlsplit(http_request.url).scheme
        if url_scheme and url_scheme in http.connections:
            connection_type = http.connections[url_scheme]

    # Custom printing only at debuglevel 4
    new_debuglevel = 4 if httplib2.debuglevel == 4 else 0
    with _Httplib2Debuglevel(http_request, new_debuglevel, http=http):
        info, content = http.request(str(http_request.url),
                                     method=str(http_request.http_method),
                                     body=http_request.body,
                                     headers=http_request.headers,
                                     redirections=redirections,
                                     connection_type=connection_type)

    if info is None:
        raise exceptions.RequestError()

    response = Response(info, content, http_request.url)
    check_response_func(response)
    return response
示例#3
0
 def loggable_body(self, value):
     if self.body is None:
         raise exceptions.RequestError(
             'Cannot set loggable body on request with no body')
     self.__loggable_body = value