Skip to content

Commit b21ae24

Browse files
committed
Add support for using static discovery documents in the package
1 parent af918e8 commit b21ae24

File tree

2 files changed

+66
-17
lines changed

2 files changed

+66
-17
lines changed

googleapiclient/discovery.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,13 @@ def _discovery_service_uri_options(discoveryServiceUrl, version):
330330

331331

332332
def _retrieve_discovery_doc(
333-
url, http, cache_discovery, cache=None, developerKey=None, num_retries=1
333+
url,
334+
http,
335+
cache_discovery,
336+
cache=None,
337+
developerKey=None,
338+
num_retries=1,
339+
static_discovery=True
334340
):
335341
"""Retrieves the discovery_doc from cache or the internet.
336342
@@ -345,6 +351,9 @@ def _retrieve_discovery_doc(
345351
from the API Console.
346352
num_retries: Integer, number of times to retry discovery with
347353
randomized exponential backoff in case of intermittent/connection issues.
354+
static_discovery: Boolean, whether or not to use the static discovery docs
355+
included in the package when the discovery doc is not available in the
356+
cache.
348357
349358
Returns:
350359
A unicode string representation of the discovery document.
@@ -359,21 +368,29 @@ def _retrieve_discovery_doc(
359368
if content:
360369
return content
361370

362-
actual_url = url
363-
# REMOTE_ADDR is defined by the CGI spec [RFC3875] as the environment
364-
# variable that contains the network address of the client sending the
365-
# request. If it exists then add that to the request for the discovery
366-
# document to avoid exceeding the quota on discovery requests.
367-
if "REMOTE_ADDR" in os.environ:
368-
actual_url = _add_query_parameter(url, "userIp", os.environ["REMOTE_ADDR"])
369-
if developerKey:
370-
actual_url = _add_query_parameter(url, "key", developerKey)
371-
logger.debug("URL being requested: GET %s", actual_url)
372-
373-
# Execute this request with retries build into HttpRequest
374-
# Note that it will already raise an error if we don't get a 2xx response
375-
req = HttpRequest(http, HttpRequest.null_postproc, actual_url)
376-
resp, content = req.execute(num_retries=num_retries)
371+
# At this point, the discovery document was not found in the cache so
372+
# we can attempt to retreive the static discovery document from the library.
373+
if static_discovery:
374+
content = discovery_cache.get_static_doc(url)
375+
376+
# If the content is None, retrieve the discovery doc from the internet
377+
# because it is not in the cache or the static doc directory.
378+
if content is None:
379+
actual_url = url
380+
# REMOTE_ADDR is defined by the CGI spec [RFC3875] as the environment
381+
# variable that contains the network address of the client sending the
382+
# request. If it exists then add that to the request for the discovery
383+
# document to avoid exceeding the quota on discovery requests.
384+
if "REMOTE_ADDR" in os.environ:
385+
actual_url = _add_query_parameter(url, "userIp", os.environ["REMOTE_ADDR"])
386+
if developerKey:
387+
actual_url = _add_query_parameter(url, "key", developerKey)
388+
logger.debug("URL being requested: GET %s", actual_url)
389+
390+
# Execute this request with retries build into HttpRequest
391+
# Note that it will already raise an error if we don't get a 2xx response
392+
req = HttpRequest(http, HttpRequest.null_postproc, actual_url)
393+
resp, content = req.execute(num_retries=num_retries)
377394

378395
try:
379396
content = content.decode("utf-8")

googleapiclient/discovery_cache/__init__.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
LOGGER = logging.getLogger(__name__)
2424

2525
DISCOVERY_DOC_MAX_AGE = 60 * 60 * 24 # 1 day
26-
26+
DISCOVERY_DOC_DIR = os.path.join(os.path.dirname(
27+
os.path.realpath(__file__)), 'documents')
2728

2829
def autodetect():
2930
"""Detects an appropriate cache module and returns it.
@@ -47,3 +48,34 @@ def autodetect():
4748
except Exception as e:
4849
LOGGER.warning(e, exc_info=True)
4950
return None
51+
52+
def get_static_doc(uri):
53+
"""Retrieves the discovery document from the directory defined in
54+
DISCOVERY_DOC_STATIC_DIR corresponding to the uri provided.
55+
56+
Args:
57+
uri: string, The URI of the discovery document in the format
58+
https://{domain}/discovery/{discoveryVer}/apis/{api}/{apiVersion}/rest
59+
60+
Returns:
61+
A string containing the contents of the JSON discovery document,
62+
otherwise None if the JSON discovery document was not found.
63+
"""
64+
65+
doc_name = None
66+
67+
# Extract the {apiVersion} and {api} from the uri which are the 2nd and 3rd
68+
# last parts of the uri respectively.
69+
# https://www.googleapis.com/discovery/v1/apis/{api}/{apiVersion}/rest
70+
uri_parts = uri.split('/')
71+
if len(uri_parts) > 3:
72+
doc_name = "{}.{}.json".format(uri_parts[-3], uri_parts[-2])
73+
74+
try:
75+
with open(os.path.join(DISCOVERY_DOC_DIR, doc_name), 'r') as f:
76+
content = f.read()
77+
except FileNotFoundError:
78+
content = None
79+
80+
return content
81+

0 commit comments

Comments
 (0)