Skip to content

Commit

Permalink
Suggestions from the AI
Browse files Browse the repository at this point in the history
Actually pretty useful.
  • Loading branch information
Jaben committed Aug 11, 2024
1 parent ded5cdf commit efaa48b
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 35 deletions.
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,49 @@ dotnet add package Docker.Registry.DotNet
```

# Usage

### Local Hub

```csharp
var configuration = new RegistryClientConfiguration("http://localhost:5000");

//configuration.UsePasswordOAuthAuthentication("username", "password")
using (var client = configuration.CreateClient())
{
// get catalog
var catalog = await await client.Catalog.GetCatalog();
var catalog = await client.Catalog.GetCatalog();

// list tags for the first catalog
var tags = await client.Tags.ListTags(catalog?.Repositories.FirstOrDefault());
}
```

### Remote Hub with Authentication

```csharp
var configuration = new RegistryClientConfiguration("https://proget.mycompany.com");

configuration.UsePasswordOAuthAuthentication("username", "password")

using (var client = configuration.CreateClient())
{
// get catalog
var catalog = await client.Catalog.GetCatalog();

// list tags for the first catalog
var tags = await client.Tags.ListTags(catalog?.Repositories.FirstOrDefault());
}
```

### Docker Hub

```csharp
var configuration = new RegistryClientConfiguration("https://hub.docker.com");

