using Grpc.Core;
using MxGateway.Contracts.Proto.Galaxy;
namespace MxGateway.Client;
///
/// gRPC implementation of IGalaxyRepositoryClientTransport.
///
internal sealed class GrpcGalaxyRepositoryClientTransport(
MxGatewayClientOptions options,
GalaxyRepository.GalaxyRepositoryClient rawClient) : IGalaxyRepositoryClientTransport
{
///
/// Gets the gateway client options.
///
public MxGatewayClientOptions Options { get; } = options;
///
/// Gets the underlying gRPC client.
///
public GalaxyRepository.GalaxyRepositoryClient RawClient { get; } = rawClient;
///
GalaxyRepository.GalaxyRepositoryClient? IGalaxyRepositoryClientTransport.RawClient => RawClient;
///
public async Task TestConnectionAsync(
TestConnectionRequest request,
CallOptions callOptions)
{
try
{
return await RawClient.TestConnectionAsync(request, callOptions)
.ResponseAsync
.ConfigureAwait(false);
}
catch (RpcException exception)
{
throw RpcExceptionMapper.Map(exception, callOptions.CancellationToken);
}
}
///
public async Task GetLastDeployTimeAsync(
GetLastDeployTimeRequest request,
CallOptions callOptions)
{
try
{
return await RawClient.GetLastDeployTimeAsync(request, callOptions)
.ResponseAsync
.ConfigureAwait(false);
}
catch (RpcException exception)
{
throw RpcExceptionMapper.Map(exception, callOptions.CancellationToken);
}
}
///
public async Task DiscoverHierarchyAsync(
DiscoverHierarchyRequest request,
CallOptions callOptions)
{
try
{
return await RawClient.DiscoverHierarchyAsync(request, callOptions)
.ResponseAsync
.ConfigureAwait(false);
}
catch (RpcException exception)
{
throw RpcExceptionMapper.Map(exception, callOptions.CancellationToken);
}
}
///
public async IAsyncEnumerable WatchDeployEventsAsync(
WatchDeployEventsRequest request,
CallOptions callOptions,
[System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
{
CancellationToken effectiveCancellationToken = cancellationToken.CanBeCanceled
? cancellationToken
: callOptions.CancellationToken;
using AsyncServerStreamingCall call = RawClient.WatchDeployEvents(request, callOptions);
IAsyncStreamReader responseStream = call.ResponseStream;
while (true)
{
DeployEvent? deployEvent;
try
{
if (!await responseStream.MoveNext(effectiveCancellationToken).ConfigureAwait(false))
{
break;
}
deployEvent = responseStream.Current;
}
catch (RpcException exception)
{
throw RpcExceptionMapper.Map(exception, effectiveCancellationToken);
}
yield return deployEvent;
}
}
///
IAsyncEnumerable IGalaxyRepositoryClientTransport.WatchDeployEventsAsync(
WatchDeployEventsRequest request,
CallOptions callOptions)
{
return WatchDeployEventsAsync(request, callOptions);
}
}