Skip to content

Commit

Permalink
Follow Nexus changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollo3zehn committed Mar 15, 2024
1 parent 93e8ab6 commit 6a91c0e
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 112 deletions.
29 changes: 25 additions & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
# How to format:
# (1) Add dotnet_diagnostic.XXXX.severity = error
# (2) Run dotnet-format: dotnet format --diagnostics XXXX
# How to apply single rule:
# Run dotnet format --diagnostics XXXX --severity info

# How to apply all rules:
# Run dotnet format --severity error/info/warn/

[*]
trim_trailing_whitespace = true

[*.cs]
# "run cleanup": https://betterprogramming.pub/enforce-net-code-style-with-editorconfig-d2f0d79091ac
# TODO: build real editorconfig file: https://github.com/dotnet/roslyn/blob/main/.editorconfig
# TODO: build real editorconfig file: https://github.com/dotnet/roslyn/blob/main/.editorconfig

# Prefer var
csharp_style_var_for_built_in_types = false
csharp_style_var_when_type_is_apparent = true
csharp_style_var_elsewhere = true
dotnet_diagnostic.IDE0007.severity = warning

# Make field
dotnet_diagnostic.IDE0044.severity = warning

# Use file scoped namespace declarations
dotnet_diagnostic.IDE0161.severity = error
csharp_style_namespace_declarations = file_scoped

# Collection initialization can be simplified
dotnet_diagnostic.IDE0300.severity = warning

# Enable naming rule violation errors on build (alternative: dotnet_analyzer_diagnostic.category-Style.severity = error)
dotnet_diagnostic.IDE1006.severity = error
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.vs/
.venv/

artifacts/
BenchmarkDotNet.Artifacts

Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TargetFrameworkVersion>net7.0</TargetFrameworkVersion>
<TargetFrameworkVersion>net8.0</TargetFrameworkVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
Expand Down
201 changes: 98 additions & 103 deletions src/Nexus.Sources.Federation/Federation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,141 +6,136 @@
using Nexus.DataModel;
using Nexus.Extensibility;

namespace Nexus.Sources
namespace Nexus.Sources;

