Skip to content

Commit

Permalink
add HttpClient utility to set user agent
Browse files Browse the repository at this point in the history
Signed-off-by: Sajay Antony <sajaya@microsoft.com>
  • Loading branch information
sajayantony committed Dec 12, 2023
1 parent 4c473a9 commit f1634b8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Oras/Remote/Auth/HttpClientWithBasicAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ public class HttpClientWithBasicAuth : HttpClient
{
public HttpClientWithBasicAuth(string username, string password)
{
DefaultRequestHeaders.Add("User-Agent", new string[] { "oras-dotnet" });
this.AddUserAgent();
DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}")));
}

public HttpClientWithBasicAuth(string username, string password, HttpMessageHandler handler) : base(handler)
{
DefaultRequestHeaders.Add("User-Agent", new string[] { "oras-dotnet" });
this.AddUserAgent();
DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}")));
}
Expand Down
12 changes: 12 additions & 0 deletions Oras/Remote/HttpClientExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Net.Http;

namespace Oras.Remote
{
internal static class HttpClientExtensions
{
public static void AddUserAgent(this HttpClient client)
{
client.DefaultRequestHeaders.Add("User-Agent", new string[] { "oras-dotnet" });
}
}
}
2 changes: 1 addition & 1 deletion Oras/Remote/Registry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Registry(string name)
reference.ValidateRegistry();
RemoteReference = reference;
HttpClient = new HttpClient();
HttpClient.DefaultRequestHeaders.Add("User-Agent", new string[] { "oras-dotnet" });
HttpClient.AddUserAgent();
}

public Registry(string name, HttpClient httpClient)
Expand Down

0 comments on commit f1634b8

Please sign in to comment.