Skip to content

Commit b8e8f6b

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: Add "google-adk" user agent to Parameter Manager and Secret Manager clients
Update unit tests to verify the presence and value of the client_info argument during client initialization. PiperOrigin-RevId: 900448054
1 parent bf84e2c commit b8e8f6b

File tree

4 files changed

+64
-32
lines changed

4 files changed

+64
-32
lines changed

src/google/adk/integrations/parameter_manager/parameter_client.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@
1818
from typing import cast
1919
from typing import Optional
2020

21+
from google.api_core.gapic_v1 import client_info
2122
import google.auth
2223
from google.auth import default as default_service_credential
2324
import google.auth.transport.requests
2425
from google.cloud import parametermanager_v1
2526
from google.oauth2 import service_account
2627

28+
from ... import version
29+
30+
USER_AGENT = f"google-adk/{version.__version__}"
31+
2732

2833
class ParameterManagerClient:
2934
"""A client for interacting with Google Cloud Parameter Manager.
@@ -112,7 +117,9 @@ def __init__(
112117
}
113118

114119
self._client = parametermanager_v1.ParameterManagerClient(
115-
credentials=self._credentials, client_options=client_options
120+
credentials=self._credentials,
121+
client_options=client_options,
122+
client_info=client_info.ClientInfo(user_agent=USER_AGENT),
116123
)
117124

118125
def get_parameter(self, resource_name: str) -> str:

src/google/adk/integrations/secret_manager/secret_client.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@
1818
from typing import cast
1919
from typing import Optional
2020

21+
from google.api_core.gapic_v1 import client_info
2122
import google.auth
2223
from google.auth import default as default_service_credential
2324
import google.auth.transport.requests
2425
from google.cloud import secretmanager
2526
from google.oauth2 import service_account
2627

28+
from ... import version
29+
30+
USER_AGENT = f"google-adk/{version.__version__}"
31+
2732

2833
class SecretManagerClient:
2934
"""A client for interacting with Google Cloud Secret Manager.
@@ -103,7 +108,9 @@ def __init__(
103108
}
104109

105110
self._client = secretmanager.SecretManagerServiceClient(
106-
credentials=self._credentials, client_options=client_options
111+
credentials=self._credentials,
112+
client_options=client_options,
113+
client_info=client_info.ClientInfo(user_agent=USER_AGENT),
107114
)
108115

109116
def get_secret(self, resource_name: str) -> str:

tests/unittests/integrations/parameter_manager/test_parameter_client.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from unittest.mock import patch
2020

2121
from google.adk.integrations.parameter_manager.parameter_client import ParameterManagerClient
22+
from google.adk.integrations.parameter_manager.parameter_client import USER_AGENT
23+
from google.api_core.gapic_v1 import client_info
2224
import pytest
2325

2426

