Example #1
0
def Http(cmd_path=None, trace_token=None, auth=True, creds=None, timeout=None):
    """Get an httplib2.Http object for working with the Google API.

  Args:
    cmd_path: str, Path of command that will use the httplib2.Http object.
    trace_token: str, Token to be used to route service request traces.
    auth: bool, True if the http object returned should be authorized.
    creds: oauth2client.client.Credentials, If auth is True and creds is not
        None, use those credentials to authorize the httplib2.Http object.
    timeout: double, The timeout in seconds to pass to httplib2.  This is the
        socket level timeout.  If timeout is None, timeout is infinite.

  Returns:
    An authorized httplib2.Http object, or a regular httplib2.Http object if no
    credentials are available.
  """

    # TODO(user): Have retry-once-if-denied logic, to allow client tools to not
    # worry about refreshing credentials.

    http = c_store._Http(timeout=timeout)  # pylint:disable=protected-access

    # Wrap the request method to put in our own user-agent, and trace reporting.
    gcloud_ua = 'gcloud/{0} command/{1} invocation-id/{2}'.format(
        config.CLOUD_SDK_VERSION, cmd_path, time.time())
    http = _WrapRequestForUserAgentAndTracing(http, trace_token, gcloud_ua)
    if auth:
        if not creds:
            creds = Credentials()
        http = creds.authorize(http)
        # Wrap the request method to put in our own error handling.
        http = _WrapRequestForAuthErrHandling(http)
    return http
def Http(auth=True, creds=None, timeout='unset'):
    """Get an httplib2.Http object for working with the Google API.

  Args:
    auth: bool, True if the http object returned should be authorized.
    creds: oauth2client.client.Credentials, If auth is True and creds is not
        None, use those credentials to authorize the httplib2.Http object.
    timeout: double, The timeout in seconds to pass to httplib2.  This is the
        socket level timeout.  If timeout is None, timeout is infinite.  If
        default argument 'unset' is given, a sensible default is selected.

  Returns:
    An authorized httplib2.Http object, or a regular httplib2.Http object if no
    credentials are available.

  Raises:
    c_store.Error: If an error loading the credentials occurs.
  """

    # Compared with setting the default timeout in the function signature (i.e.
    # timeout=300), this lets you test with short default timeouts by mocking
    # GetDefaultTimeout.
    effective_timeout = timeout if timeout != 'unset' else GetDefaultTimeout()

    # TODO(user): Have retry-once-if-denied logic, to allow client tools to not
    # worry about refreshing credentials.

    http = c_store._Http(  # pylint:disable=protected-access
        timeout=effective_timeout,
        proxy_info=_GetHttpProxyInfo())

    # Wrap first to dump any data added by other wrappers.
    if properties.VALUES.core.log_http.GetBool():
        http = _WrapRequestForLogging(http)

    # Wrap the request method to put in our own user-agent, and trace reporting.
    gcloud_ua = MakeUserAgentString(
        properties.VALUES.metrics.command_name.Get())

    http = _WrapRequestForUserAgentAndTracing(
        http, properties.VALUES.core.trace_token.Get(),
        properties.VALUES.core.trace_email.Get(),
        properties.VALUES.core.trace_log.GetBool(), gcloud_ua)

    authority_selector = properties.VALUES.auth.authority_selector.Get()
    authorization_token_file = (
        properties.VALUES.auth.authorization_token_file.Get())
    if authority_selector or authorization_token_file:
        http = _WrapRequestForIAMAuth(http, authority_selector,
                                      authorization_token_file)

    if auth:
        if not creds:
            creds = c_store.Load()
        http = creds.authorize(http)
        # Wrap the request method to put in our own error handling.
        http = _WrapRequestForAuthErrHandling(http)

    return http
