Skip to content

Commit

Permalink
working ContainerNamesQuery2
Browse files Browse the repository at this point in the history
  • Loading branch information
woutervanranst committed Aug 20, 2024
1 parent 4c3c200 commit ff5e2eb
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 41 deletions.
3 changes: 1 addition & 2 deletions src/Arius.Core/Facade/RepositoryOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs;

namespace Arius.Core.Facade;

Expand Down
17 changes: 15 additions & 2 deletions src/Arius.Core/Facade/StorageAccountOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Arius.Core.Commands;
using Arius.Core.Commands;
using Azure.Storage;
using Azure.Storage.Blobs;

Expand Down Expand Up @@ -39,6 +38,20 @@ public static BlobServiceClient GetBlobServiceClient(this StorageAccountOptions
return new BlobServiceClient(new Uri($"https://{storageAccount.AccountName}.blob.core.windows.net/"),
new StorageSharedKeyCredential(storageAccount.AccountName, storageAccount.AccountKey));
}

public static BlobServiceClient GetBlobServiceClient(this StorageAccountOptions storageAccount, int maxRetries, TimeSpan networkTimeout)
{
var o = new BlobClientOptions
{
Retry =
{
MaxRetries = maxRetries,
NetworkTimeout = networkTimeout,
}
};

return GetBlobServiceClient(storageAccount, o);
}
public static BlobServiceClient GetBlobServiceClient(this StorageAccountOptions storageAccount, BlobClientOptions options)
{
return new BlobServiceClient(new Uri($"https://{storageAccount.AccountName}.blob.core.windows.net/"),
Expand Down
7 changes: 6 additions & 1 deletion src/Arius.Core/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Global using directives

global using WouterVanRanst.Utils.Extensions;
global using Microsoft.Extensions.Logging;
global using System;
global using System.Linq;
global using System.Threading.Tasks;
global using WouterVanRanst.Utils.Extensions;
global using System.Threading;
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using Arius.Core.Facade;
using System.Collections.Generic;
using System.Linq;
using Arius.Core.Facade;
using Azure.Storage.Blobs;
using Microsoft.Extensions.Logging;

namespace Arius.Core.Queries.ContainerNames;

Expand All @@ -20,16 +16,7 @@ public ContainerNamesQueryHandler(ILogger<ContainerNamesQueryHandler> logger, St

protected override (QueryResultStatus Status, IAsyncEnumerable<string>? Result) ExecuteImpl(ContainerNamesQuery queryOptions)
{
var bco = new BlobClientOptions
{
Retry =
{
MaxRetries = queryOptions.MaxRetries,
NetworkTimeout = TimeSpan.FromSeconds(5),
}
};

var blobServiceClient = storageAccountOptions.GetBlobServiceClient(bco);
var blobServiceClient = storageAccountOptions.GetBlobServiceClient(queryOptions.MaxRetries, TimeSpan.FromSeconds(5));

return (QueryResultStatus.Success, blobServiceClient.GetBlobContainersAsync().Select(bci => bci.Name));
}
Expand Down
28 changes: 16 additions & 12 deletions src/Arius.Core/Queries/ContainerNames2/ContainerNamesQuery2.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using Arius.Core.Facade;
using MediatR;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

namespace Arius.Core.Queries.ContainerNames2;

Expand All @@ -15,23 +12,30 @@ public record ContainerNamesQuery2 : IRequest<IAsyncEnumerable<string>>, IStorag
public required int MaxRetries { get; init; }
}

public class ContainerNamesQueryHandler : IRequestHandler<ContainerNamesQuery2, IAsyncEnumerable<string>>
internal class ContainerNamesQuery2Handler : IRequestHandler<ContainerNamesQuery2, IAsyncEnumerable<string>>
{
private readonly ILogger<ContainerNamesQueryHandler> logger;
private readonly ILogger<ContainerNamesQuery2Handler> logger;

public ContainerNamesQueryHandler(ILogger<ContainerNamesQueryHandler> logger)
public ContainerNamesQuery2Handler(ILogger<ContainerNamesQuery2Handler> logger)
{
this.logger = logger;
}

public async Task<IAsyncEnumerable<string>> Handle(ContainerNamesQuery2 request, CancellationToken cancellationToken)
{
return GetContainerNamesAsync(cancellationToken);
}
return GetContainerNames(cancellationToken);

private async IAsyncEnumerable<string> GetContainerNamesAsync([EnumeratorCancellation] CancellationToken cancellationToken)
{
yield return "a";
yield return "b";

async IAsyncEnumerable<string> GetContainerNames([EnumeratorCancellation] CancellationToken cancellationToken)
{
var sao = new StorageAccountOptions(request.AccountName, request.AccountKey);

var bsc = sao.GetBlobServiceClient(request.MaxRetries, TimeSpan.FromSeconds(5));

await foreach (var container in bsc.GetBlobContainersAsync(cancellationToken: cancellationToken))
{
yield return container.Name;
}
}
}
}
5 changes: 1 addition & 4 deletions src/Arius.Core/Repositories/Repository.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System.Threading.Tasks;
using Arius.Core.Facade;
using Arius.Core.Facade;
using Arius.Core.Models;
using Arius.Core.Repositories.BlobRepository;
using Arius.Core.Repositories.StateDb;
using Microsoft.Extensions.Logging;
using PostSharp.Constraints;

namespace Arius.Core.Repositories;
Expand Down
5 changes: 1 addition & 4 deletions src/Arius.Core/Repositories/RepositoryBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System.Threading.Tasks;
using Arius.Core.Facade;
using Arius.Core.Facade;
using Arius.Core.Repositories.BlobRepository;
using Azure.Core;
using Azure.Storage.Blobs;
using Microsoft.Extensions.Logging;

namespace Arius.Core.Repositories;

Expand Down
6 changes: 5 additions & 1 deletion src/Arius.Web.Application/RepositoryOptionsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public class RepositoryOptionsService
private readonly IRepositoryOptionsRepository repositoryOptionsRepository;
private readonly IMediator mediator;

public RepositoryOptionsService(IRepositoryOptionsRepository repositoryOptionsRepository, IMediator mediator)
public RepositoryOptionsService(
IRepositoryOptionsRepository repositoryOptionsRepository,
IMediator mediator)
{
this.repositoryOptionsRepository = repositoryOptionsRepository;
this.mediator = mediator;
Expand All @@ -29,6 +31,8 @@ public async Task AddRepositoryOptionsAsync(RepositoryOptions config)

var r = await mediator.Send(q);

var rr = await r.ToListAsync();


await repositoryOptionsRepository.AddAsync(config);
}
Expand Down

0 comments on commit ff5e2eb

Please sign in to comment.