Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions client/src/main/java/io/a2a/A2A.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import java.util.Map;

import io.a2a.client.A2ACardResolver;
import io.a2a.http.A2AHttpClient;
import io.a2a.http.JdkA2AHttpClient;
import io.a2a.spec.A2AClientError;
import io.a2a.spec.A2AClientJSONError;
import io.a2a.spec.AgentCard;
import io.a2a.spec.Message;
import io.a2a.spec.TextPart;
import io.a2a.transport.A2ATransport;
import io.a2a.transport.http.JdkA2AHttpTransport;


/**
Expand Down Expand Up @@ -80,20 +80,20 @@ private static Message toMessage(String text, Message.Role role, String messageI
* @throws A2AClientJSONError f the response body cannot be decoded as JSON or validated against the AgentCard schema
*/
public static AgentCard getAgentCard(String agentUrl) throws A2AClientError, A2AClientJSONError {
return getAgentCard(new JdkA2AHttpClient(), agentUrl);
return getAgentCard(new JdkA2AHttpTransport(), agentUrl);
}

/**
* Get the agent card for an A2A agent.
*
* @param httpClient the http client to use
* @param transport the transport to use
* @param agentUrl the base URL for the agent whose agent card we want to retrieve
* @return the agent card
* @throws A2AClientError If an HTTP error occurs fetching the card
* @throws A2AClientJSONError f the response body cannot be decoded as JSON or validated against the AgentCard schema
*/
public static AgentCard getAgentCard(A2AHttpClient httpClient, String agentUrl) throws A2AClientError, A2AClientJSONError {
return getAgentCard(httpClient, agentUrl, null, null);
public static AgentCard getAgentCard(A2ATransport transport, String agentUrl) throws A2AClientError, A2AClientJSONError {
return getAgentCard(transport, agentUrl, null, null);
}

/**
Expand All @@ -108,13 +108,13 @@ public static AgentCard getAgentCard(A2AHttpClient httpClient, String agentUrl)
* @throws A2AClientJSONError f the response body cannot be decoded as JSON or validated against the AgentCard schema
*/
public static AgentCard getAgentCard(String agentUrl, String relativeCardPath, Map<String, String> authHeaders) throws A2AClientError, A2AClientJSONError {
return getAgentCard(new JdkA2AHttpClient(), agentUrl, relativeCardPath, authHeaders);
return getAgentCard(new JdkA2AHttpTransport(), agentUrl, relativeCardPath, authHeaders);
}

/**
* Get the agent card for an A2A agent.
*
* @param httpClient the http client to use
* @param transport the transport to use
* @param agentUrl the base URL for the agent whose agent card we want to retrieve
* @param relativeCardPath optional path to the agent card endpoint relative to the base
* agent URL, defaults to ".well-known/agent.json"
Expand All @@ -123,8 +123,8 @@ public static AgentCard getAgentCard(String agentUrl, String relativeCardPath, M
* @throws A2AClientError If an HTTP error occurs fetching the card
* @throws A2AClientJSONError f the response body cannot be decoded as JSON or validated against the AgentCard schema
*/
public static AgentCard getAgentCard(A2AHttpClient httpClient, String agentUrl, String relativeCardPath, Map<String, String> authHeaders) throws A2AClientError, A2AClientJSONError {
A2ACardResolver resolver = new A2ACardResolver(httpClient, agentUrl, relativeCardPath, authHeaders);
public static AgentCard getAgentCard(A2ATransport transport, String agentUrl, String relativeCardPath, Map<String, String> authHeaders) throws A2AClientError, A2AClientJSONError {
A2ACardResolver resolver = new A2ACardResolver(transport, agentUrl, relativeCardPath, authHeaders);
return resolver.getAgentCard();
}
}
55 changes: 12 additions & 43 deletions client/src/main/java/io/a2a/client/A2ACardResolver.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,48 @@
package io.a2a.client;

import static io.a2a.util.Utils.unmarshalFrom;

import java.io.IOException;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import io.a2a.http.A2AHttpClient;
import io.a2a.http.A2AHttpResponse;
import io.a2a.transport.A2ATransport;
import io.a2a.spec.A2AClientError;
import io.a2a.spec.A2AClientJSONError;
import io.a2a.spec.AgentCard;

public class A2ACardResolver {
private final A2AHttpClient httpClient;
private final A2ATransport transport;
private final String url;
private final Map<String, String> authHeaders;

static String DEFAULT_AGENT_CARD_PATH = "/.well-known/agent.json";

static final TypeReference<AgentCard> AGENT_CARD_TYPE_REFERENCE = new TypeReference<>() {};
/**
* @param httpClient the http client to use
* @param transport the transport to use
* @param baseUrl the base URL for the agent whose agent card we want to retrieve
*/
public A2ACardResolver(A2AHttpClient httpClient, String baseUrl) {
this(httpClient, baseUrl, null, null);
public A2ACardResolver(A2ATransport transport, String baseUrl) {
this(transport, baseUrl, null, null);
}

/**
* @param httpClient the http client to use
* @param transport the transport to use
* @param baseUrl the base URL for the agent whose agent card we want to retrieve
* @param agentCardPath optional path to the agent card endpoint relative to the base
* agent URL, defaults to ".well-known/agent.json"
*/
public A2ACardResolver(A2AHttpClient httpClient, String baseUrl, String agentCardPath) {
this(httpClient, baseUrl, agentCardPath, null);
public A2ACardResolver(A2ATransport transport, String baseUrl, String agentCardPath) {
this(transport, baseUrl, agentCardPath, null);
}

/**
* @param httpClient the http client to use
* @param transport the transport to use
* @param baseUrl the base URL for the agent whose agent card we want to retrieve
* @param agentCardPath optional path to the agent card endpoint relative to the base
* agent URL, defaults to ".well-known/agent.json"
* @param authHeaders the HTTP authentication headers to use. May be {@code null}
*/
public A2ACardResolver(A2AHttpClient httpClient, String baseUrl, String agentCardPath, Map<String, String> authHeaders) {
this.httpClient = httpClient;
public A2ACardResolver(A2ATransport transport, String baseUrl, String agentCardPath, Map<String, String> authHeaders) {
this.transport = transport;
if (!baseUrl.endsWith("/")) {
baseUrl += "/";
}
Expand All @@ -67,33 +62,7 @@ public A2ACardResolver(A2AHttpClient httpClient, String baseUrl, String agentCar
* @throws A2AClientJSONError f the response body cannot be decoded as JSON or validated against the AgentCard schema
*/
public AgentCard getAgentCard() throws A2AClientError, A2AClientJSONError {
A2AHttpClient.GetBuilder builder = httpClient.createGet()
.url(url)
.addHeader("Content-Type", "application/json");

if (authHeaders != null) {
for (Map.Entry<String, String> entry : authHeaders.entrySet()) {
builder.addHeader(entry.getKey(), entry.getValue());
}
}

String body;
try {
A2AHttpResponse response = builder.get();
if (!response.success()) {
throw new A2AClientError("Failed to obtain agent card: " + response.status());
}
body = response.body();
} catch (IOException | InterruptedException e) {
throw new A2AClientError("Failed to obtain agent card", e);
}

try {
return unmarshalFrom(body, AGENT_CARD_TYPE_REFERENCE);
} catch (JsonProcessingException e) {
throw new A2AClientJSONError("Could not unmarshal agent card response", e);
}

return transport.getAgentCard(url, authHeaders);
}


Expand Down
Loading