Пример #1
0
def _GetReportingClient():
    """Returns a client that uses an API key for Cloud SDK crash reports.

  Returns:
    An error reporting client that uses an API key for Cloud SDK crash reports.
  """
    http_client = http.Http()
    orig_request = http_client.request

    def RequestWithAPIKey(*args, **kwargs):
        """Wrap request for request/response logging.

    Args:
      *args: Positional arguments.
      **kwargs: Keyword arguments.

    Returns:
      Original returned response of this wrapped request.
    """
        # Modify request url to enable API key based authentication.
        url_parts = urlparse.urlsplit(args[0])
        query_params = urlparse.parse_qs(url_parts.query)
        query_params['key'] = CRASH_API_KEY

        modified_url_parts = list(url_parts)
        modified_url_parts[3] = urllib.urlencode(query_params, doseq=True)
        modified_args = list(args)
        modified_args[0] = urlparse.urlunsplit(modified_url_parts)

        return orig_request(*modified_args, **kwargs)

    http_client.request = RequestWithAPIKey

    client_class = core_apis.GetClientClass(util.API_NAME, util.API_VERSION)
    return client_class(get_credentials=False, http=http_client)
Пример #2
0
    def ParseCollectionPath(self,
                            collection,
                            collection_path,
                            kwargs,
                            resolve=True):
        """Parse a collection path into a Resource.

    Args:
      collection: str, the name/id for the resource from commandline argument.
      collection_path: str, The human-typed collection-path from the command
          line. Can be None to indicate all params should be taken from kwargs.
      kwargs: {str:(str or func()->str)}, flags (available from context) or
          resolvers that can help parse this resource. If the fields in
          collection-path do not provide all the necessary information,
          kwargs will be searched for what remains.
      resolve: bool, If True, call the resource's .Resolve() method before
          returning, ensuring that all of the resource parameters are defined.
          If False, don't call them, under the assumption that it will be called
          later.
    Returns:
      protorpc.messages.Message, The object containing info about this resource.

    Raises:
      InvalidCollectionException: If the provided collection-path is malformed.

    """
        # Register relevant API if necessary and possible
        api_name = _APINameFromCollection(collection)
        api_version = self.RegisterApiByName(api_name)

        parser = (self.parsers_by_collection.get(api_name, {}).get(
            api_version, {}).get(collection, None))
        if parser is None:
            raise InvalidCollectionException(collection)

        # Use current override endpoint for this resource name.
        endpoint_override_property = getattr(
            properties.VALUES.api_endpoint_overrides, api_name, None)
        base_url = None
        if endpoint_override_property is not None:
            base_url = endpoint_override_property.Get()
            if base_url is not None:
                # Check base url style. If it includes api version then override
                # also replaces the version, otherwise it only overrides the domain.
                client_class = core_apis.GetClientClass(api_name, api_version)
                _, url_version, _ = resource_util.SplitDefaultEndpointUrl(
                    client_class.BASE_URL)
                if url_version is None:
                    base_url += api_version + u'/'

        parser_collection = parser.collection_info.full_name
        if not parser_collection.startswith(collection):
            raise InvalidCollectionException(parser_collection)
        subcollection = ''
        if len(parser_collection) != len(collection):
            subcollection = parser_collection[len(collection) + 1:]
        return parser.ParseCollectionPath(collection_path, kwargs, resolve,
                                          base_url, subcollection)
Пример #3
0
def _GetApiBaseUrl(api_name, api_version):
    """Determine base url to use for resources of given version."""
    # Use current override endpoint for this resource name.
    endpoint_override_property = getattr(
        properties.VALUES.api_endpoint_overrides, api_name, None)
    base_url = None
    if endpoint_override_property is not None:
        base_url = endpoint_override_property.Get()
        if base_url is not None:
            # Check base url style. If it includes api version then override
            # also replaces the version, otherwise it only overrides the domain.
            client_class = core_apis.GetClientClass(api_name, api_version)
            _, url_version, _ = resource_util.SplitDefaultEndpointUrl(
                client_class.BASE_URL)
            if url_version is None:
                base_url += api_version + u'/'
    return base_url
Пример #4
0
 def GetClientClass(cls):
   return apis.GetClientClass('bio', 'v1')
Пример #5
0
def GetClientClass():
    return apis.GetClientClass(_API_NAME, _API_VERSION)