diff --git a/googleapiclient/discovery.py b/googleapiclient/discovery.py index 849eb28aeb9..8947bf14bda 100644 --- a/googleapiclient/discovery.py +++ b/googleapiclient/discovery.py @@ -275,6 +275,8 @@ def build( requested_url, discovery_http, cache_discovery, + serviceName, + version, cache, developerKey, num_retries=num_retries, @@ -338,6 +340,8 @@ def _retrieve_discovery_doc( url, http, cache_discovery, + serviceName, + version, cache=None, developerKey=None, num_retries=1, @@ -350,6 +354,8 @@ def _retrieve_discovery_doc( http: httplib2.Http, An instance of httplib2.Http or something that acts like it through which HTTP requests will be made. cache_discovery: Boolean, whether or not to cache the discovery doc. + serviceName: string, name of the service. + version: string, the version of the service. cache: googleapiclient.discovery_cache.base.Cache, an optional cache object for the discovery documents. developerKey: string, Key for controlling API usage, generated @@ -378,7 +384,7 @@ def _retrieve_discovery_doc( # At this point, the discovery document was not found in the cache so # we can attempt to retreive the static discovery document from the library. if static_discovery: - content = discovery_cache.get_static_doc(url) + content = discovery_cache.get_static_doc(serviceName, version) # If the content is None, retrieve the discovery doc from the internet # because it is not in the cache or the static doc directory. diff --git a/googleapiclient/discovery_cache/__init__.py b/googleapiclient/discovery_cache/__init__.py index 54227b27938..886bef3e539 100644 --- a/googleapiclient/discovery_cache/__init__.py +++ b/googleapiclient/discovery_cache/__init__.py @@ -50,35 +50,28 @@ def autodetect(): exc_info=False) return None -def get_static_doc(uri): +def get_static_doc(serviceName, version): """Retrieves the discovery document from the directory defined in DISCOVERY_DOC_STATIC_DIR corresponding to the uri provided. Args: - uri: string, The URI of the discovery document in the format - https://{domain}/discovery/{discoveryVer}/apis/{api}/{apiVersion}/rest + serviceName: string, name of the service. + version: string, the version of the service. Returns: A string containing the contents of the JSON discovery document, otherwise None if the JSON discovery document was not found. """ - doc_name = None content = None + doc_name = "{}.{}.json".format(serviceName, version) - # Extract the {apiVersion} and {api} from the uri which are the 2nd and 3rd - # last parts of the uri respectively. - # https://www.googleapis.com/discovery/v1/apis/{api}/{apiVersion}/rest - uri_parts = uri.split('/') - if len(uri_parts) > 3: - doc_name = "{}.{}.json".format(uri_parts[-3], uri_parts[-2]) - - try: - with open(os.path.join(DISCOVERY_DOC_DIR, doc_name), 'r') as f: - content = f.read() - except FileNotFoundError: - # File does not exist. Nothing to do here. - pass + try: + with open(os.path.join(DISCOVERY_DOC_DIR, doc_name), 'r') as f: + content = f.read() + except FileNotFoundError: + # File does not exist. Nothing to do here. + pass return content