Skip to content

Commit f816508

Browse files
committed
Only request docs if static_discovery=False
1 parent a8b116a commit f816508

File tree

3 files changed

+26
-41
lines changed

3 files changed

+26
-41
lines changed

docs/start.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,7 @@ with build('drive', 'v3') as service:
6565
# ...
6666
```
6767

68-
**Note**: Under the hood, the `build()` function retrieves a discovery artifact in order to construct the service object. If the `cache_discovery` argument of `build()` is set to `True`, the library will attempt to retrieve the discovery artifact from the legacy cache which is only supported with `oauth2client<4.0`. If the artifact is not available in the legacy cache and the `static_discovery` argument of `build()` is set to `True`, the library will attempt to retieve the discovery artifact from the static copy of the discovery artifacts that are shipped with the library. As a last resort if the discovery artifact is not on disk, the discovery artifact will be retrieved from the internet. The maintainers recommend keeping `static_discovery=True`, which is the default, in order to avoid reliability issues related to transient network errors. The library currently does not detect breaking changes of discovery artifacts, and as a result, user code is not guaranteed to continue to succeed or fail with different versions of `google-api-python-client`. Users can set `static_discovery=False` as workaround if there is an incompatible discovery artifact shipped with the library. In addition, users may roll back the version of `google-api-python-client` if there is a breaking change in a discovery artifact. The maintainers of `google-api-python-client` encourage users to report issues with discovery artifacts so that the discovery artifacts can be corrected.
69-
70-
In the below example, the discovery artifact for `'drive'` version `'v3'` will only be retrieved from the internet if it is not available in the legacy cache and it is not one of the static discovery artifacts shipped with the library.
71-
72-
```python
73-
from googleapiclient.discovery import build
74-
75-
with build('drive', 'v3', cache_discovery=True, static_discovery=True):
76-
# ...
77-
```
68+
**Note**: Under the hood, the `build()` function retrieves a discovery artifact in order to construct the service object. If the `cache_discovery` argument of `build()` is set to `True`, the library will attempt to retrieve the discovery artifact from the legacy cache which is only supported with `oauth2client<4.0`. If the artifact is not available in the legacy cache and the `static_discovery` argument of `build()` is set to `True`, the library will attempt to retieve the discovery artifact from the static copy of the discovery artifacts that are shipped with the library. The maintainers recommend keeping `static_discovery=True`, which is the default, in order to avoid reliability issues related to transient network errors. The library currently does not detect breaking changes of discovery artifacts, and as a result, user code is not guaranteed to continue to succeed or fail with different versions of `google-api-python-client`. Users can set `static_discovery=False` as workaround if there is an incompatible discovery artifact shipped with the library. In addition, users may roll back the version of `google-api-python-client` if there is a breaking change in a discovery artifact. The maintainers of `google-api-python-client` encourage users to report issues with discovery artifacts so that the discovery artifacts can be corrected.
7869

7970
### Collections
8071

googleapiclient/discovery.py

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,7 @@ def build(
248248
num_retries: Integer, number of times to retry discovery with
249249
randomized exponential backoff in case of intermittent/connection issues.
250250
static_discovery: Boolean, whether or not to use the static discovery docs
251-
included in the package when the discovery doc is not available in the
252-
cache.
251+
included in the library.
253252
254253
Returns:
255254
A Resource object with methods for interacting with the service.
@@ -363,16 +362,13 @@ def _retrieve_discovery_doc(
363362
num_retries: Integer, number of times to retry discovery with
364363
randomized exponential backoff in case of intermittent/connection issues.
365364
static_discovery: Boolean, whether or not to use the static discovery docs
366-
included in the package when the discovery doc is not available in the
367-
cache.
365+
included in the library.
368366
369367
Returns:
370368
A unicode string representation of the discovery document.
371369
"""
372370
from . import discovery_cache
373371

374-
content = None
375-
376372
if cache_discovery:
377373
if cache is None:
378374
cache = discovery_cache.autodetect()
@@ -381,31 +377,30 @@ def _retrieve_discovery_doc(
381377
if content:
382378
return content
383379

384-
# At this point, the discovery document was not found in the cache so
385-
# we can attempt to retreive the static discovery document from the library.
380+
# When `static_discovery=True`, use static discovery artifacts included
381+
# with the library
386382
if static_discovery:
387383
content = discovery_cache.get_static_doc(serviceName, version)
388384
if content:
389385
return content
390-
391-
# If the content is None, retrieve the discovery doc from the internet
392-
# because it is not in the cache or the static doc directory.
393-
if content is None:
394-
actual_url = url
395-
# REMOTE_ADDR is defined by the CGI spec [RFC3875] as the environment
396-
# variable that contains the network address of the client sending the
397-
# request. If it exists then add that to the request for the discovery
398-
# document to avoid exceeding the quota on discovery requests.
399-
if "REMOTE_ADDR" in os.environ:
400-
actual_url = _add_query_parameter(url, "userIp", os.environ["REMOTE_ADDR"])
401-
if developerKey:
402-
actual_url = _add_query_parameter(url, "key", developerKey)
403-
logger.debug("URL being requested: GET %s", actual_url)
404-
405-
# Execute this request with retries build into HttpRequest
406-
# Note that it will already raise an error if we don't get a 2xx response
407-
req = HttpRequest(http, HttpRequest.null_postproc, actual_url)
408-
resp, content = req.execute(num_retries=num_retries)
386+
else:
387+
raise UnknownApiNameOrVersion("name: %s version: %s" % (serviceName, version))
388+
389+
actual_url = url
390+
# REMOTE_ADDR is defined by the CGI spec [RFC3875] as the environment
391+
# variable that contains the network address of the client sending the
392+
# request. If it exists then add that to the request for the discovery
393+
# document to avoid exceeding the quota on discovery requests.
394+
if "REMOTE_ADDR" in os.environ:
395+
actual_url = _add_query_parameter(url, "userIp", os.environ["REMOTE_ADDR"])
396+
if developerKey:
397+
actual_url = _add_query_parameter(url, "key", developerKey)
398+
logger.debug("URL being requested: GET %s", actual_url)
399+
400+
# Execute this request with retries build into HttpRequest
401+
# Note that it will already raise an error if we don't get a 2xx response
402+
req = HttpRequest(http, HttpRequest.null_postproc, actual_url)
403+
resp, content = req.execute(num_retries=num_retries)
409404

410405
try:
411406
content = content.decode("utf-8")

tests/test_discovery.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,10 +1162,9 @@ def test_retrieve_from_internet_when_static_discovery_false(self):
11621162
build("drive", "v3", http=http, cache_discovery=False,
11631163
static_discovery=False)
11641164

1165-
def test_retrieve_from_internet_when_static_doc_does_not_exist(self):
1166-
http = HttpMockSequence([({"status": "400"}, "")])
1167-
with self.assertRaises(HttpError):
1168-
build("doesnotexist", "v3", http=http, cache_discovery=False,
1165+
def test_unknown_api_when_static_discovery_true(self):
1166+
with self.assertRaises(UnknownApiNameOrVersion):
1167+
build("doesnotexist", "v3", cache_discovery=False,
11691168
static_discovery=True)
11701169

11711170

0 commit comments

Comments
 (0)