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 🦕
diff --git a/UPGRADING.md b/UPGRADING.md
index 67143a6..9ac0088 100644
--- a/UPGRADING.md
+++ b/UPGRADING.md
@@ -28,7 +28,10 @@
Only discovery documents listed in [this public directory](https://www.googleapis.com/discovery/v1/apis/)
are included in the library. Users of private APIs should set the
`static_discovery` argument of `discovery.build()` to `False` to continue to
-retrieve the service definition from the internet.
+retrieve the service definition from the internet. As of version 2.1.0,
+for backwards compatability with version 1.x, if `static_discovery` is not
+specified, the default value for `static_discovery` will be `True` when
+the `discoveryServiceUrl` argument of `discovery.build()` is provided.
If you experience issues or have questions, please file an [issue](https://github.com/googleapis/google-api-python-client/issues).
diff --git a/googleapiclient/discovery.py b/googleapiclient/discovery.py
index a81ceca..4281ebb 100644
--- a/googleapiclient/discovery.py
+++ b/googleapiclient/discovery.py
@@ -182,7 +182,7 @@
serviceName,
version,
http=None,
- discoveryServiceUrl=DISCOVERY_URI,
+ discoveryServiceUrl=None,
developerKey=None,
model=None,
requestBuilder=HttpRequest,
@@ -193,7 +193,7 @@
adc_cert_path=None,
adc_key_path=None,
num_retries=1,
- static_discovery=True,
+ static_discovery=None,
):
"""Construct a Resource for interacting with an API.
@@ -248,7 +248,10 @@
num_retries: Integer, number of times to retry discovery with
randomized exponential backoff in case of intermittent/connection issues.
static_discovery: Boolean, whether or not to use the static discovery docs
- included in the library.
+ included in the library. The default value for `static_discovery` depends
+ on the value of `discoveryServiceUrl`. `static_discovery` will default to
+ `True` when `discoveryServiceUrl` is also not provided, otherwise it will
+ default to `False`.
Returns:
A Resource object with methods for interacting with the service.
@@ -259,6 +262,21 @@
"""
params = {"api": serviceName, "apiVersion": version}
+ # The default value for `static_discovery` depends on the value of
+ # `discoveryServiceUrl`. `static_discovery` will default to `True` when
+ # `discoveryServiceUrl` is also not provided, otherwise it will default to
+ # `False`. This is added for backwards compatability with
+ # google-api-python-client 1.x which does not support the `static_discovery`
+ # parameter.
+ if static_discovery is None:
+ if discoveryServiceUrl is None:
+ static_discovery = True
+ else:
+ static_discovery = False
+
+ if discoveryServiceUrl is None:
+ discoveryServiceUrl = DISCOVERY_URI
+
if http is None:
discovery_http = build_http()
else:
diff --git a/tests/test_discovery.py b/tests/test_discovery.py
index fbb6475..43ab2c7 100644
--- a/tests/test_discovery.py
+++ b/tests/test_discovery.py
@@ -592,7 +592,7 @@
def test_resource_close(self):
discovery = read_datafile("plus.json")
-
+
with mock.patch("httplib2.Http", autospec=True) as httplib2_http:
http = httplib2_http()
plus = build_from_document(
@@ -927,8 +927,7 @@
"v1",
http=http,
developerKey=None,
- discoveryServiceUrl="http://example.com",
- static_discovery=False,
+ discoveryServiceUrl="http://example.com"
)
self.fail("Should have raised an exception.")
except HttpError as e:
@@ -946,8 +945,7 @@
"v1",
http=http,
developerKey=None,
- discoveryServiceUrl="http://example.com",
- static_discovery=False,
+ discoveryServiceUrl="http://example.com"
)
self.fail("Should have raised an exception.")
except HttpError as e: