Client library reviewed.
All checks were successful
Pack and Push NuGet Package / publish (push) Successful in 43s
All checks were successful
Pack and Push NuGet Package / publish (push) Successful in 43s
This commit is contained in:
284
code/MessengerApi.Client/Client.cs
Normal file
284
code/MessengerApi.Client/Client.cs
Normal file
@ -0,0 +1,284 @@
|
||||
using MessengerApi.Model;
|
||||
using MessengerApi.Model.Messages;
|
||||
using portaloggy;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace MessengerApi
|
||||
{
|
||||
/// <summary>
|
||||
/// Exists for mocking reason. This is implemented by <see cref="Client"/>.
|
||||
/// </summary>
|
||||
public interface IClient
|
||||
{
|
||||
/// <summary>
|
||||
/// Receives pending messages from the messenger API.
|
||||
/// </summary>
|
||||
/// <param name="credentials">Credentials to the API.</param>
|
||||
IEnumerable<InboxMessage> GetMessages();
|
||||
|
||||
/// <summary>
|
||||
/// Acknowledges message reception to the server.
|
||||
/// </summary>
|
||||
void AckMessage(InboxMessage message);
|
||||
|
||||
/// <summary>
|
||||
/// Acknowledges message reception to the server.
|
||||
/// </summary>
|
||||
/// <param name="messageId"></param>
|
||||
void AckMessage(Guid messageId);
|
||||
|
||||
/// <summary>
|
||||
/// Sends a message.
|
||||
/// </summary>
|
||||
/// <param name="credentials">Credentials to the API.</param>
|
||||
Guid SendMessage(OutboxMessage outboxMessage);
|
||||
|
||||
/// <summary>
|
||||
/// Returns user ids for allowed message recipients.
|
||||
/// </summary>
|
||||
Contact[] GetYellowPages();
|
||||
|
||||
/// <summary>
|
||||
/// Returns number of pending messages, waiting for delivery.
|
||||
/// </summary>
|
||||
int Peek();
|
||||
|
||||
/// <summary>
|
||||
/// Gets delivery state of a message.
|
||||
/// </summary>
|
||||
State Verify(Guid messageId);
|
||||
}
|
||||
|
||||
public class Client : IClient
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
private Credentials _credentials;
|
||||
|
||||
public Client(Credentials credentials, HttpClient httpClient = null, ILogger logger = null)
|
||||
{
|
||||
_credentials = credentials;
|
||||
_httpClient = httpClient ?? new HttpClient();
|
||||
_logger = logger ?? new ConsoleLogger();
|
||||
}
|
||||
|
||||
public IEnumerable<InboxMessage> GetMessages()
|
||||
{
|
||||
var url = $"{_credentials.ApiUrl}/receive";
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
||||
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _credentials.ApiKey);
|
||||
|
||||
_logger.Debug($"Sending query to {url} with content {request.ToString()} to obtain messages.");
|
||||
|
||||
var response = _httpClient.Send(request);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
_logger.Error(response.ReasonPhrase);
|
||||
throw new HttpRequestException("Can't receive.", null, response.StatusCode);
|
||||
}
|
||||
|
||||
var responseContent = response.Content
|
||||
.ReadAsStringAsync()
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(responseContent))
|
||||
{
|
||||
_logger.Debug($"Received response of {responseContent}.");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(responseContent))
|
||||
{
|
||||
return Enumerable.Empty<InboxMessage>().ToArray();
|
||||
}
|
||||
|
||||
var json = JsonNode.Parse(responseContent);
|
||||
var messages = new List<InboxMessage>();
|
||||
|
||||
foreach (var item in json["messages"].AsArray())
|
||||
{
|
||||
messages.Add(new InboxMessage
|
||||
{
|
||||
Id = item["id"].GetValue<Guid>(),
|
||||
Payload = item["payload"].ToJsonString(),
|
||||
PayloadType = item["payloadType"].ToJsonString(),
|
||||
Sender = item["sender"].GetValue<Guid>(),
|
||||
});
|
||||
}
|
||||
|
||||
_logger.Debug($"Received {messages.Count} messages.");
|
||||
|
||||
return messages.ToArray();
|
||||
}
|
||||
|
||||
public Guid SendMessage(OutboxMessage outboxMessage)
|
||||
{
|
||||
var body = new JsonObject();
|
||||
|
||||
if(outboxMessage.ToUserId.HasValue)
|
||||
{
|
||||
body.Add("toUserId", JsonValue.Create<Guid>(outboxMessage.ToUserId.Value));
|
||||
}
|
||||
|
||||
if(outboxMessage.Payload != null)
|
||||
{
|
||||
body.Add("payload", JsonValue.Create<string>(outboxMessage.Payload));
|
||||
}
|
||||
|
||||
if(outboxMessage.PayloadType != null)
|
||||
{
|
||||
body.Add("payloadType", JsonValue.Create<string>(outboxMessage.PayloadType));
|
||||
}
|
||||
|
||||
if(outboxMessage.LifespanInSeconds != null)
|
||||
{
|
||||
body.Add("lifespanInSeconds", JsonValue.Create<int>(outboxMessage.LifespanInSeconds.Value));
|
||||
}
|
||||
|
||||
var content = new StringContent(body.ToString(), Encoding.UTF8, "application/json");
|
||||
|
||||
var url = $"{_credentials.ApiUrl}/send";
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, url);
|
||||
request.Content = content;
|
||||
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _credentials.ApiKey);
|
||||
|
||||
_logger.Debug($"Sending query to {url} with content {body.ToString()} to obtain messages.");
|
||||
|
||||
var response = _httpClient.Send(request);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
_logger.Error(response.ReasonPhrase);
|
||||
throw new HttpRequestException("Can't send.", null, response.StatusCode);
|
||||
}
|
||||
|
||||
var responseString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
|
||||
var guid = JsonSerializer.Deserialize<Guid>(responseString);
|
||||
return guid;
|
||||
}
|
||||
|
||||
public Contact[] GetYellowPages()
|
||||
{
|
||||
var url = $"{_credentials.ApiUrl}/yellowpages";
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
||||
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _credentials.ApiKey);
|
||||
|
||||
_logger.Debug($"Sending query to {url} with content {request.ToString()}.");
|
||||
|
||||
var response = _httpClient.Send(request);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
_logger.Error(response.ReasonPhrase);
|
||||
throw new HttpRequestException("Can't receive.", null, response.StatusCode);
|
||||
}
|
||||
|
||||
var responseContent = response.Content
|
||||
.ReadAsStringAsync()
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
|
||||
var json = JsonNode.Parse(responseContent);
|
||||
|
||||
var contacts = json["users"].AsArray().Select(x => new Contact
|
||||
{
|
||||
Id = x["id"].GetValue<Guid>(),
|
||||
Name = x["name"].GetValue<string>()
|
||||
}).ToArray();
|
||||
|
||||
return contacts;
|
||||
}
|
||||
|
||||
public void AckMessage(InboxMessage message)
|
||||
{
|
||||
this.AckMessage(message.Id);
|
||||
}
|
||||
|
||||
public void AckMessage(Guid messageId)
|
||||
{
|
||||
var body = new JsonObject
|
||||
{
|
||||
{ "messageId", JsonValue.Create<Guid>(messageId) }
|
||||
};
|
||||
|
||||
var content = new StringContent(body.ToString(), Encoding.UTF8, "application/json");
|
||||
|
||||
var url = $"{_credentials.ApiUrl}/ack";
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, url);
|
||||
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _credentials.ApiKey);
|
||||
|
||||
_logger.Debug($"Sending query to {url} with content {body.ToString()} to obtain messages.");
|
||||
|
||||
var response = _httpClient.Send(request);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
_logger.Error(response.ReasonPhrase);
|
||||
throw new HttpRequestException("Can't send.", null, response.StatusCode);
|
||||
}
|
||||
}
|
||||
|
||||
public int Peek()
|
||||
{
|
||||
var url = $"{_credentials.ApiUrl}/peek";
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
||||
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _credentials.ApiKey);
|
||||
|
||||
_logger.Debug($"Sending query to {url} with content {request.ToString()} to obtain messages.");
|
||||
|
||||
var response = _httpClient.Send(request);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
_logger.Error(response.ReasonPhrase);
|
||||
throw new HttpRequestException("Can't receive.", null, response.StatusCode);
|
||||
}
|
||||
|
||||
var responseContent = response.Content
|
||||
.ReadAsStringAsync()
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(responseContent))
|
||||
{
|
||||
_logger.Debug($"Received response of {responseContent}.");
|
||||
return int.Parse(responseContent);
|
||||
}
|
||||
|
||||
throw new InvalidOperationException("Unreadable response.");
|
||||
}
|
||||
|
||||
public State Verify(Guid messageId)
|
||||
{
|
||||
var url = $"{_credentials.ApiUrl}/verify?messageId={messageId}";
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
||||
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _credentials.ApiKey);
|
||||
|
||||
_logger.Debug($"Sending query to {url} to get message state.");
|
||||
|
||||
var response = _httpClient.Send(request);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
_logger.Error(response.ReasonPhrase);
|
||||
throw new HttpRequestException("Can't receive.", null, response.StatusCode);
|
||||
}
|
||||
|
||||
var responseContent = response.Content
|
||||
.ReadAsStringAsync()
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(responseContent))
|
||||
{
|
||||
return JsonSerializer.Deserialize<State>(responseContent);
|
||||
}
|
||||
|
||||
throw new InvalidOperationException("Unreadable response.");
|
||||
}
|
||||
}
|
||||
}
|
||||
27
code/MessengerApi.Client/MessengerApi.Client.csproj
Normal file
27
code/MessengerApi.Client/MessengerApi.Client.csproj
Normal file
@ -0,0 +1,27 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>disable</Nullable>
|
||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||
<PackageOutputPath>$(OutputPath)</PackageOutputPath>
|
||||
<AssemblyVersion>$([System.DateTime]::UtcNow.ToString("yyyy.MM.dd.HHmm"))</AssemblyVersion>
|
||||
<PackageVersion>$([System.DateTime]::UtcNow.ToString("yyyy.MM.dd.HHmm"))</PackageVersion>
|
||||
<BaseOutputPath>..\out\</BaseOutputPath>
|
||||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
|
||||
<Title>$(AssemblyName)</Title>
|
||||
<PackageProjectUrl>https://gitea.masita.net/mc/messengerapi-tools</PackageProjectUrl>
|
||||
<RepositoryUrl>https://gitea.masita.net/mc/messengerapi-tools</RepositoryUrl>
|
||||
<PackageTags>logging;log;logger</PackageTags>
|
||||
<PackageLicenseExpression>mit-0</PackageLicenseExpression>
|
||||
<Description>Messenger API consumer library for .NET.</Description>
|
||||
<Copyright>mc @ 2024</Copyright>
|
||||
<Authors>mc</Authors>
|
||||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="portaloggy" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
9
code/MessengerApi.Client/Model/Contact.cs
Normal file
9
code/MessengerApi.Client/Model/Contact.cs
Normal file
@ -0,0 +1,9 @@
|
||||
namespace MessengerApi.Model
|
||||
{
|
||||
public class Contact
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
||||
15
code/MessengerApi.Client/Model/Credentials.cs
Normal file
15
code/MessengerApi.Client/Model/Credentials.cs
Normal file
@ -0,0 +1,15 @@
|
||||
namespace MessengerApi.Model
|
||||
{
|
||||
public class Credentials
|
||||
{
|
||||
public string ApiKey { get; private set; }
|
||||
|
||||
public string ApiUrl { get; private set; }
|
||||
|
||||
public Credentials(string apiKey, string apiUrl)
|
||||
{
|
||||
ApiKey = apiKey;
|
||||
ApiUrl = apiUrl;
|
||||
}
|
||||
}
|
||||
}
|
||||
16
code/MessengerApi.Client/Model/Messages/InboxMessage.cs
Normal file
16
code/MessengerApi.Client/Model/Messages/InboxMessage.cs
Normal file
@ -0,0 +1,16 @@
|
||||
namespace MessengerApi.Model.Messages
|
||||
{
|
||||
/// <summary>
|
||||
/// Message when received is inbox. For server apps, this is request-type of message. For clients, this is a response-type of message.
|
||||
/// </summary>
|
||||
public class InboxMessage
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public Guid Sender { get; set; }
|
||||
|
||||
public string PayloadType { get; set; }
|
||||
|
||||
public string Payload { get; set; }
|
||||
}
|
||||
}
|
||||
16
code/MessengerApi.Client/Model/Messages/OutboxMessage.cs
Normal file
16
code/MessengerApi.Client/Model/Messages/OutboxMessage.cs
Normal file
@ -0,0 +1,16 @@
|
||||
namespace MessengerApi.Model.Messages
|
||||
{
|
||||
/// <summary>
|
||||
/// Outbox type of message. A server-app will treat this as a response. A client app will treat this as a request.
|
||||
/// </summary>
|
||||
public class OutboxMessage
|
||||
{
|
||||
public Guid? ToUserId { get; set; }
|
||||
|
||||
public string PayloadType { get; set; }
|
||||
|
||||
public string Payload { get; set; }
|
||||
|
||||
public int? LifespanInSeconds { get; set; }
|
||||
}
|
||||
}
|
||||
13
code/MessengerApi.Client/Model/State.cs
Normal file
13
code/MessengerApi.Client/Model/State.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace MessengerApi.Model
|
||||
{
|
||||
public class State
|
||||
{
|
||||
[JsonPropertyName("isDelivered")]
|
||||
public bool IsDelivered { get; set; }
|
||||
|
||||
[JsonPropertyName("isAcknowledged")]
|
||||
public bool IsAcknowledged { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user