Example #1
0
    def RegisterApiByName(self, api_name, api_version=None):
        """Register the given API if it has not been registered already.

    Args:
      api_name: str, The API name.
      api_version: if available, the version of the API being registered.
    Returns:
      api version which was registered.
    """
        registered_versions = self.registered_apis.get(api_name, [])
        if api_version in registered_versions:
            # This API version has been registered.
            registered_versions.remove(api_version)
            registered_versions.append(api_version)
            return api_version
        if api_version is None:
            if registered_versions:
                # Use last registered api version as default.
                return registered_versions[-1]
            api_version = core_apis.GetDefaultVersion(api_name)

        for collection in core_apis.GetApiCollections(api_name, api_version):
            self._RegisterCollection(collection)

        self.registered_apis[api_name].append(api_version)
        return api_version
Example #2
0
    def _RegisterAPIByName(self, api_name, api_version=None):
        """Register the given API if it has not been registered already.

    Args:
      api_name: str, The API name.
      api_version: if available, the version of the API being registered.
    """
        if api_version and api_version in self.registered_apis.get(
                api_name, []):
            # This API version has been registered.
            return
        if not api_version and api_name in self.registered_apis:
            # This API doesn't have a specified version, and we have some API version
            # registered under this name.
            return

        # Register only URL's if not default version.
        if api_version is None:
            # Default version is used if no version is provided.
            urls_only = False
        else:
            default_version = core_apis.GetDefaultVersion(api_name)
            urls_only = api_version != default_version

        api_client = core_apis.GetClientInstance(api_name,
                                                 api_version,
                                                 no_http=True)
        self._RegisterAPI(api_client, urls_only, api_version)
Example #3
0
def _APINameAndVersionFromURL(url):
    """Get the API name and version from a resource url.

  Supports four formats:
  http(s)://www.googleapis.com/api/version/resource-path,
  http(s)://www-googleapis-staging.sandbox.google.com/api/version/resource-path,
  http(s)://api.googleapis.com/version/resource-path, and
  http(s)://someotherdoman/api/version/resource-path.

  If there is an api endpoint override defined that maches the url,
  that api name will be returned.

  Args:
    url: str, The resource url.

  Returns:
    (str, str): The API name. and version
  """
    endpoint_overrides = properties.VALUES.api_endpoint_overrides.AllValues()
    for name, overridden_url in endpoint_overrides.iteritems():
        if overridden_url == url:
            return name, core_apis.GetDefaultVersion(name)

    tokens = _StripUrl(url).split('/')
    domain = tokens[0]
    if ('googleapis' not in domain or domain.startswith('www.')
            or domain.startswith('www-')):
        api_name = tokens[1]
        if len(tokens) > 2:
            version = tokens[2]
        else:
            version = None
    else:
        api_name = tokens[0].split('.')[0]
        if len(tokens) > 1:
            version = tokens[1]
        else:
            version = core_apis.GetDefaultVersion(api_name)
    return api_name, version
Example #4
0
  def _RegisterAPIByName(self, api_name, api_version=None):
    """Register the given API if it has not been registered already.

    Args:
      api_name: str, The API name.
      api_version: if available, the version of the API being registered.
    Returns:
      api version which was registered.
    """
    registered_versions = self.registered_apis.get(api_name, set([]))
    if not api_version:
      if len(registered_versions) == 1:
        api_version = next(iter(registered_versions))
      else:
        api_version = core_apis.GetDefaultVersion(api_name)
    if api_version and api_version in registered_versions:
      # This API version has been registered.
      return api_version

    api_client = core_apis.GetClientInstance(api_name, api_version,
                                             no_http=True)
    self._RegisterAPI(api_name, api_version, api_client)
    return api_version
Example #5
0
def LoadApis():
    """Populate the global module namespace with API clients."""
    for api_name in apis_map.MAP:
        globals()[api_name] = apis.GetClientInstance(
            api_name, apis.GetDefaultVersion(api_name))