Skip to content

Commit

Permalink
Use serviceName and version from build()
Browse files Browse the repository at this point in the history
  • Loading branch information
parthea committed Dec 11, 2020
1 parent f2772a6 commit a984d80
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
8 changes: 7 additions & 1 deletion googleapiclient/discovery.py
Expand Up @@ -275,6 +275,8 @@ def build(
requested_url,
discovery_http,
cache_discovery,
serviceName,
version,
cache,
developerKey,
num_retries=num_retries,
Expand Down Expand Up @@ -338,6 +340,8 @@ def _retrieve_discovery_doc(
url,
http,
cache_discovery,
serviceName,
version,
cache=None,
developerKey=None,
num_retries=1,
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
27 changes: 10 additions & 17 deletions googleapiclient/discovery_cache/__init__.py
Expand Up @@ -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

0 comments on commit a984d80

Please sign in to comment.