1
0
tinklu-paslaugos/Lab4/ChatRoomContract/ChatRoomRestClient.cs
2024-12-07 17:21:09 +02:00

124 lines
3.3 KiB
C#

using NswagGenerated;
namespace ChatRoomContract;
/// <summary>
/// Wrapper around generated NSwag client
/// </summary>
public class ChatRoomRestClient : IChatRoomService
{
NswagGenerated.NswagChatRoomClient nswag;
/// <summary>
/// Chat room client constructor
/// </summary>
/// <param name="baseUrl">Server base url</param>
/// <param name="httpClient">HTTP Client</param>
public ChatRoomRestClient(string baseUrl, HttpClient httpClient)
{
nswag = new NswagGenerated.NswagChatRoomClient(baseUrl, httpClient);
}
/// <summary>
/// Approve a message
/// </summary>
/// <param name="messageId">Message ID</param>
public void ApproveMessage(int messageId)
{
nswag.ApproveMessage(messageId);
}
/// <summary>
/// Get timestamp until when the client is blocked
/// </summary>
/// <param name="clientId">Client ID</param>
/// <returns>Optional datetime object</returns>
public DateTime? GetBlockedUntil(int clientId)
{
try
{
var offset = nswag.GetBlockedUntil(clientId);
return offset.DateTime;
}
catch (ApiException e)
{
if (e.StatusCode == 204)
{
return null;
}
throw;
}
}
/// <summary>
/// Get the next message which hasn't been approved or rejected
/// </summary>
/// <returns>Message object. Returns null if there is no message</returns>
public Message? GetNewMessage()
{
NswagGenerated.Message message;
try
{
message = nswag.GetNewMessage();
}
catch (ApiException e)
{
if (e.StatusCode == 204)
{
return null;
}
throw;
}
return new Message
{
id = message.Id,
contents = message.Contents,
needsToBeCensored = message.NeedsToBeCensored
};
}
/// <summary>
/// Get number of strikes a participant has
/// </summary>
/// <param name="clientId">Client ID</param>
/// <returns>Number of strikes</returns>
public int GetStrikes(int clientId)
{
return nswag.GetStrikes(clientId);
}
/// <summary>
/// Register client with a name
/// </summary>
/// <param name="name">Name of client, can be duplicate between clients</param>
/// <returns>Client ID</returns>
public int RegisterClient(string name)
{
return nswag.RegisterClient(name);
}
/// <summary>
/// Reject a message
/// </summary>
/// <param name="messageId">Message ID</param>
public void RejectMessage(int messageId)
{
nswag.RejectMessage(messageId);
}
/// <summary>
/// Send a message, will be given to a moderator to be approved
/// </summary>
/// <param name="clientId">Client ID</param>
/// <param name="contents">Message contents</param>
/// <param name="needsToBeCensored">Does this message need to be censored?</param>
/// <returns>Was sending successful, can fail if user is blocked</returns>
public bool SendMessage(int clientId, string contents, bool needsToBeCensored)
{
return nswag.SendMessage(clientId, contents, needsToBeCensored);
}
}