diff --git a/UPGRADING.md b/UPGRADING.md index 67143a622e3..9ac0088393c 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -28,7 +28,10 @@ discovery document is private and it will not be shipped with the library. 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 a81ceca90fe..4281ebb290e 100644 --- a/googleapiclient/discovery.py +++ b/googleapiclient/discovery.py @@ -182,7 +182,7 @@ def build( serviceName, version, http=None, - discoveryServiceUrl=DISCOVERY_URI, + discoveryServiceUrl=None, developerKey=None, model=None, requestBuilder=HttpRequest, @@ -193,7 +193,7 @@ def build( 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 @@ def build( 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 @@ def build( """ 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 fbb64752cf6..43ab2c79a8d 100644 --- a/tests/test_discovery.py +++ b/tests/test_discovery.py @@ -592,7 +592,7 @@ def test_building_with_context_manager(self): 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 @@ def test_userip_is_added_to_discovery_uri(self): "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 @@ def test_userip_missing_is_not_added_to_discovery_uri(self): "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: