From 409ec4940905d1a635ccb4fb16111095c456e8fe Mon Sep 17 00:00:00 2001 From: arithmetic1728 Date: Fri, 6 Aug 2021 15:19:29 -0700 Subject: [PATCH 1/2] feat: add api_key to client options --- google/api_core/client_options.py | 9 +++++++- tests/unit/test_client_options.py | 36 +++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/google/api_core/client_options.py b/google/api_core/client_options.py index be5523df..4a2a84a4 100644 --- a/google/api_core/client_options.py +++ b/google/api_core/client_options.py @@ -66,11 +66,14 @@ class ClientOptions(object): quota_project_id (Optional[str]): A project name that a client's quota belongs to. credentials_file (Optional[str]): A path to a file storing credentials. + ``credentials_file` and ``api_key`` are mutually exclusive. scopes (Optional[Sequence[str]]): OAuth access token override scopes. + api_key (Optional[str]): Google API key. ``credentials_file`` and + ``api_key`` are mutually exclusive. Raises: ValueError: If both ``client_cert_source`` and ``client_encrypted_cert_source`` - are provided. + are provided, or both ``credentials_file`` and ``api_key`` are provided. """ def __init__( @@ -81,17 +84,21 @@ def __init__( quota_project_id=None, credentials_file=None, scopes=None, + api_key=None, ): if client_cert_source and client_encrypted_cert_source: raise ValueError( "client_cert_source and client_encrypted_cert_source are mutually exclusive" ) + if api_key and credentials_file: + raise ValueError("api_key and credentials_file are mutually exclusive") self.api_endpoint = api_endpoint self.client_cert_source = client_cert_source self.client_encrypted_cert_source = client_encrypted_cert_source self.quota_project_id = quota_project_id self.credentials_file = credentials_file self.scopes = scopes + self.api_key = api_key def __repr__(self): return "ClientOptions: " + repr(self.__dict__) diff --git a/tests/unit/test_client_options.py b/tests/unit/test_client_options.py index 38b9ad0a..0122e8ca 100644 --- a/tests/unit/test_client_options.py +++ b/tests/unit/test_client_options.py @@ -72,6 +72,36 @@ def test_constructor_with_both_cert_sources(): ) +def test_constructor_with_api_key(): + + options = client_options.ClientOptions( + api_endpoint="foo.googleapis.com", + client_cert_source=get_client_cert, + quota_project_id="quote-proj", + api_key="api-key", + scopes=[ + "https://www.googleapis.com/auth/cloud-platform", + "https://www.googleapis.com/auth/cloud-platform.read-only", + ], + ) + + assert options.api_endpoint == "foo.googleapis.com" + assert options.client_cert_source() == (b"cert", b"key") + assert options.quota_project_id == "quote-proj" + assert options.api_key == "api-key" + assert options.scopes == [ + "https://www.googleapis.com/auth/cloud-platform", + "https://www.googleapis.com/auth/cloud-platform.read-only", + ] + + +def test_constructor_with_both_api_key_and_credentials_file(): + with pytest.raises(ValueError): + client_options.ClientOptions( + api_key="api-key", credentials_file="path/to/credentials.json", + ) + + def test_from_dict(): options = client_options.from_dict( { @@ -94,6 +124,7 @@ def test_from_dict(): "https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/cloud-platform.read-only", ] + assert options.api_key is None def test_from_dict_bad_argument(): @@ -109,9 +140,10 @@ def test_from_dict_bad_argument(): def test_repr(): options = client_options.ClientOptions(api_endpoint="foo.googleapis.com") + print(repr(options)) assert ( repr(options) - == "ClientOptions: {'api_endpoint': 'foo.googleapis.com', 'client_cert_source': None, 'client_encrypted_cert_source': None}" - or "ClientOptions: {'client_encrypted_cert_source': None, 'client_cert_source': None, 'api_endpoint': 'foo.googleapis.com'}" + == "ClientOptions: {'api_endpoint': 'foo.googleapis.com', 'client_cert_source': None, 'client_encrypted_cert_source': None, 'api_key': None}" + or "ClientOptions: {'client_encrypted_cert_source': None, 'client_cert_source': None, 'api_endpoint': 'foo.googleapis.com', 'api_key': None}" ) From d90472856e5d08b72ca7fb769493add83e93bdc1 Mon Sep 17 00:00:00 2001 From: arithmetic1728 Date: Wed, 1 Sep 2021 11:00:25 -0700 Subject: [PATCH 2/2] update --- tests/unit/test_client_options.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/test_client_options.py b/tests/unit/test_client_options.py index 0122e8ca..fbff5457 100644 --- a/tests/unit/test_client_options.py +++ b/tests/unit/test_client_options.py @@ -140,7 +140,6 @@ def test_from_dict_bad_argument(): def test_repr(): options = client_options.ClientOptions(api_endpoint="foo.googleapis.com") - print(repr(options)) assert ( repr(options)