Initial version

This commit is contained in:
2026-06-09 22:27:38 +02:00
parent 5afbf3b01c
commit 5ff2ac8941
57 changed files with 2343 additions and 98 deletions
@@ -0,0 +1,35 @@
using Grpc.Core;
using Grpc.Core.Interceptors;
using EonaCat.gRPC.Proto;
using Microsoft.AspNetCore.Http;
namespace EonaCat.gRPC.Client.Helpers;
public class AuthHeaderInterceptor : Interceptor
{
private readonly IHttpContextAccessor _httpContextAccessor;
public AuthHeaderInterceptor(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context,
AsyncUnaryCallContinuation<TRequest, TResponse> continuation)
{
var metadata = new Metadata();
//var authResponse = AuthService.Authenticate();
var authResponse = new AuthenticationResponse();
metadata.Add("authorization", $"Bearer {authResponse.AccessToken}");
metadata.Add("expiry", $"{authResponse.ExpiresIn}");
var userIdentity = _httpContextAccessor.HttpContext?.User.Identity;
if (userIdentity is { IsAuthenticated: true, Name: { } })
metadata.Add("User", userIdentity.Name);
var callOptions = context.Options.WithHeaders(metadata);
context = new ClientInterceptorContext<TRequest, TResponse>(context.Method, context.Host, callOptions);
return base.AsyncUnaryCall(request, context, continuation);
}
}
@@ -0,0 +1,70 @@
using EonaCat.gRPC.Proto;
using EonaCat.Json;
namespace EonaCat.gRPC.Client.Helpers;
public static class ConsoleExtensions
{
public static void Success(string data, bool disableNewLine = false)
{
Console.ForegroundColor = ConsoleColor.Green;
if (disableNewLine)
Console.Write(data);
else
Console.WriteLine(data);
Console.ResetColor();
}
public static void Error(string message, bool disableNewLine = false)
{
Console.ForegroundColor = ConsoleColor.Red;
if (disableNewLine)
Console.Write(message);
else
Console.WriteLine(message);
Console.ResetColor();
}
public static void PrintResponse<T>(BaseResponse<T> response, Formatting formatting = Formatting.Indented) where T : class?
{
try
{
if (response.IsSuccess)
{
Success("------ Success Response ------");
if (response.Data != null)
{
Success("Response:");
Success(JsonHelper.ToJson(response, formatting));
foreach (var property in response.Data.GetType().GetProperties())
{
var value = property.GetValue(response.Data);
if (value != null)
{
Success($"{property.Name}: {value}");
}
else
{
Success($"{property.Name}: null");
}
}
}
else
{
Success("No data available in the response.");
}
}
else
{
// Print error response
Error("------ Error Response ------");
Error($"Message: {response.Message}");
}
}
catch (Exception e)
{
// do nothing.
}
}
}
@@ -0,0 +1,70 @@
// enhance-your-grpc-client-logs-with-a-generic-logging-interceptor
// https://anthonygiretti.com/2022/08/08/net-6-enhance-your-grpc-client-logs-with-a-generic-logging-interceptor/
using Grpc.Core;
using Grpc.Core.Interceptors;
using Microsoft.Extensions.Logging;
namespace EonaCat.gRPC.Client.Helpers;
public class TracerInterceptor : Interceptor
{
private readonly ILogger<TracerInterceptor> _logger;
public TracerInterceptor(ILoggerFactory logger)
{
_logger = logger.CreateLogger<TracerInterceptor>();
}
public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(
ClientInterceptorContext<TRequest, TResponse> context,
AsyncClientStreamingCallContinuation<TRequest, TResponse> continuation)
where TRequest : class
where TResponse : class
{
_logger.LogDebug($"Calling {context.Method.Name} {context.Method.Type} method at {DateTime.UtcNow} UTC from machine {Environment.MachineName}");
var continued = continuation(context);
return continued;
}
public override AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>(
ClientInterceptorContext<TRequest, TResponse> context,
AsyncDuplexStreamingCallContinuation<TRequest, TResponse> continuation)
where TRequest : class
where TResponse : class
{
_logger.LogDebug($"Calling {context.Method.Name} {context.Method.Type} method at {DateTime.UtcNow} UTC from machine {Environment.MachineName}");
var continued = continuation(context);
return continued;
}
public override AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>(
TRequest request,
ClientInterceptorContext<TRequest, TResponse> context,
AsyncServerStreamingCallContinuation<TRequest, TResponse> continuation)
where TRequest : class
where TResponse : class
{
_logger.LogDebug($"Calling {context.Method.Name} {context.Method.Type} method. Payload received: {request.GetType()} : {request}");
_logger.LogDebug($"Calling {context.Method.Name} {context.Method.Type} method at {DateTime.UtcNow} UTC from machine {Environment.MachineName}");
var continued = continuation(request, context);
return continued;
}
public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(
TRequest request,
ClientInterceptorContext<TRequest, TResponse> context,
AsyncUnaryCallContinuation<TRequest, TResponse> continuation)
where TRequest : class
where TResponse : class
{
_logger.LogDebug($"Calling {context.Method.Name} {context.Method.Type} method. Payload received: {request.GetType()} : {request}");
_logger.LogDebug($"Calling {context.Method.Name} {context.Method.Type} method at {DateTime.UtcNow} UTC from machine {Environment.MachineName}");
var continued = continuation(request, context);
return continued;
}
}