chore: Add X-Goog-API-Client header to GDA API requests

PiperOrigin-RevId: 869372484
This commit is contained in:
Google Team Member
2026-02-12 13:57:35 -08:00
committed by Copybara-Service
parent c6b1c74321
commit d85932faa3
4 changed files with 26 additions and 2 deletions
@@ -25,6 +25,8 @@ import requests
from . import client
from .config import BigQueryToolConfig
_GDA_CLIENT_ID = "GOOGLE_ADK"
def ask_data_insights(
project_id: str,
@@ -129,6 +131,7 @@ def ask_data_insights(
headers = {
"Authorization": f"Bearer {credentials.token}",
"Content-Type": "application/json",
"X-Goog-API-Client": _GDA_CLIENT_ID,
}
ca_url = f"https://geminidataanalytics.googleapis.com/v1alpha/projects/{project_id}/locations/{location}:chat"
@@ -149,7 +152,7 @@ def ask_data_insights(
"systemInstruction": instructions,
"options": {"chart": {"image": {"noImage": {}}}},
},
"clientIdEnum": "GOOGLE_ADK",
"clientIdEnum": _GDA_CLIENT_ID,
}
resp = _get_stream(
@@ -23,6 +23,7 @@ from ..tool_context import ToolContext
from .config import DataAgentToolConfig
BASE_URL = "https://geminidataanalytics.googleapis.com/v1beta"
_GDA_CLIENT_ID = "GOOGLE_ADK"
def _get_http_headers(
@@ -41,6 +42,7 @@ def _get_http_headers(
return {
"Authorization": f"Bearer {credentials.token}",
"Content-Type": "application/json",
"X-Goog-API-Client": _GDA_CLIENT_ID,
}
@@ -294,7 +296,7 @@ def ask_data_agent(
"dataAgentContext": {
"dataAgent": data_agent_name,
},
"clientIdEnum": "GOOGLE_ADK",
"clientIdEnum": _GDA_CLIENT_ID,
}
resp = _get_stream(
chat_url,
@@ -89,6 +89,12 @@ def test_ask_data_insights_success(mock_get_stream):
assert result["response"] == "Final formatted string from stream"
mock_get_stream.assert_called_once()
# Verify that the correct headers and client ID were passed to _get_stream
args, _ = mock_get_stream.call_args
headers = args[2]
assert headers["X-Goog-API-Client"] == "GOOGLE_ADK"
assert headers["Authorization"] == "Bearer fake-token"
@mock.patch.object(data_insights_tool, "_get_stream")
def test_ask_data_insights_handles_exception(mock_get_stream):
@@ -196,3 +196,16 @@ def test_get_stream_from_file(mock_post, case_file_path):
# 6. Assert that the final list of dicts matches the expected output
assert result == expected_final_list
def test_get_http_headers_includes_client_id():
"""Tests _get_http_headers includes the correct GDA client ID."""
mock_creds = mock.Mock()
mock_creds.token = "fake-token"
# pylint: disable=protected-access
headers = data_agent_tool._get_http_headers(mock_creds)
assert headers["X-Goog-API-Client"] == "GOOGLE_ADK"
assert headers["Content-Type"] == "application/json"
assert headers["Authorization"] == "Bearer fake-token"