Repository: googleapis/python-api-core
Pull request #379 (2022-05-14 04:10:42)

feat: adds support for audience in client_options
Adds support for `audience` in client_options.

atulep: @viacheslav-rostovtsev FYI. This is needed to make `audience` configurable in the downstream GAPIC client.
atulep: @viacheslav-rostovtsev What can be a good documentation for `audience`?
viacheslav-rostovtsev: - let's call the parameter `api_audience` and not just `audience` because it looks like in the TPC work we might get other type of audience as well.
- The documentation should be something like: 'The intended audience for the API calls to the service that will be set when using certain 3rd party authentication flows. Audience is typically a resource identifier. If not set, the service endpoint value will be used as a default.'
- Good examples of API audience are google service endpoints like `https://language.googleapis.com`
atulep: @viacheslav-rostovtsev PTAL again

Patch:
diff --git a/google/api_core/client_options.py b/google/api_core/client_options.py
index 4a2a84a..ee9f28a 100644
--- a/google/api_core/client_options.py
+++ b/google/api_core/client_options.py
@@ -70,6 +70,11 @@ class ClientOptions(object):
         scopes (Optional[Sequence[str]]): OAuth access token override scopes.
         api_key (Optional[str]): Google API key. ``credentials_file`` and
             ``api_key`` are mutually exclusive.
+        api_audience (Optional[str]): The intended audience for the API calls
+            to the service that will be set when using certain 3rd party
+            authentication flows. Audience is typically a resource identifier.
+            If not set, the service endpoint value will be used as a default.
+            An example of a valid ``api_audience`` is: "https://language.googleapis.com".
 
     Raises:
         ValueError: If both ``client_cert_source`` and ``client_encrypted_cert_source``
@@ -85,6 +90,7 @@ class ClientOptions(object):
         credentials_file=None,
         scopes=None,
         api_key=None,
+        api_audience=None,
     ):
         if client_cert_source and client_encrypted_cert_source:
             raise ValueError(
@@ -99,6 +105,7 @@ class ClientOptions(object):
         self.credentials_file = credentials_file
         self.scopes = scopes
         self.api_key = api_key
+        self.api_audience = api_audience
 
     def __repr__(self):
         return "ClientOptions: " + repr(self.__dict__)


Test patch:
diff --git a/tests/unit/test_client_options.py b/tests/unit/test_client_options.py
index 334b8c1..d56a1b3 100644
--- a/tests/unit/test_client_options.py
+++ b/tests/unit/test_client_options.py
@@ -36,6 +36,7 @@ def test_constructor():
             "https://www.googleapis.com/auth/cloud-platform",
             "https://www.googleapis.com/auth/cloud-platform.read-only",
         ],
+        api_audience="foo2.googleapis.com",
     )
 
     assert options.api_endpoint == "foo.googleapis.com"
@@ -46,6 +47,7 @@ def test_constructor():
         "https://www.googleapis.com/auth/cloud-platform",
         "https://www.googleapis.com/auth/cloud-platform.read-only",
     ]
+    assert options.api_audience == "foo2.googleapis.com"
 
 
 def test_constructor_with_encrypted_cert_source():
@@ -114,6 +116,7 @@ def test_from_dict():
                 "https://www.googleapis.com/auth/cloud-platform",
                 "https://www.googleapis.com/auth/cloud-platform.read-only",
             ],
+            "api_audience": "foo2.googleapis.com",
         }
     )
 
@@ -126,6 +129,7 @@ def test_from_dict():
         "https://www.googleapis.com/auth/cloud-platform.read-only",
     ]
     assert options.api_key is None
+    assert options.api_audience == "foo2.googleapis.com"
 
 
 def test_from_dict_bad_argument():