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: str, The API version, None for the default version. 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] # pylint:disable=protected-access api_version = apis_internal._GetDefaultVersion(api_name) # pylint:disable=protected-access for collection in apis_internal._GetApiCollections(api_name, api_version): self._RegisterCollection(collection) self.registered_apis[api_name].append(api_version) return api_version
def testCanImportEveryModule(self): for api_name in apis_internal._GetApiNames(): for api_version in apis_internal._GetVersions(api_name): collections = list( apis_internal._GetApiCollections(api_name, api_version)) for collection in collections: self.assertEqual(api_name, collection.api_name)
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] # pylint:disable=protected-access api_version = apis_internal._GetDefaultVersion(api_name) # pylint:disable=protected-access for collection in apis_internal._GetApiCollections(api_name, api_version): self._RegisterCollection(collection) self.registered_apis[api_name].append(api_version) return api_version
def GetAPICollections(api_name, api_version): """Gets the registered collections for the given API version. Args: api_name: str, The name of the API. api_version: str, The version string of the API. Returns: [APICollection], A list of the registered collections. """ # pylint:disable=protected-access return [ APICollection(c) for c in apis_internal._GetApiCollections(api_name, api_version) ]
def get_collection_names(api_name, api_versions): """Gets collection names for all collections in each specified version. Args: api_name: Name of the API to return collection names for. api_versions: Desired api versions to return collections for. Returns: collection_names: Names of every registered apitools collection. """ collection_names = set() for version in api_versions: resource_collections = [ registry.APICollection(c) for c in apis_internal._GetApiCollections(api_name, version) # pylint:disable=protected-access ] for resource_collection in resource_collections: collection_names.add(resource_collection.name) return collection_names
def GetAPICollections(api_name=None, api_version=None): """Gets the registered collections for the given API version. Args: api_name: str, The name of the API or None for all apis. api_version: str, The version string of the API or None to use the default version. Returns: [APICollection], A list of the registered collections. """ if api_name: all_apis = {api_name: _ValidateAndGetDefaultVersion(api_name, api_version)} else: all_apis = {x.name: x.version for x in GetAllAPIs() if x.is_default} collections = [] for n, v in all_apis.iteritems(): # pylint:disable=protected-access collections.extend( [APICollection(c) for c in apis_internal._GetApiCollections(n, v)]) return collections
def GetAPICollections(api_name=None, api_version=None): """Gets the registered collections for the given API version. Args: api_name: str, The name of the API or None for all apis. api_version: str, The version string of the API or None to use the default version. Returns: [APICollection], A list of the registered collections. """ if api_name: all_apis = {api_name: _ValidateAndGetDefaultVersion(api_name, api_version)} else: all_apis = {x.name: x.version for x in GetAllAPIs() if x.is_default} collections = [] for n, v in six.iteritems(all_apis): # pylint:disable=protected-access collections.extend( [APICollection(c) for c in apis_internal._GetApiCollections(n, v)]) return collections
def get_collection_to_apis_dict(self, api_name, api_versions): """Gets collection names for all collections in all versions of an api. Args: api_name: Name of the api to be added. api_versions: All registered versions of the api. Returns: collction_names: Names of every registered apitools collection. """ collection_to_apis_dict = {} for version in api_versions: resource_collections = [ registry.APICollection(c) for c in apis_internal._GetApiCollections(api_name, version) # pylint:disable=protected-access ] for resource_collection in resource_collections: if resource_collection.name in collection_to_apis_dict: collection_to_apis_dict[resource_collection.name].append(version) else: collection_to_apis_dict[resource_collection.name] = [version] return collection_to_apis_dict