Skip to content

Commit 3b4f2e2

Browse files
authored
fix: Change default of static_discovery when discoveryServiceUrl set (#1261)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [x] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/google-api-python-client/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) Fixes #1225 🦕
1 parent d1a255f commit 3b4f2e2

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

UPGRADING.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ discovery document is private and it will not be shipped with the library.
2828
Only discovery documents listed in [this public directory](https://www.googleapis.com/discovery/v1/apis/)
2929
are included in the library. Users of private APIs should set the
3030
`static_discovery` argument of `discovery.build()` to `False` to continue to
31-
retrieve the service definition from the internet.
31+
retrieve the service definition from the internet. As of version 2.1.0,
32+
for backwards compatability with version 1.x, if `static_discovery` is not
33+
specified, the default value for `static_discovery` will be `True` when
34+
the `discoveryServiceUrl` argument of `discovery.build()` is provided.
3235

3336
If you experience issues or have questions, please file an [issue](https://github.com/googleapis/google-api-python-client/issues).
3437

googleapiclient/discovery.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def build(
182182
serviceName,
183183
version,
184184
http=None,
185-
discoveryServiceUrl=DISCOVERY_URI,
185+
discoveryServiceUrl=None,
186186
developerKey=None,
187187
model=None,
188188
requestBuilder=HttpRequest,
@@ -193,7 +193,7 @@ def build(
193193
adc_cert_path=None,
194194
adc_key_path=None,
195195
num_retries=1,
196-
static_discovery=True,
196+
static_discovery=None,
197197
):
198198
"""Construct a Resource for interacting with an API.
199199
@@ -248,7 +248,10 @@ 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 library.
251+
included in the library. The default value for `static_discovery` depends
252+
on the value of `discoveryServiceUrl`. `static_discovery` will default to
253+
`True` when `discoveryServiceUrl` is also not provided, otherwise it will
254+
default to `False`.
252255
253256
Returns:
254257
A Resource object with methods for interacting with the service.
@@ -259,6 +262,21 @@ def build(
259262
"""
260263
params = {"api": serviceName, "apiVersion": version}
261264

265+
# The default value for `static_discovery` depends on the value of
266+
# `discoveryServiceUrl`. `static_discovery` will default to `True` when
267+
# `discoveryServiceUrl` is also not provided, otherwise it will default to
268+
# `False`. This is added for backwards compatability with
269+
# google-api-python-client 1.x which does not support the `static_discovery`
270+
# parameter.
271+
if static_discovery is None:
272+
if discoveryServiceUrl is None:
273+
static_discovery = True
274+
else:
275+
static_discovery = False
276+
277+
if discoveryServiceUrl is None:
278+
discoveryServiceUrl = DISCOVERY_URI
279+
262280
if http is None:
263281
discovery_http = build_http()
264282
else:

tests/test_discovery.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ def test_building_with_context_manager(self):
592592

593593
def test_resource_close(self):
594594
discovery = read_datafile("plus.json")
595-
595+
596596
with mock.patch("httplib2.Http", autospec=True) as httplib2_http:
597597
http = httplib2_http()
598598
plus = build_from_document(
@@ -927,8 +927,7 @@ def test_userip_is_added_to_discovery_uri(self):
927927
"v1",
928928
http=http,
929929
developerKey=None,
930-
discoveryServiceUrl="http://example.com",
931-
static_discovery=False,
930+
discoveryServiceUrl="http://example.com"
932931
)
933932
self.fail("Should have raised an exception.")
934933
except HttpError as e:
@@ -946,8 +945,7 @@ def test_userip_missing_is_not_added_to_discovery_uri(self):
946945
"v1",
947946
http=http,
948947
developerKey=None,
949-
discoveryServiceUrl="http://example.com",
950-
static_discovery=False,
948+
discoveryServiceUrl="http://example.com"
951949
)
952950
self.fail("Should have raised an exception.")
953951
except HttpError as e:

0 commit comments

Comments
 (0)