|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""A2A Client for integration tests.""" |
| 16 | + |
| 17 | +from a2a.client.client import ClientConfig as A2AClientConfig |
| 18 | +from a2a.client.client_factory import ClientFactory as A2AClientFactory |
| 19 | +from a2a.extensions.common import HTTP_EXTENSION_HEADER |
| 20 | +from a2a.types import TransportProtocol as A2ATransport |
| 21 | +from google.adk.a2a.agent.interceptors.new_integration_extension import _NEW_A2A_ADK_INTEGRATION_EXTENSION |
| 22 | +from google.adk.agents.remote_a2a_agent import RemoteA2aAgent |
| 23 | +import httpx |
| 24 | + |
| 25 | +from .server import agent_card |
| 26 | + |
| 27 | + |
| 28 | +def create_client(app, streaming: bool = False) -> RemoteA2aAgent: |
| 29 | + """Creates a RemoteA2aAgent connected to the provided FastAPI app. |
| 30 | +
|
| 31 | + Args: |
| 32 | + app: The FastAPI application (server) to connect to. |
| 33 | + streaming: Whether to enable streaming mode in the client. |
| 34 | +
|
| 35 | + Returns: |
| 36 | + A RemoteA2aAgent instance. |
| 37 | + """ |
| 38 | + |
| 39 | + client = httpx.AsyncClient( |
| 40 | + transport=httpx.ASGITransport(app=app), base_url="http://test" |
| 41 | + ) |
| 42 | + |
| 43 | + client_config = A2AClientConfig( |
| 44 | + httpx_client=client, |
| 45 | + streaming=streaming, |
| 46 | + polling=False, |
| 47 | + supported_transports=[A2ATransport.jsonrpc], |
| 48 | + ) |
| 49 | + factory = A2AClientFactory(config=client_config) |
| 50 | + |
| 51 | + # use_legacy=False forces the new implementation |
| 52 | + agent = RemoteA2aAgent( |
| 53 | + name="remote_agent", |
| 54 | + agent_card=agent_card, |
| 55 | + a2a_client_factory=factory, |
| 56 | + use_legacy=False, |
| 57 | + ) |
| 58 | + |
| 59 | + return agent |
| 60 | + |
| 61 | + |
| 62 | +def create_a2a_client(app, streaming: bool = False): |
| 63 | + """Creates a bare A2A Client connected to the provided FastAPI app. |
| 64 | +
|
| 65 | + This is in contrast to create_client, which wraps the a2a_client into a |
| 66 | + RemoteA2aAgent for the standard runner framework ecosystem execution. |
| 67 | +
|
| 68 | + Args: |
| 69 | + app: The FastAPI application (server) to connect to. |
| 70 | + streaming: Whether to enable streaming mode in the client. |
| 71 | +
|
| 72 | + Returns: |
| 73 | + An A2A Client instance. |
| 74 | + """ |
| 75 | + client = httpx.AsyncClient( |
| 76 | + transport=httpx.ASGITransport(app=app), |
| 77 | + base_url="http://test", |
| 78 | + headers={HTTP_EXTENSION_HEADER: _NEW_A2A_ADK_INTEGRATION_EXTENSION}, |
| 79 | + ) |
| 80 | + |
| 81 | + client_config = A2AClientConfig( |
| 82 | + httpx_client=client, |
| 83 | + streaming=streaming, |
| 84 | + polling=False, |
| 85 | + supported_transports=[A2ATransport.jsonrpc], |
| 86 | + ) |
| 87 | + factory = A2AClientFactory(config=client_config) |
| 88 | + return factory.create(agent_card) |
0 commit comments