[ExtensionDescription(
"Provides access to other Nexus databases.",
"https://github.com/Apollo3zehn/nexus-sources-federation",
"https://github.com/Apollo3zehn/nexus-sources-federation")]
public class Federation : IDataSource
{
[ExtensionDescription(
"Provides access to other Nexus databases.",
"https://github.com/Apollo3zehn/nexus-sources-federation",
"https://github.com/Apollo3zehn/nexus-sources-federation")]
public class Federation : IDataSource
{
private DataSourceContext _context = default!;
private Api.INexusClient _nexusClient = default!;
private string _sourcePath = "/";
private string _mountPoint = "/";
private string _includePattern = default!;
private DataSourceContext _context = default!;
private Api.INexusClient _nexusClient = default!;
private string _sourcePath = "/";
private string _mountPoint = "/";
private string _includePattern = default!;

private static JsonSerializerOptions _jsonSerializerOptions;
private static readonly JsonSerializerOptions _jsonSerializerOptions;

static Federation()
{
_jsonSerializerOptions = new JsonSerializerOptions();
_jsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
}
static Federation()
{
_jsonSerializerOptions = new JsonSerializerOptions();
_jsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
}

public Func<HttpClient, Api.INexusClient> CreateNexusClient { get; set; }
= httpClient => new Api.NexusClient(httpClient);
public Func<HttpClient, Api.INexusClient> CreateNexusClient { get; set; }
= httpClient => new Api.NexusClient(httpClient);

public Task SetContextAsync(DataSourceContext context, ILogger logger, CancellationToken cancellationToken)
{
_context = context;
public Task SetContextAsync(DataSourceContext context, ILogger logger, CancellationToken cancellationToken)
{
_context = context;

// http client
if (_context.ResourceLocator is null)
throw new Exception("The resource locator must be set.");
// http client
if (_context.ResourceLocator is null)
throw new Exception("The resource locator must be set.");

var httpClient = new HttpClient()
{
BaseAddress = _context.ResourceLocator
};
var httpClient = new HttpClient()
{
BaseAddress = _context.ResourceLocator
};

// token
var token = _context.SourceConfiguration?.GetStringValue($"access-token");
// token
var token = (_context.SourceConfiguration?.GetStringValue($"access-token")) ?? throw new Exception("The access-token property is not set.");
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token);

if (token is null)
throw new Exception("The access-token property is not set.");
_nexusClient = CreateNexusClient(httpClient);

httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token);
// source-path
var sourcePath = _context.SourceConfiguration?.GetStringValue($"source-path");

_nexusClient = CreateNexusClient(httpClient);
if (sourcePath is not null)
_sourcePath = '/' + sourcePath.Trim('/');

// source-path
var sourcePath = _context.SourceConfiguration?.GetStringValue($"source-path");
// mount-point
var mountPoint = _context.SourceConfiguration?.GetStringValue($"mount-point");

if (sourcePath is not null)
_sourcePath = '/' + sourcePath.Trim('/');
if (mountPoint is not null)
_mountPoint = '/' + mountPoint.Trim('/');

// mount-point
var mountPoint = _context.SourceConfiguration?.GetStringValue($"mount-point");
// include-pattern
var includePattern = _context.SourceConfiguration?.GetStringValue($"include-pattern");
_includePattern = includePattern ?? "";

if (mountPoint is not null)
_mountPoint = '/' + mountPoint.Trim('/');
return Task.CompletedTask;
}

// include-pattern
var includePattern = _context.SourceConfiguration?.GetStringValue($"include-pattern");
_includePattern = includePattern ?? "";
public async Task<CatalogRegistration[]> GetCatalogRegistrationsAsync(string path, CancellationToken cancellationToken)
{
if (path == "/")
path = _mountPoint;

return Task.CompletedTask;
}
var catalogInfos = await _nexusClient.Catalogs.GetChildCatalogInfosAsync(ToSourcePathPrefixedCatalogId(path), cancellationToken);

public async Task<CatalogRegistration[]> GetCatalogRegistrationsAsync(string path, CancellationToken cancellationToken)
{
if (path == "/")
path = _mountPoint;
return catalogInfos
.Where(catalogInfo => Regex.IsMatch(catalogInfo.Id, _includePattern))
.Select(catalogInfo => new CatalogRegistration(ToMountPointPrefixedCatalogId(catalogInfo.Id), catalogInfo.Title, IsTransient: true))
.ToArray();
}

var catalogInfos = await _nexusClient.Catalogs.GetChildCatalogInfosAsync(ToSourcePathPrefixedCatalogId(path), cancellationToken);
public async Task<ResourceCatalog> GetCatalogAsync(string catalogId, CancellationToken cancellationToken)
{
var resourceCatalog = await _nexusClient.Catalogs.GetAsync(ToSourcePathPrefixedCatalogId(catalogId), cancellationToken);
resourceCatalog = resourceCatalog with { Id = catalogId };

return catalogInfos
.Where(catalogInfo => Regex.IsMatch(catalogInfo.Id, _includePattern))
.Select(catalogInfo => new CatalogRegistration(ToMountPointPrefixedCatalogId(catalogInfo.Id), catalogInfo.Title, IsTransient: true))
.ToArray();
}
var jsonString = JsonSerializer.Serialize(resourceCatalog, _jsonSerializerOptions);
var actualResourceCatalog = JsonSerializer.Deserialize<ResourceCatalog>(jsonString, _jsonSerializerOptions)!;

public async Task<ResourceCatalog> GetCatalogAsync(string catalogId, CancellationToken cancellationToken)
{
var resourceCatalog = await _nexusClient.Catalogs.GetAsync(ToSourcePathPrefixedCatalogId(catalogId));
resourceCatalog = resourceCatalog with { Id = catalogId };
return actualResourceCatalog;
}

var jsonString = JsonSerializer.Serialize(resourceCatalog, _jsonSerializerOptions);
var actualResourceCatalog = JsonSerializer.Deserialize<ResourceCatalog>(jsonString, _jsonSerializerOptions)!;
public async Task<double> GetAvailabilityAsync(string catalogId, DateTime begin, DateTime end, CancellationToken cancellationToken)
{
var availability = await _nexusClient.Catalogs
.GetAvailabilityAsync(ToSourcePathPrefixedCatalogId(catalogId), begin, end, end - begin, cancellationToken);

return actualResourceCatalog;
}
return availability.Data[0];
}