@@ -47,9 +49,11 @@ def test_init_with_default_credentials(
4749
mock_default_service_credential.assert_called_once_with(
4850
scopes=["https://www.googleapis.com/auth/cloud-platform"]
4951
)
50-
mock_pm_client_class.assert_called_once_with(
51-
credentials=mock_credentials, client_options=None
52-
)
52+
mock_pm_client_class.assert_called_once()
53+
call_kwargs = mock_pm_client_class.call_args.kwargs
54+
assert call_kwargs["credentials"] == mock_credentials
55+
assert call_kwargs["client_options"] is None
56+
assert call_kwargs["client_info"].user_agent == USER_AGENT
5357
assert client._credentials == mock_credentials
5458
assert client._client == mock_pm_client_class.return_value
5559

@@ -77,9 +81,11 @@ def test_init_with_service_account_json(
7781
mock_from_service_account_info.assert_called_once_with(
7882
json.loads(service_account_json)
7983
)
80-
mock_pm_client_class.assert_called_once_with(
81-
credentials=mock_credentials, client_options=None
82-
)
84+
mock_pm_client_class.assert_called_once()
85+
call_kwargs = mock_pm_client_class.call_args.kwargs
86+
assert call_kwargs["credentials"] == mock_credentials
87+
assert call_kwargs["client_options"] is None
88+
assert call_kwargs["client_info"].user_agent == USER_AGENT
8389
assert client._credentials == mock_credentials
8490
assert client._client == mock_pm_client_class.return_value
8591

@@ -101,9 +107,11 @@ def test_init_with_auth_token(self, mock_pm_client_class):
101107

102108
# Verify
103109
mock_credentials.refresh.assert_called_once()
104-
mock_pm_client_class.assert_called_once_with(
105-
credentials=mock_credentials, client_options=None
106-
)
110+
mock_pm_client_class.assert_called_once()
111+
call_kwargs = mock_pm_client_class.call_args.kwargs
112+
assert call_kwargs["credentials"] == mock_credentials
113+
assert call_kwargs["client_options"] is None
114+
assert call_kwargs["client_info"].user_agent == USER_AGENT
107115
assert client._credentials == mock_credentials
108116
assert client._client == mock_pm_client_class.return_value
109117

@@ -127,12 +135,13 @@ def test_init_with_location(
127135
ParameterManagerClient(location=location)
128136

129137
# Verify
130-
mock_pm_client_class.assert_called_once_with(
131-
credentials=mock_credentials,
132-
client_options={
133-
"api_endpoint": f"parametermanager.{location}.rep.googleapis.com"
134-
},
135-
)
138+
mock_pm_client_class.assert_called_once()
139+
call_kwargs = mock_pm_client_class.call_args.kwargs
140+
assert call_kwargs["credentials"] == mock_credentials
141+
assert call_kwargs["client_options"] == {
142+
"api_endpoint": f"parametermanager.{location}.rep.googleapis.com"
143+
}
144+
assert call_kwargs["client_info"].user_agent == USER_AGENT
136145

137146
@patch(
138147
"google.adk.integrations.parameter_manager.parameter_client.default_service_credential"

tests/unittests/integrations/secret_manager/test_secret_client.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from unittest.mock import patch
2020

2121
from google.adk.integrations.secret_manager.secret_client import SecretManagerClient
22+
from google.adk.integrations.secret_manager.secret_client import USER_AGENT
23+
from google.api_core.gapic_v1 import client_info
2224
import pytest
2325

2426
import google
@@ -49,9 +51,11 @@ def test_init_with_default_credentials(
4951
mock_default_service_credential.assert_called_once_with(
5052
scopes=["https://www.googleapis.com/auth/cloud-platform"]
5153
)
52-
mock_secret_manager_client.assert_called_once_with(
53-
credentials=mock_credentials, client_options=None
54-
)
54+
mock_secret_manager_client.assert_called_once()
55+
call_kwargs = mock_secret_manager_client.call_args.kwargs
56+
assert call_kwargs["credentials"] == mock_credentials
57+
assert call_kwargs["client_options"] is None
58+
assert call_kwargs["client_info"].user_agent == USER_AGENT
5559
assert client._credentials == mock_credentials
5660
assert client._client == mock_secret_manager_client.return_value
5761

@@ -79,9 +83,11 @@ def test_init_with_service_account_json(
7983
mock_from_service_account_info.assert_called_once_with(
8084
json.loads(service_account_json)
8185
)
82-
mock_secret_manager_client.assert_called_once_with(
83-
credentials=mock_credentials, client_options=None
84-
)
86+
mock_secret_manager_client.assert_called_once()
87+
call_kwargs = mock_secret_manager_client.call_args.kwargs
88+
assert call_kwargs["credentials"] == mock_credentials
89+
assert call_kwargs["client_options"] is None
90+
assert call_kwargs["client_info"].user_agent == USER_AGENT
8591
assert client._credentials == mock_credentials
8692
assert client._client == mock_secret_manager_client.return_value
8793

@@ -105,9 +111,11 @@ def test_init_with_auth_token(self, mock_secret_manager_client):
105111

106112
# Verify
107113
mock_credentials.refresh.assert_called_once()
108-
mock_secret_manager_client.assert_called_once_with(
109-
credentials=mock_credentials, client_options=None
110-
)
114+
mock_secret_manager_client.assert_called_once()
115+
call_kwargs = mock_secret_manager_client.call_args.kwargs
116+
assert call_kwargs["credentials"] == mock_credentials
117+
assert call_kwargs["client_options"] is None
118+
assert call_kwargs["client_info"].user_agent == USER_AGENT
111119
assert client._credentials == mock_credentials
112120
assert client._client == mock_secret_manager_client.return_value
113121

@@ -131,12 +139,13 @@ def test_init_with_location(
131139
SecretManagerClient(location=location)
132140

133141
# Verify
134-
mock_secret_manager_client.assert_called_once_with(
135-
credentials=mock_credentials,
136-
client_options={
137-
"api_endpoint": f"secretmanager.{location}.rep.googleapis.com"
138-
},
139-
)
142+
mock_secret_manager_client.assert_called_once()
143+
call_kwargs = mock_secret_manager_client.call_args.kwargs
144+
assert call_kwargs["credentials"] == mock_credentials
145+
assert call_kwargs["client_options"] == {
146+
"api_endpoint": f"secretmanager.{location}.rep.googleapis.com"
147+
}
148+
assert call_kwargs["client_info"].user_agent == USER_AGENT
140149

141150
@patch(
142151
"google.adk.integrations.secret_manager.secret_client.default_service_credential"

0 commit comments

Comments
 (0)