Skip to content

Commit 17828ea

Browse files
authored
fix: raise ValueError if api_endpoint is unset when using AnonymousCredentials in AsyncGrpcClient. (#1778)
Added validation to ensure `api_endpoint` is provided when using `AnonymousCredentials` in `AsyncGrpcClient`.
1 parent 8b7fbde commit 17828ea

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

google/cloud/storage/asyncio/async_grpc_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ def __init__(
5555
attempt_direct_path=True,
5656
):
5757
if isinstance(credentials, auth_credentials.AnonymousCredentials):
58+
if client_options is None or client_options.api_endpoint is None:
59+
raise ValueError(
60+
"Either client_options or `client_option.api_endpoint` is None. Please provide api_endpoint when `AnonymousCredentials` is used "
61+
)
5862
self._grpc_client = self._create_anonymous_client(
5963
client_options, credentials
6064
)

tests/unit/asyncio/test_async_grpc_client.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,23 @@ def test_grpc_client_with_anon_creds(
184184
transport = kwargs["transport"]
185185
assert isinstance(transport._credentials, AnonymousCredentials)
186186

187+
def test_grpc_client_with_anon_creds_no_client_options(self):
188+
# Act & Assert
189+
message = "Either client_options or `client_option.api_endpoint` is None. Please provide api_endpoint when `AnonymousCredentials` is used "
190+
with pytest.raises(ValueError, match=message):
191+
async_grpc_client.AsyncGrpcClient(
192+
credentials=AnonymousCredentials(),
193+
)
194+
195+
def test_grpc_client_with_anon_creds_empty_client_options(self):
196+
# Act & Assert
197+
message = "Either client_options or `client_option.api_endpoint` is None. Please provide api_endpoint when `AnonymousCredentials` is used "
198+
with pytest.raises(ValueError, match=message):
199+
async_grpc_client.AsyncGrpcClient(
200+
client_options=client_options.ClientOptions(),
201+
credentials=AnonymousCredentials(),
202+
)
203+
187204
@mock.patch("google.cloud._storage_v2.StorageAsyncClient")
188205
def test_user_agent_with_custom_client_info(self, mock_async_storage_client):
189206
"""Test that gcloud-python user agent is appended to existing user agent.

0 commit comments

Comments
 (0)