public async Task<double> GetAvailabilityAsync(string catalogId, DateTime begin, DateTime end, CancellationToken cancellationToken)
{
var availability = await _nexusClient.Catalogs
.GetAvailabilityAsync(ToSourcePathPrefixedCatalogId(catalogId), begin, end, end - begin, cancellationToken);
public async Task<(DateTime Begin, DateTime End)> GetTimeRangeAsync(string catalogId, CancellationToken cancellationToken)
{
var timeRange = await _nexusClient.Catalogs
.GetTimeRangeAsync(ToSourcePathPrefixedCatalogId(catalogId), cancellationToken);

return availability.Data[0];
}
return (timeRange.Begin, timeRange.End);
}

public async Task<(DateTime Begin, DateTime End)> GetTimeRangeAsync(string catalogId, CancellationToken cancellationToken)
public async Task ReadAsync(DateTime begin, DateTime end, ReadRequest[] requests, ReadDataHandler readData, IProgress<double> progress, CancellationToken cancellationToken)
{
foreach (var request in requests)
{
var timeRange = await _nexusClient.Catalogs
.GetTimeRangeAsync(ToSourcePathPrefixedCatalogId(catalogId), cancellationToken);

return (timeRange.Begin, timeRange.End);
}
var response = await _nexusClient.Data.GetStreamAsync(ToSourcePathPrefixedCatalogId(request.CatalogItem.ToPath()), begin, end, cancellationToken);
var stream = await response.Content.ReadAsStreamAsync(cancellationToken);
var targetBuffer = request.Data;

public async Task ReadAsync(DateTime begin, DateTime end, ReadRequest[] requests, ReadDataHandler readData, IProgress<double> progress, CancellationToken cancellationToken)
{
foreach (var request in requests)
while (targetBuffer.Length > 0)
{
var response = await _nexusClient.Data.GetStreamAsync(ToSourcePathPrefixedCatalogId(request.CatalogItem.ToPath()), begin, end, cancellationToken);
var stream = await response.Content.ReadAsStreamAsync();
var targetBuffer = request.Data;

while (targetBuffer.Length > 0)
{
var bytesRead = await stream.ReadAsync(targetBuffer);
targetBuffer = targetBuffer.Slice(bytesRead);
}

request.Status.Span.Fill(1);
var bytesRead = await stream.ReadAsync(targetBuffer, cancellationToken);
targetBuffer = targetBuffer[bytesRead..];
}
}

private string ToMountPointPrefixedCatalogId(string catalogId)
{
// absolute, relative
return Path.Combine(_mountPoint, catalogId.Substring(_sourcePath.Length).TrimStart('/'));
request.Status.Span.Fill(1);
}
}

private string ToSourcePathPrefixedCatalogId(string catalogId)
{
// absolute, relative
return Path.Combine(_sourcePath, catalogId.Substring(_mountPoint.Length).TrimStart('/'));
}
private string ToMountPointPrefixedCatalogId(string catalogId)
{
// absolute, relative
return Path.Combine(_mountPoint, catalogId[_sourcePath.Length..].TrimStart('/'));
}

private string ToSourcePathPrefixedCatalogId(string catalogId)
{
// absolute, relative
return Path.Combine(_sourcePath, catalogId[_mountPoint.Length..].TrimStart('/'));
}
}
4 changes: 2 additions & 2 deletions src/Nexus.Sources.Federation/Nexus.Sources.Federation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Nexus.Api" Version="2.0.0-beta.21" />
<PackageReference Include="Nexus.Extensibility" Version="2.0.0-beta.21">
<PackageReference Include="Nexus.Api" Version="2.0.0-beta.24" />
<PackageReference Include="Nexus.Extensibility" Version="2.0.0-beta.24">
<ExcludeAssets>runtime;native</ExcludeAssets>
</PackageReference>
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion tests/Nexus.Sources.Federation.Tests/FederationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task ProvidesCatalogRegistrations(string? sourcePath, string? mount
It.IsAny<string>(),
It.IsAny<CancellationToken>()
))
.ReturnsAsync((string catalogId, CancellationToken cancellationToken) =>
.ReturnsAsync((string catalogId, CancellationToken cancellationToken) =>
{
Assert.True(catalogId.StartsWith('/'));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="Nexus.Extensibility" Version="2.0.0-beta.21" />
<PackageReference Include="Nexus.Extensibility" Version="2.0.0-beta.24" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PrivateAssets>all</PrivateAssets>
Expand Down

0 comments on commit 6a91c0e

Please sign in to comment.