frostfs-sdk-csharp/src/FrostFS.SDK.Service/Service.cs
Ivan Pchelintsev b307c2c899 [#1] Add object Get operation + code quality
Signed-off-by: Ivan Pchelintsev <i.pchelintsev@yadro.com>
2024-05-22 14:29:20 +03:00

81 lines
No EOL
2.8 KiB
C#

using FrostFS.SDK.ClientV2;
using FrostFS.SDK.ClientV2.Interfaces;
using FrostFS.SDK.ClientV2.Mappers.GRPC;
using FrostFS.SDK.ModelsV2;
namespace FrostFS.SDK.Service;
// TODO: Проверять ответы Grpc
public class FrostFsService
{
private readonly IFrostFSClient _client;
public FrostFsService(IFrostFSClient client)
{
_client = client;
}
public async Task<ContainerId[]> ListContainersAsync()
{
var listContainersResponse = await _client.ListContainersAsync();
return listContainersResponse.Body.ContainerIds.Select(
cid => ContainerId.FromHash(cid.Value.ToByteArray())
).ToArray();
}
public async Task<ContainerId> CreateContainerAsync(ModelsV2.Container container)
{
var createContainerResponse = await _client.CreateContainerAsync(container.ToGrpcMessage());
return ContainerId.FromHash(createContainerResponse.Body.ContainerId.Value.ToByteArray());
}
public async Task<ModelsV2.Container> GetContainerAsync(ContainerId containerId)
{
var getContainerResponse = await _client.GetContainerAsync(containerId.ToGrpcMessage());
return getContainerResponse.Body.Container.ToModel();
}
public async Task DeleteContainerAsync(ContainerId containerId)
{
var deleteContainerResponse = await _client.DeleteContainerAsync(containerId.ToGrpcMessage());
}
public async Task<ObjectHeader> GetObjectHeadAsync(ContainerId containerId, ObjectId objectId)
{
var getObjectHeadResponse = await _client.GetObjectHeadAsync(
containerId.ToGrpcMessage(),
objectId.ToGrpcMessage()
);
return getObjectHeadResponse.Body.Header.Header.ToModel();
}
public async Task<ModelsV2.Object> GetObjectAsync(ContainerId containerId, ObjectId objectId)
{
var obj = await _client.GetObjectAsync(
containerId.ToGrpcMessage(),
objectId.ToGrpcMessage()
);
return obj.ToModel();
}
public async Task<ObjectId> PutObjectAsync(ObjectHeader header, Stream payload)
{
var putObjectResponse = await _client.PutObjectAsync(header.ToGrpcMessage(), payload);
return ObjectId.FromHash(putObjectResponse.Body.ObjectId.Value.ToByteArray());
}
public async Task<ObjectId> PutObjectAsync(ObjectHeader header, byte[] payload)
{
var putObjectResponse = await _client.PutObjectAsync(header.ToGrpcMessage(), new MemoryStream(payload));
return ObjectId.FromHash(putObjectResponse.Body.ObjectId.Value.ToByteArray());
}
public async Task DeleteObjectAsync(ContainerId containerId, ObjectId objectId)
{
var deleteObjectResponse = await _client.DeleteObjectAsync(
containerId.ToGrpcMessage(),
objectId.ToGrpcMessage()
);
}
}