Skip to content

Commit

Permalink
refactor: Update GenerateDescriptor with Async suffix (#33)
Browse files Browse the repository at this point in the history
Signed-off-by: Sajay Antony <[email protected]>
  • Loading branch information
sajayantony authored Dec 13, 2023
1 parent bb7a18b commit 52fcd19
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Oras.Tests/RemoteTest/RepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
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
6 changes: 3 additions & 3 deletions Oras/Remote/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ public async Task<Descriptor> 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)
};
Expand All @@ -607,7 +607,7 @@ public async Task<Descriptor> ResolveAsync(string reference, CancellationToken c
/// <param name="httpMethod"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task<Descriptor> GenerateDescriptor(HttpResponseMessage res, RemoteReference reference, HttpMethod httpMethod)
public async Task<Descriptor> GenerateDescriptorAsync(HttpResponseMessage res, RemoteReference reference, HttpMethod httpMethod)
{
string mediaType;
try
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 52fcd19

Please sign in to comment.