Initial commit carried over from private repo. This is V2.
This commit is contained in:
150
code/MessengerApi.Contracts/Client/MessengerClient.cs
Normal file
150
code/MessengerApi.Contracts/Client/MessengerClient.cs
Normal file
@ -0,0 +1,150 @@
|
||||
using portaloggy;
|
||||
using System.Text;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace MessengerApi.Contracts
|
||||
{
|
||||
public class MessengerClient : IMessengerClient
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
private DateTime _lastReceivedUtc;
|
||||
private Credentials _credentials;
|
||||
|
||||
public MessengerClient(Credentials credentials, HttpClient httpClient = null, ILogger logger = null)
|
||||
{
|
||||
_credentials = credentials;
|
||||
_httpClient = httpClient ?? new HttpClient();
|
||||
_logger = logger ?? new ConsoleLogger();
|
||||
_lastReceivedUtc = DateTime.MinValue.ToUniversalTime();
|
||||
}
|
||||
|
||||
public IEnumerable<InboxMessage> GetMessages()
|
||||
{
|
||||
var since = Uri.EscapeDataString(this._lastReceivedUtc.ToString("o"));
|
||||
var url = $"{_credentials.ApiUrl}/receive?sinceUtc={since}";
|
||||
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())
|
||||
{
|
||||
if (item["id"].GetValue<int>() == -1)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
messages.Add(new InboxMessage
|
||||
{
|
||||
Id = item["id"].GetValue<Guid>(),
|
||||
Payload = item["payload"].ToJsonString(),
|
||||
PayloadId = item["payloadId"].ToJsonString(),
|
||||
PayloadType = item["payloadType"].ToJsonString(),
|
||||
Sender = item["sender"].GetValue<Guid>(),
|
||||
SenderTimestamp = item["senderTimestamp"].GetValue<DateTime>()
|
||||
});
|
||||
}
|
||||
|
||||
_lastReceivedUtc = DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(10));
|
||||
_logger.Debug($"Received {messages.Count} messages and last check timestamp is set to {_lastReceivedUtc.ToString("s")}.");
|
||||
|
||||
return messages.ToArray();
|
||||
}
|
||||
|
||||
public void 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));
|
||||
}
|
||||
|
||||
var content = new StringContent(body.ToString(), Encoding.UTF8, "application/json");
|
||||
|
||||
var url = $"{_credentials.ApiUrl}/send";
|
||||
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 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)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user