Skip to content

Commit 195fe5b

Browse files
authored
Merge pull request #15 from brandonh-msft/feat/14-http-client-auth-support
Add authorization capability to A2A HTTP client
2 parents fc6198d + 729fbfa commit 195fe5b

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/a2a-net.Client.Asbtractions/Configuration/A2AProtocolClientOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ public class A2AProtocolClientOptions
2424
/// </summary>
2525
public virtual Uri Endpoint { get; set; } = null!;
2626

27+
public virtual Func<(string Scheme, string Token)>? Authorization { get; set; } = null;
2728
}

src/a2a-net.Client.Http/Services/A2AProtocolHttpClient.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,34 @@ namespace A2A.Client.Services;
1616
/// <summary>
1717
/// Represents the default HTTP implementation of the <see cref="IA2AProtocolClient"/> interface
1818
/// </summary>
19-
/// <param name="options">The service used to access the current <see cref="A2AProtocolClientOptions"/></param>
20-
/// <param name="httpClient">The service used to perform HTTP requests</param>
21-
public class A2AProtocolHttpClient(IOptions<A2AProtocolClientOptions> options, HttpClient httpClient)
22-
: IA2AProtocolClient, IDisposable
19+
public class A2AProtocolHttpClient : IA2AProtocolClient, IDisposable
2320
{
2421

2522
bool _disposed;
2623

24+
/// <param name="options">The service used to access the current <see cref="A2AProtocolClientOptions"/></param>
25+
/// <param name="httpClient">The service used to perform HTTP requests</param>
26+
public A2AProtocolHttpClient(IOptions<A2AProtocolClientOptions> options, HttpClient httpClient)
27+
{
28+
this.Options = options.Value;
29+
this.HttpClient = httpClient;
30+
31+
if (this.Options.Authorization is not null)
32+
{
33+
var (scheme, token) = this.Options.Authorization();
34+
this.HttpClient.DefaultRequestHeaders.Authorization = new(scheme, token);
35+
}
36+
}
37+
2738
/// <summary>
2839
/// Gets the current <see cref="A2AProtocolClientOptions"/>
2940
/// </summary>
30-
protected A2AProtocolClientOptions Options { get; } = options.Value;
41+
protected A2AProtocolClientOptions Options { get; }
3142

3243
/// <summary>
3344
/// Gets the service used to perform HTTP requests
3445
/// </summary>
35-
protected HttpClient HttpClient { get; } = httpClient;
46+
protected HttpClient HttpClient { get; }
3647

3748
/// <inheritdoc/>
3849
public virtual async Task<RpcResponse<Models.Task>> SendTaskAsync(SendTaskRequest request, CancellationToken cancellationToken = default)

0 commit comments

Comments
 (0)