Example #3
0
def Http(auth=True, creds=None, timeout='unset'):
  """Get an httplib2.Http object for working with the Google API.

  Args:
    auth: bool, True if the http object returned should be authorized.
    creds: oauth2client.client.Credentials, If auth is True and creds is not
        None, use those credentials to authorize the httplib2.Http object.
    timeout: double, The timeout in seconds to pass to httplib2.  This is the
        socket level timeout.  If timeout is None, timeout is infinite.  If
        default argument 'unset' is given, a sensible default is selected.

  Returns:
    An authorized httplib2.Http object, or a regular httplib2.Http object if no
    credentials are available.

  Raises:
    c_store.Error: If an error loading the credentials occurs.
  """

  # Compared with setting the default timeout in the function signature (i.e.
  # timeout=300), this lets you test with short default timeouts by mocking
  # GetDefaultTimeout.
  effective_timeout = timeout if timeout != 'unset' else GetDefaultTimeout()

  # TODO(user): Have retry-once-if-denied logic, to allow client tools to not
  # worry about refreshing credentials.

  http = c_store._Http(  # pylint:disable=protected-access
      timeout=effective_timeout, proxy_info=_GetHttpProxyInfo())

  # Wrap first to dump any data added by other wrappers.
  if properties.VALUES.core.log_http.GetBool():
    http = _WrapRequestForLogging(http)

  # Wrap the request method to put in our own user-agent, and trace reporting.
  gcloud_ua = MakeUserAgentString(properties.VALUES.metrics.command_name.Get())

  http = _WrapRequestForUserAgentAndTracing(
      http,
      properties.VALUES.core.trace_token.Get(),
      properties.VALUES.core.trace_email.Get(),
      properties.VALUES.core.trace_log.GetBool(),
      gcloud_ua)

  authority_selector = properties.VALUES.auth.authority_selector.Get()
  authorization_token_file = (
      properties.VALUES.auth.authorization_token_file.Get())
  if authority_selector or authorization_token_file:
    http = _WrapRequestForIAMAuth(
        http, authority_selector, authorization_token_file)

  if auth:
    if not creds:
      creds = c_store.Load()
    http = creds.authorize(http)
    # Wrap the request method to put in our own error handling.
    http = _WrapRequestForAuthErrHandling(http)

  return http
Example #4
0
def Http(cmd_path=None,
         trace_token=None,
         trace_email=None,
         trace_log=False,
         auth=True,
         creds=None,
         timeout='unset',
         log_http=False):
    """Get an httplib2.Http object for working with the Google API.

  Args:
    cmd_path: str, Path of command that will use the httplib2.Http object.
    trace_token: str, Token to be used to route service request traces.
    trace_email: str, username to which service request traces should be sent.
    trace_log: bool, Enable/disable server side logging of service requests.
    auth: bool, True if the http object returned should be authorized.
    creds: oauth2client.client.Credentials, If auth is True and creds is not
        None, use those credentials to authorize the httplib2.Http object.
    timeout: double, The timeout in seconds to pass to httplib2.  This is the
        socket level timeout.  If timeout is None, timeout is infinite.  If
        default argument 'unset' is given, a sensible default is selected.
    log_http: bool, Enable/disable client side logging of service requests.

  Returns:
    An authorized httplib2.Http object, or a regular httplib2.Http object if no
    credentials are available.

  Raises:
    c_store.Error: If an error loading the credentials occurs.
  """

    # Compared with setting the default timeout in the function signature (i.e.
    # timeout=300), this lets you test with short default timeouts by mocking
    # GetDefaultTimeout.
    effective_timeout = timeout if timeout != 'unset' else GetDefaultTimeout()

    # TODO(jasmuth): Have retry-once-if-denied logic, to allow client tools to not
    # worry about refreshing credentials.

    http = c_store._Http(  # pylint:disable=protected-access
        timeout=effective_timeout)

    # Wrap first to dump any data added by other wrappers.
    if log_http:
        http = _WrapRequestForLogging(http)

    # Wrap the request method to put in our own user-agent, and trace reporting.
    gcloud_ua = MakeUserAgentString(cmd_path)

    http = _WrapRequestForUserAgentAndTracing(http, trace_token, trace_email,
                                              trace_log, gcloud_ua)
    if auth:
        if not creds:
            creds = c_store.Load()
        http = creds.authorize(http)
        # Wrap the request method to put in our own error handling.
        http = _WrapRequestForAuthErrHandling(http)
    return http
Example #5
0
File: cli.py Project: wemanuel/smry
def Http(cmd_path=None, trace_token=None,
         auth=True, creds=None, timeout=None, log_http=False):
  """Get an httplib2.Http object for working with the Google API.

  Args:
    cmd_path: str, Path of command that will use the httplib2.Http object.
    trace_token: str, Token to be used to route service request traces.
    auth: bool, True if the http object returned should be authorized.
    creds: oauth2client.client.Credentials, If auth is True and creds is not
        None, use those credentials to authorize the httplib2.Http object.
    timeout: double, The timeout in seconds to pass to httplib2.  This is the
        socket level timeout.  If timeout is None, timeout is infinite.
    log_http: bool, Enable/disable client side logging of service requests.

  Returns:
    An authorized httplib2.Http object, or a regular httplib2.Http object if no
    credentials are available.

  Raises:
    c_store.Error: If an error loading the credentials occurs.
  """

  # TODO(user): Have retry-once-if-denied logic, to allow client tools to not
  # worry about refreshing credentials.

  http = c_store._Http(timeout=timeout)  # pylint:disable=protected-access

  # Wrap first to dump any data added by other wrappers.
  if log_http:
    http = _WrapRequestForLogging(http)

  # Wrap the request method to put in our own user-agent, and trace reporting.
  gcloud_ua = (
      'gcloud/{0}'
      ' command/{1}'
      ' invocation-id/{2}'
      ' environment/{3}'
      ' interactive/{4}'
      ' {5}'
      ).format(
          config.CLOUD_SDK_VERSION,
          cmd_path,
          uuid.uuid4().hex,
          properties.VALUES.metrics.environment.Get(),
          console_io.IsInteractive(error=True, heuristic=True),
          platforms.Platform.Current().UserAgentFragment())

  http = _WrapRequestForUserAgentAndTracing(http, trace_token,
                                            gcloud_ua)
  if auth:
    if not creds:
      creds = c_store.Load()
    http = creds.authorize(http)
    # Wrap the request method to put in our own error handling.
    http = _WrapRequestForAuthErrHandling(http)
  return http
