Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: modernize Registry.Remote #94

Merged
merged 10 commits into from
Jan 9, 2024
4 changes: 2 additions & 2 deletions src/OrasProject.Oras/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public static async Task CopyGraphAsync(this ITarget src, ITarget dst, Descripto
{
// check if node exists in target
if (await dst.ExistsAsync(node, cancellationToken).ConfigureAwait(false))
{
return;
{
return;
}

// retrieve successors
Expand Down
2 changes: 1 addition & 1 deletion src/OrasProject.Oras/Registry/IRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface IRegistry
/// <param name="name"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<IRepository> GetRepository(string name, CancellationToken cancellationToken = default);
Task<IRepository> GetRepositoryAsync(string name, CancellationToken cancellationToken = default);

/// <summary>
/// Repositories lists the name of repositories available in the registry.
Expand Down
31 changes: 23 additions & 8 deletions src/OrasProject.Oras/Registry/Reference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

using OrasProject.Oras.Exceptions;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;

namespace OrasProject.Oras.Registry;
Expand Down Expand Up @@ -82,11 +83,11 @@ public string Digest
{
if (_reference == null)
{
throw new InvalidReferenceException("null content reference");
throw new InvalidReferenceException("Null content reference");
}
if (_isTag)
{
throw new InvalidReferenceException("not a digest");
throw new InvalidReferenceException("Not a digest");
}
return _reference;
}
Expand All @@ -101,11 +102,11 @@ public string Tag
{
if (_reference == null)
{
throw new InvalidReferenceException("null content reference");
throw new InvalidReferenceException("Null content reference");
}
if (!_isTag)
{
throw new InvalidReferenceException("not a tag");
throw new InvalidReferenceException("Not a tag");
}
return _reference;
}
Expand Down Expand Up @@ -142,7 +143,7 @@ public static Reference Parse(string reference)
var parts = reference.Split('/', 2);
if (parts.Length == 1)
{
throw new InvalidReferenceException("missing repository");
throw new InvalidReferenceException("Missing repository");
}
var registry = parts[0];
var path = parts[1];
Expand Down Expand Up @@ -186,6 +187,20 @@ public static Reference Parse(string reference)
return new Reference(registry, path);
}

public static bool TryParse(string reference, [NotNullWhen(true)] out Reference? parsedReference)
{
try
{
parsedReference = Parse(reference);
return true;
}
catch (InvalidReferenceException)
{
parsedReference = null;
return false;
}
}

public Reference(string registry) => _registry = ValidateRegistry(registry);

public Reference(string registry, string? repository) : this(registry)
Expand All @@ -199,7 +214,7 @@ private static string ValidateRegistry(string registry)
var url = "dummy://" + registry;
if (!Uri.IsWellFormedUriString(url, UriKind.Absolute) || new Uri(url).Authority != registry)
{
throw new InvalidReferenceException("invalid registry");
throw new InvalidReferenceException("Invalid registry");
}
return registry;
}
Expand All @@ -208,7 +223,7 @@ private static string ValidateRepository(string? repository)
{
if (repository == null || !_repositoryRegex.IsMatch(repository))
{
throw new InvalidReferenceException("invalid respository");
throw new InvalidReferenceException("Invalid respository");
}
return repository;
}
Expand All @@ -219,7 +234,7 @@ private static string ValidateReferenceAsTag(string? reference)
{
if (reference == null || !_tagRegex.IsMatch(reference))
{
throw new InvalidReferenceException("invalid tag");
throw new InvalidReferenceException("Invalid tag");
}
return reference;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,22 @@
using System.Net.Http.Headers;
using System.Text;

namespace OrasProject.Oras.Remote.Auth
namespace OrasProject.Oras.Registry.Remote.Auth;

/// <summary>
/// HttpClientWithBasicAuth adds the Basic Auth Scheme to the Authorization Header
/// </summary>
public class HttpClientWithBasicAuth : HttpClient
{
/// <summary>
/// HttpClientWithBasicAuth adds the Basic Auth Scheme to the Authorization Header
/// </summary>
public class HttpClientWithBasicAuth : HttpClient
{
public HttpClientWithBasicAuth(string username, string password)
{
this.AddUserAgent();
DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}")));
}
public HttpClientWithBasicAuth(string username, string password) => Initialize(username, password);

public HttpClientWithBasicAuth(string username, string password, HttpMessageHandler handler) : base(handler)
{
this.AddUserAgent();
DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}")));
}
public HttpClientWithBasicAuth(string username, string password, HttpMessageHandler handler) : base(handler)
=> Initialize(username, password);

private void Initialize(string username, string password)
{
this.AddUserAgent();
DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}")));
}
}
Loading
Loading