Exemplo n.º 1
0
 def perform_request(self,
                     http_client,
                     operation,
                     url,
                     data=None,
                     headers=None):
     """Sets the Authorization header to the basic auth string."""
     if headers is None:
         headers = {'Authorization': self.auth_header}
     else:
         headers['Authorization'] = self.auth_header
     return http_client.request(operation, url, data=data, headers=headers)
Exemplo n.º 2
0
    def _SendJsonRequest(self, method, path, body=None, timeout_secs=300):
        """Sends a request to the broker.

    Args:
      method: (str) The HTTP method.
      path: (str) The URI path.
      body: (str) The request body.
      timeout_secs: (float) The request timeout, in seconds.

    Returns:
      (HTTPResponse, str) or (None, None).

    Raises:
      RequestTimeoutError: The request timed-out.
      RequestSocketError: The request failed due to a socket error.
      RequestError: The request errored out in some other way.
    """
        uri = 'http://{0}{1}'.format(self._address, path)
        http_client = http.HttpClient(timeout=timeout_secs)
        try:
            http_response, body = http_client.request(
                uri=uri,
                method=method,
                headers={'Content-Type': 'application/json; charset=UTF-8'},
                body=body)
            return http_response, body.decode('utf-8')
        except socket.error as e:
            if isinstance(e, socket.timeout):
                raise RequestTimeoutError(e)
            error = RequestSocketError(e)
            if e.errno:
                error.errno = e.errno
            raise error
        except six.moves.http_client.HTTPException as e:
            if isinstance(e, six.moves.http_client.ResponseNotReady):
                raise RequestTimeoutError(e)
            raise RequestError(e)
        except httplib2.HttpLib2Error as e:
            raise RequestError(e)