Implementation carried over from v1.
All checks were successful
Pack and Push NuGet Package / publish (push) Successful in 43s

This commit is contained in:
2025-07-05 05:11:04 +02:00
commit 9766124241
11 changed files with 689 additions and 0 deletions

View File

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<AssemblyVersion>$([System.DateTime]::UtcNow.ToString("yyyy.MM.dd.HHmm"))</AssemblyVersion>
<PackageVersion>$([System.DateTime]::UtcNow.ToString("yyyy.MM.dd.HHmm"))</PackageVersion>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>$(AssemblyName)</Title>
<BaseOutputPath>..\out\</BaseOutputPath>
<PackageTags>logging;log;logger</PackageTags>
<PackageLicenseExpression>mit-0</PackageLicenseExpression>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MessengerApi.SubscriptionClient" />
<PackageReference Include="portaloggy" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,71 @@
using portaloggy;
using MessengerApi.Model.Messages;
using MessengerApi.Model;
namespace MessengerApi
{
public interface IQueryClient : IDisposable
{
Task<InboxMessage> Query(OutboxMessage message);
}
public class QueryClient : SubscriptionClient, IQueryClient
{
private readonly int _timeoutInSeconds;
public QueryClient(
Credentials credentials,
HttpClient httpClient = null,
ILogger logger = null,
int maximumPermittedWaitTimeInSeconds = 10) :base(credentials, httpClient, logger)
{
this._timeoutInSeconds = maximumPermittedWaitTimeInSeconds;
}
public async Task<InboxMessage> Query(OutboxMessage message)
{
try
{
var messageTypeMask = $"{message.PayloadType}-{Guid.NewGuid()}";
var maskedMessage = new OutboxMessage
{
Payload = message.Payload,
LifespanInSeconds = message.LifespanInSeconds,
PayloadType = messageTypeMask,
ToUserId = message.ToUserId
};
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(this._timeoutInSeconds));
var sub = this.Subscribe(messageTypeMask);
var result = (InboxMessage)null;
sub.OnMessage += (source, message) =>
{
result = message;
cts.Cancel();
};
this.SendMessage(maskedMessage);
while (!cts.IsCancellationRequested)
{
await Task.Delay(TimeSpan.FromSeconds(1));
}
this.Unsubscribe(sub);
if (result == null)
{
throw new InvalidOperationException("Couldn't retrieve response.");
}
result.PayloadType = message.PayloadType;
return result;
}
catch
{
throw;
}
}
}
}