Files
Wino-Mail/Wino.Core/Http/GmailClientMessageHandler.cs

33 lines
1.2 KiB
C#
Raw Normal View History

using System.Net.Http;
2024-04-18 01:44:37 +02:00
using System.Threading;
using System.Threading.Tasks;
using Google.Apis.Http;
using Wino.Core.Domain.Entities.Shared;
using Wino.Core.Domain.Interfaces;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
namespace Wino.Core.Http;
internal class GmailClientMessageHandler : ConfigurableMessageHandler
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
private readonly IGmailAuthenticator _gmailAuthenticator;
private readonly MailAccount _mailAccount;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public GmailClientMessageHandler(IGmailAuthenticator gmailAuthenticator, MailAccount mailAccount) : base(new HttpClientHandler())
{
_gmailAuthenticator = gmailAuthenticator;
_mailAccount = mailAccount;
}
2025-02-16 11:54:23 +01:00
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
// This call here will automatically trigger Google Auth's interactive login if the token is not found.
// or refresh the token based on the FileDataStore.
2025-02-16 11:54:23 +01:00
var tokenInformation = await _gmailAuthenticator.GetTokenInformationAsync(_mailAccount);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", tokenInformation.AccessToken);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return await base.SendAsync(request, cancellationToken);
2024-04-18 01:44:37 +02:00
}
}