diff --git a/Oras.Tests/RemoteTest/RepositoryTest.cs b/Oras.Tests/RemoteTest/RepositoryTest.cs index 5b1b60c..47e4e5b 100644 --- a/Oras.Tests/RemoteTest/RepositoryTest.cs +++ b/Oras.Tests/RemoteTest/RepositoryTest.cs @@ -2119,7 +2119,7 @@ public async Task ManifestStore_generateDescriptorWithVariousDockerContentDigest var err = false; try { - await s.GenerateDescriptor(resp, reference, method); + await s.GenerateDescriptorAsync(resp, reference, method); } catch (Exception e) { diff --git a/Oras/Remote/Auth/HttpClientWithBasicAuth.cs b/Oras/Remote/Auth/HttpClientWithBasicAuth.cs index 8bad285..5acb00b 100644 --- a/Oras/Remote/Auth/HttpClientWithBasicAuth.cs +++ b/Oras/Remote/Auth/HttpClientWithBasicAuth.cs @@ -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}"))); } diff --git a/Oras/Remote/HttpClientExtensions.cs b/Oras/Remote/HttpClientExtensions.cs new file mode 100644 index 0000000..63c488a --- /dev/null +++ b/Oras/Remote/HttpClientExtensions.cs @@ -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" }); + } + } +} diff --git a/Oras/Remote/Registry.cs b/Oras/Remote/Registry.cs index ef3daed..4fa7c3d 100644 --- a/Oras/Remote/Registry.cs +++ b/Oras/Remote/Registry.cs @@ -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) diff --git a/Oras/Remote/Repository.cs b/Oras/Remote/Repository.cs index 6220f1a..af34353 100644 --- a/Oras/Remote/Repository.cs +++ b/Oras/Remote/Repository.cs @@ -593,7 +593,7 @@ public async Task ResolveAsync(string reference, CancellationToken c return res.StatusCode switch { - HttpStatusCode.OK => await GenerateDescriptor(res, remoteReference, req.Method), + HttpStatusCode.OK => await GenerateDescriptorAsync(res, remoteReference, req.Method), HttpStatusCode.NotFound => throw new NotFoundException($"reference {reference} not found"), _ => throw await ErrorUtility.ParseErrorResponse(res) }; @@ -607,7 +607,7 @@ public async Task ResolveAsync(string reference, CancellationToken c /// /// /// - public async Task GenerateDescriptor(HttpResponseMessage res, RemoteReference reference, HttpMethod httpMethod) + public async Task GenerateDescriptorAsync(HttpResponseMessage res, RemoteReference reference, HttpMethod httpMethod) { string mediaType; try @@ -749,7 +749,7 @@ public async Task DeleteAsync(Descriptor target, CancellationToken cancellationT } else { - desc = await GenerateDescriptor(resp, remoteReference, HttpMethod.Get); + desc = await GenerateDescriptorAsync(resp, remoteReference, HttpMethod.Get); } return (desc, await resp.Content.ReadAsStreamAsync());