using (var client = configuration.CreateClient())
{
// load respository
var tags = await client.Repository.ListRepositoryTags("grafana", "loki-docker-driver");
}
```
29 changes: 19 additions & 10 deletions src/Docker.Registry.DotNet/Application/OAuth/OAuthClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,30 @@ internal class OAuthClient

activity?.AddEvent(new ActivityEvent("Getting Token"));

using var response = await _client.SendAsync(request, token);

if (!response.IsSuccessStatusCode)
try
{
activity?.AddEvent(new ActivityEvent("Failed to Authenticate"));
using var response = await _client.SendAsync(request, token);

throw new UnauthorizedAccessException(
$"Unable to authenticate: {await response.Content.ReadAsStringAsyncWithCancellation(token)}");
}
if (!response.IsSuccessStatusCode)
{
activity?.AddEvent(new ActivityEvent("Failed to Authenticate"));

throw new UnauthorizedAccessException(
$"Unable to authenticate: {await response.Content.ReadAsStringAsyncWithCancellation(token)}");
}

var body = await response.Content.ReadAsStringAsyncWithCancellation(token);
var body = await response.Content.ReadAsStringAsyncWithCancellation(token);

var authToken = JsonConvert.DeserializeObject<OAuthToken>(body);
var authToken = JsonConvert.DeserializeObject<OAuthToken>(body);

return authToken;
return authToken;
}
catch (Exception ex)
{
activity?.AddTag("Authentication Exception", ex);

throw new UnauthorizedAccessException($"Unable to authenticate: {ex}");
}
}

public Task<OAuthToken?> GetToken(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ListRepositoryTagsResponse
public string Next { get; set; }

Check warning on line 9 in src/Docker.Registry.DotNet/Domain/Repository/ListRepositoryTagsResponse.cs

View workflow job for this annotation

GitHub Actions / build (8.x)

Non-nullable property 'Next' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[JsonProperty("previous")]
public object Previous { get; set; }
public string Previous { get; set; }

Check warning on line 12 in src/Docker.Registry.DotNet/Domain/Repository/ListRepositoryTagsResponse.cs

View workflow job for this annotation

GitHub Actions / build (8.x)

Non-nullable property 'Previous' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[JsonProperty("results")]
public List<RepositoryTag> Tags { get; set; }

Check warning on line 15 in src/Docker.Registry.DotNet/Domain/Repository/ListRepositoryTagsResponse.cs

View workflow job for this annotation

GitHub Actions / build (8.x)

Non-nullable property 'Tags' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
Expand Down
14 changes: 7 additions & 7 deletions src/Docker.Registry.DotNet/Domain/Repository/RepositoryTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class RepositoryTag
public int Id { get; set; }

[JsonProperty("images")]
public List<RepositoryTagImage> Images { get; set; }
public List<RepositoryTagImage> Images { get; set; } = new List<RepositoryTagImage>();

[JsonProperty("last_updated")]
[JsonConverter(typeof(IsoDateTimeConverter))]
Expand All @@ -21,10 +21,10 @@ public class RepositoryTag
public int LastUpdater { get; set; }

[JsonProperty("last_updater_username")]
public string LastUpdaterUsername { get; set; }
public string? LastUpdaterUsername { get; set; }

[JsonProperty("name")]
public string Name { get; set; }
public string? Name { get; set; }

[JsonProperty("repository")]
public int Repository { get; set; }
Expand All @@ -36,7 +36,7 @@ public class RepositoryTag
public bool V2 { get; set; }

[JsonProperty("tag_status")]
public string TagStatus { get; set; }
public string? TagStatus { get; set; }

[JsonProperty("tag_last_pulled")]
[JsonConverter(typeof(IsoDateTimeConverter))]
Expand All @@ -47,11 +47,11 @@ public class RepositoryTag
public DateTime TagLastPushed { get; set; }

[JsonProperty("media_type")]
public string MediaType { get; set; }
public string? MediaType { get; set; }

[JsonProperty("content_type")]
public string ContentType { get; set; }
public string? ContentType { get; set; }

[JsonProperty("digest")]
public string Digest { get; set; }
public string? Digest { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@ namespace Docker.Registry.DotNet.Domain.Repository;
public class RepositoryTagImage
{
[JsonProperty("architecture")]
public string Architecture { get; set; }
public string? Architecture { get; set; }

[JsonProperty("features")]
public string Features { get; set; }
public string? Features { get; set; }

[JsonProperty("variant")]
public object Variant { get; set; }
public object? Variant { get; set; }

[JsonProperty("digest")]
public string Digest { get; set; }
public string? Digest { get; set; }

[JsonProperty("os")]
public string Os { get; set; }
public string? Os { get; set; }

[JsonProperty("os_features")]
public string OsFeatures { get; set; }
public string? OsFeatures { get; set; }

[JsonProperty("os_version")]
public object OsVersion { get; set; }
public object? OsVersion { get; set; }

[JsonProperty("size")]
public long Size { get; set; }

[JsonProperty("status")]
public string Status { get; set; }
public string? Status { get; set; }

[JsonProperty("last_pulled")]
[JsonConverter(typeof(IsoDateTimeConverter))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal static Uri BuildUri(this Uri baseUri, string path, IReadOnlyQueryString
/// <param name="response"></param>
/// <param name="name"></param>
/// <returns>The first value if one is found, null otherwise.</returns>
public static string GetHeader(this RegistryApiResponse response, string name)
public static string? GetHeader(this RegistryApiResponse response, string name)
{
if (response == null) throw new ArgumentNullException(nameof(response));

Expand All @@ -64,7 +64,7 @@ public static string GetHeader(this RegistryApiResponse response, string name)
/// <param name="headers"></param>
/// <param name="name"></param>
/// <returns>The first value if one is found, null otherwise.</returns>
public static string[] GetHeaders(
public static string?[] GetHeaders(
this IEnumerable<KeyValuePair<string, string[]>> headers,
string name)
{
Expand Down Expand Up @@ -106,7 +106,7 @@ public static void AddRange(
return null;
}

public static string GetString(
public static string? GetString(
this HttpResponseHeaders responseHeaders,
string name)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal class JsonSerializer
}
};

public T DeserializeObject<T>(string json)
public T? DeserializeObject<T>(string json)
{
return JsonConvert.DeserializeObject<T>(json, Settings);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Docker.Registry.DotNet/RegistryClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public class RegistryClientConfiguration

public RegistryClientConfiguration(string baseAddress, TimeSpan defaultTimeout = default)
{
var valid = Uri.TryCreate(baseAddress, UriKind.Absolute, out var parsedBaseAddress);
var isValidUri = Uri.TryCreate(baseAddress, UriKind.Absolute, out var parsedBaseAddress);

if (valid) this.SetBaseAddress(parsedBaseAddress);
if (isValidUri ) this.SetBaseAddress(parsedBaseAddress);

Check warning on line 28 in src/Docker.Registry.DotNet/RegistryClientConfiguration.cs

View workflow job for this annotation

GitHub Actions / build (8.x)

Possible null reference argument for parameter 'baseAddress' in 'RegistryClientConfiguration RegistryClientConfiguration.SetBaseAddress(Uri baseAddress)'.
else throw new ArgumentException("BaseAddress is not a valid Uri", nameof(baseAddress));

this.SetDefaultTimeout(defaultTimeout);
Expand Down Expand Up @@ -75,7 +75,7 @@ public RegistryClientConfiguration SetBaseAddress(Uri baseAddress)
if (baseAddress.Scheme is not ("http" or "https"))
throw new ArgumentOutOfRangeException(
nameof(this.BaseAddress),
"Base Address Uri must start with http:// or https://");
"BaseAddress must use either http or https schema.");

this.BaseAddress = baseAddress;

Expand Down
2 changes: 1 addition & 1 deletion src/Docker.Registry.DotNet/RegistryClientLegacyHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public static Task CompleteBlobUploadAsync(
}

[Obsolete("Use CancelBlobUpload() instead")]
public static Task CompleteBlobUploadAsync(
public static Task CancelBlobUploadAsync(
this IBlobUploadOperations operations,
string name,
string uuid,
Expand Down

0 comments on commit efaa48b

Please sign in to comment.