Example #6
0
def Http(cmd_path=None,
         trace_token=None,
         auth=True,
         creds=None,
         timeout=None,
         log_http=False):
    """Get an httplib2.Http object for working with the Google API.

  Args:
    cmd_path: str, Path of command that will use the httplib2.Http object.
    trace_token: str, Token to be used to route service request traces.
    auth: bool, True if the http object returned should be authorized.
    creds: oauth2client.client.Credentials, If auth is True and creds is not
        None, use those credentials to authorize the httplib2.Http object.
    timeout: double, The timeout in seconds to pass to httplib2.  This is the
        socket level timeout.  If timeout is None, timeout is infinite.
    log_http: bool, Enable/disable client side logging of service requests.

  Returns:
    An authorized httplib2.Http object, or a regular httplib2.Http object if no
    credentials are available.

  Raises:
    c_store.Error: If an error loading the credentials occurs.
  """

    # TODO(user): Have retry-once-if-denied logic, to allow client tools to not
    # worry about refreshing credentials.

    http = c_store._Http(timeout=timeout)  # pylint:disable=protected-access

    # Wrap first to dump any data added by other wrappers.
    if log_http:
        http = _WrapRequestForLogging(http)

    # Wrap the request method to put in our own user-agent, and trace reporting.
    gcloud_ua = ('gcloud/{0}'
                 ' command/{1}'
                 ' invocation-id/{2}'
                 ' environment/{3}'
                 ' interactive/{4}'
                 ' {5}').format(
                     config.CLOUD_SDK_VERSION, cmd_path,
                     uuid.uuid4().hex,
                     properties.VALUES.metrics.environment.Get(),
                     console_io.IsInteractive(error=True, heuristic=True),
                     platforms.Platform.Current().UserAgentFragment())

    http = _WrapRequestForUserAgentAndTracing(http, trace_token, gcloud_ua)
    if auth:
        if not creds:
            creds = c_store.Load()
        http = creds.authorize(http)
        # Wrap the request method to put in our own error handling.
        http = _WrapRequestForAuthErrHandling(http)
    return http
Example #7
0
def Http(cmd_path=None, trace_token=None,
         trace_email=None,
         trace_log=False,
         auth=True, creds=None, timeout='unset', log_http=False):
  """Get an httplib2.Http object for working with the Google API.

  Args:
    cmd_path: str, Path of command that will use the httplib2.Http object.
    trace_token: str, Token to be used to route service request traces.
    trace_email: str, username to which service request traces should be sent.
    trace_log: bool, Enable/disable server side logging of service requests.
    auth: bool, True if the http object returned should be authorized.
    creds: oauth2client.client.Credentials, If auth is True and creds is not
        None, use those credentials to authorize the httplib2.Http object.
    timeout: double, The timeout in seconds to pass to httplib2.  This is the
        socket level timeout.  If timeout is None, timeout is infinite.  If
        default argument 'unset' is given, a sensible default is selected.
    log_http: bool, Enable/disable client side logging of service requests.

  Returns:
    An authorized httplib2.Http object, or a regular httplib2.Http object if no
    credentials are available.

  Raises:
    c_store.Error: If an error loading the credentials occurs.
  """

  # Compared with setting the default timeout in the function signature (i.e.
  # timeout=300), this lets you test with short default timeouts by mocking
  # GetDefaultTimeout.
  effective_timeout = timeout if timeout != 'unset' else GetDefaultTimeout()

  # TODO(jasmuth): Have retry-once-if-denied logic, to allow client tools to not
  # worry about refreshing credentials.

  http = c_store._Http(  # pylint:disable=protected-access
      timeout=effective_timeout)

  # Wrap first to dump any data added by other wrappers.
  if log_http:
    http = _WrapRequestForLogging(http)

  # Wrap the request method to put in our own user-agent, and trace reporting.
  gcloud_ua = MakeUserAgentString(cmd_path)

  http = _WrapRequestForUserAgentAndTracing(http, trace_token,
                                            trace_email,
                                            trace_log,
                                            gcloud_ua)
  if auth:
    if not creds:
      creds = c_store.Load()
    http = creds.authorize(http)
    # Wrap the request method to put in our own error handling.
    http = _WrapRequestForAuthErrHandling(http)
  return http