11package io .a2a .spec ;
22
3- import static io .a2a .util .Utils .unmarshalFrom ;
4-
5- import java .io .IOException ;
63import java .util .Collections ;
74import java .util .Map ;
85
9- import com . fasterxml . jackson . core . type . TypeReference ;
6+ import io . a2a . client . A2ACardResolver ;
107import io .a2a .http .A2AHttpClient ;
11- import io .a2a .http .A2AHttpResponse ;
128import io .a2a .http .JdkA2AHttpClient ;
139
1410
@@ -27,13 +23,6 @@ public class A2A {
2723
2824 public static final String JSONRPC_VERSION = "2.0" ;
2925
30- public static final String AGENT_CARD_REQUEST = ".well-known/agent.json" ;
31-
32- private static final TypeReference <AgentCard > AGENT_CARD_TYPE_REFERENCE = new TypeReference <>() {};
33-
34- public static String getRequestEndpoint (String agentUrl , String request ) {
35- return agentUrl .endsWith ("/" ) ? agentUrl + request : agentUrl + "/" + request ;
36- }
3726
3827 /**
3928 * Convert the given text to a user message.
@@ -93,9 +82,10 @@ private static Message toMessage(String text, Message.Role role, String messageI
9382 *
9483 * @param agentUrl the base URL for the agent whose agent card we want to retrieve
9584 * @return the agent card
96- * @throws A2AServerException if the agent card cannot be retrieved for any reason
85+ * @throws A2AClientError If an HTTP error occurs fetching the card
86+ * @throws A2AClientJSONError f the response body cannot be decoded as JSON or validated against the AgentCard schema
9787 */
98- public static AgentCard getAgentCard (String agentUrl ) throws A2AServerException {
88+ public static AgentCard getAgentCard (String agentUrl ) throws A2AClientError , A2AClientJSONError {
9989 return getAgentCard (new JdkA2AHttpClient (), agentUrl );
10090 }
10191
@@ -105,9 +95,10 @@ public static AgentCard getAgentCard(String agentUrl) throws A2AServerException
10595 * @param httpClient the http client to use
10696 * @param agentUrl the base URL for the agent whose agent card we want to retrieve
10797 * @return the agent card
108- * @throws A2AServerException if the agent card cannot be retrieved for any reason
98+ * @throws A2AClientError If an HTTP error occurs fetching the card
99+ * @throws A2AClientJSONError f the response body cannot be decoded as JSON or validated against the AgentCard schema
109100 */
110- public static AgentCard getAgentCard (A2AHttpClient httpClient , String agentUrl ) throws A2AServerException {
101+ public static AgentCard getAgentCard (A2AHttpClient httpClient , String agentUrl ) throws A2AClientError , A2AClientJSONError {
111102 return getAgentCard (httpClient , agentUrl , null , null );
112103 }
113104
@@ -119,9 +110,10 @@ public static AgentCard getAgentCard(A2AHttpClient httpClient, String agentUrl)
119110 * agent URL, defaults to ".well-known/agent.json"
120111 * @param authHeaders the HTTP authentication headers to use
121112 * @return the agent card
122- * @throws A2AServerException if the agent card cannot be retrieved for any reason
113+ * @throws A2AClientError If an HTTP error occurs fetching the card
114+ * @throws A2AClientJSONError f the response body cannot be decoded as JSON or validated against the AgentCard schema
123115 */
124- public static AgentCard getAgentCard (String agentUrl , String relativeCardPath , Map <String , String > authHeaders ) throws A2AServerException {
116+ public static AgentCard getAgentCard (String agentUrl , String relativeCardPath , Map <String , String > authHeaders ) throws A2AClientError , A2AClientJSONError {
125117 return getAgentCard (new JdkA2AHttpClient (), agentUrl , relativeCardPath , authHeaders );
126118 }
127119
@@ -134,38 +126,12 @@ public static AgentCard getAgentCard(String agentUrl, String relativeCardPath, M
134126 * agent URL, defaults to ".well-known/agent.json"
135127 * @param authHeaders the HTTP authentication headers to use
136128 * @return the agent card
137- * @throws A2AServerException if the agent card cannot be retrieved for any reason
129+ * @throws A2AClientError If an HTTP error occurs fetching the card
130+ * @throws A2AClientJSONError f the response body cannot be decoded as JSON or validated against the AgentCard schema
138131 */
139- public static AgentCard getAgentCard (A2AHttpClient httpClient , String agentUrl , String relativeCardPath , Map <String , String > authHeaders ) throws A2AServerException {
140- if (relativeCardPath == null || relativeCardPath .isEmpty ()) {
141- relativeCardPath = AGENT_CARD_REQUEST ;
142- } else {
143- if (relativeCardPath .startsWith ("/" )) {
144- relativeCardPath = relativeCardPath .substring (1 );
145- }
146- }
147- A2AHttpClient .GetBuilder builder = httpClient .createGet ()
148- .url (getRequestEndpoint (agentUrl , relativeCardPath ))
149- .addHeader ("Content-Type" , "application/json" );
150-
151- if (authHeaders != null ) {
152- for (Map .Entry <String , String > entry : authHeaders .entrySet ()) {
153- builder .addHeader (entry .getKey (), entry .getValue ());
154- }
155- }
156-
157- try {
158- A2AHttpResponse response = builder .get ();
159- if (!response .success ()) {
160- throw new A2AServerException ("Failed to obtain agent card: " + response .status ());
161- }
162- String body = response .body ();
163- return unmarshalFrom (body , AGENT_CARD_TYPE_REFERENCE );
164- } catch (IOException e ) {
165- throw new A2AServerException ("Failed to obtain agent card" , e );
166- } catch (InterruptedException e ) {
167- throw new A2AServerException ("Timed out obtaining agent card" , e );
168- }
132+ public static AgentCard getAgentCard (A2AHttpClient httpClient , String agentUrl , String relativeCardPath , Map <String , String > authHeaders ) throws A2AClientError , A2AClientJSONError {
133+ A2ACardResolver resolver = new A2ACardResolver (httpClient , agentUrl , relativeCardPath , authHeaders );
134+ return resolver .getAgentCard ();
169135 }
170136
171137 protected static boolean isValidMethodName (String methodName ) {
0 commit comments