using System.Threading.Tasks; using FrostFS.SDK.ClientV2.Mappers.GRPC.Netmap; using FrostFS.Container; using FrostFS.SDK.ClientV2.Mappers.GRPC; using FrostFS.SDK.Cryptography; using System.Collections.Generic; using FrostFS.SDK.ModelsV2; namespace FrostFS.SDK.ClientV2; internal class ContainerServiceProvider : ContextAccessor { private readonly ContainerService.ContainerServiceClient containerServiceClient; internal ContainerServiceProvider(ContainerService.ContainerServiceClient service, ClientEnvironment context) : base(context) { containerServiceClient = service; } internal async Task GetContainerAsync(ContainerId cid, Context context) { var request = new GetRequest { Body = new GetRequest.Types.Body { ContainerId = cid.ToGrpcMessage() }, }; request.AddMetaHeader(); request.Sign(Context.Key); var response = await containerServiceClient.GetAsync(request, null, context.Deadline, context.CancellationToken); Verifier.CheckResponse(response); return response.Body.Container.ToModel(); } internal async IAsyncEnumerable ListContainersAsync(Context ctx) { var request = new ListRequest { Body = new ListRequest.Types.Body { OwnerId = Context.Owner.ToGrpcMessage() } }; request.AddMetaHeader(); request.Sign(Context.Key); var response = await containerServiceClient.ListAsync(request, null, ctx.Deadline, ctx.CancellationToken); Verifier.CheckResponse(response); foreach (var cid in response.Body.ContainerIds) { yield return new ContainerId(Base58.Encode(cid.Value.ToByteArray())); } } internal async Task CreateContainerAsync(ModelsV2.Container container, Context ctx) { var grpcContainer = container.ToGrpcMessage(); grpcContainer.OwnerId = Context.Owner.ToGrpcMessage(); grpcContainer.Version = Context.Version.ToGrpcMessage(); var request = new PutRequest { Body = new PutRequest.Types.Body { Container = grpcContainer, Signature = Context.Key.SignRFC6979(grpcContainer), } }; request.AddMetaHeader(); request.Sign(Context.Key); var response = await containerServiceClient.PutAsync(request, null, ctx.Deadline, ctx.CancellationToken); Verifier.CheckResponse(response); return new ContainerId(Base58.Encode(response.Body.ContainerId.Value.ToByteArray())); } internal async Task DeleteContainerAsync(ContainerId cid, Context ctx) { var request = new DeleteRequest { Body = new DeleteRequest.Types.Body { ContainerId = cid.ToGrpcMessage(), Signature = Context.Key.SignRFC6979(cid.ToGrpcMessage().Value) } }; request.AddMetaHeader(); request.Sign(Context.Key); var response = await containerServiceClient.DeleteAsync(request, null, ctx.Deadline, ctx.CancellationToken); Verifier.CheckResponse(response); } }