Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test/all #2

Merged
merged 5 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Code/Kavenegar.Core/BaseKavenegarApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Kavenegar.Core.Dto.Result;
using Shared.Infrastructure;

namespace Kavenegar.Core;

public class BaseKavenegarApi
{
private const string ApiAddress = "https://api.kavenegar.com/v1";
private readonly IHttpClientHelper _httpClientHelper;

protected BaseKavenegarApi(
IHttpClientHelper httpClientHelper,
string apiKey)
{
_httpClientHelper = httpClientHelper;
_httpClientHelper.BaseAddress = Path.Combine(ApiAddress, apiKey);
}

protected async Task<T?> RequestSender<T>(
string requestUri,
object? body,
Dictionary<string, object?>? queryParams,
CancellationToken cancellationToken)
{
var httpResponseMessage = await _httpClientHelper.PostAsync(
requestUri,
body,
queryParams,
cancellationToken);

var deserializedObj = await httpResponseMessage.Deserialize<ResultDto<T>>(cancellationToken);

return deserializedObj == null ? default : deserializedObj.Value;
}
}
27 changes: 27 additions & 0 deletions Code/Kavenegar.Core/DependencyManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.Extensions.DependencyInjection;
using Shared.Infrastructure;

namespace Kavenegar.Core;

public static class DependencyManager
{
public static IServiceCollection AddKavenegar(
this IServiceCollection serviceCollection,
string apiKey)
{
serviceCollection.AddScoped<HttpClient>();
serviceCollection.AddScoped<IHttpClientHelper, HttpClientHelper>();

serviceCollection.AddScoped<IKavenegarProfileApi, KavenegarProfileApi>(
serviceProvider => new KavenegarProfileApi(
serviceProvider.GetRequiredService<IHttpClientHelper>(),
apiKey));

serviceCollection.AddScoped<IKavenegarMessageSender, KavenegarMessageSender>(
serviceProvider => new KavenegarMessageSender(
serviceProvider.GetRequiredService<IHttpClientHelper>(),
apiKey));

return serviceCollection;
}
}
16 changes: 16 additions & 0 deletions Code/Kavenegar.Core/Dto/Message/MessageInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Kavenegar.Core.Enums;

namespace Kavenegar.Core.Dto.Message;

public class MessageInfo
{
public MessageInfo(
string message)
{
Message = message;
}

public string? Sender { get; set; }
public string Message { get; set; }
public MessageType Type { get; set; } = MessageType.Flash;
}
16 changes: 16 additions & 0 deletions Code/Kavenegar.Core/Dto/Message/SendMessageInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Kavenegar.Core.Dto.Message;

public class SendMessageInfo
{
public SendMessageInfo(
MessageInfo messageInfo,
string receptor)
{
MessageInfo = messageInfo;
Receptor = receptor;
}

public MessageInfo MessageInfo { get; set; }
public string Receptor { get; set; }
public string LocalMessageId { get; set; } = null!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

public class SendMultiMessageRequest
{
public IEnumerable<SendMessageInfo> SendMessageInfos { get; set; } = null!;
public SendMultiMessageRequest(
IEnumerable<SendMessageInfo> sendMessageInfos)
{
SendMessageInfos = sendMessageInfos;
}

public IEnumerable<SendMessageInfo> SendMessageInfos { get; set; }
public DateTime Date { get; set; }
public bool Hide { get; set; }
}
23 changes: 23 additions & 0 deletions Code/Kavenegar.Core/Dto/Message/SendSingleMessageRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Kavenegar.Core.Dto.Message;

public class SendSingleMessageRequest
{
public SendSingleMessageRequest(
MessageInfo messageInfo,
Dictionary<string, string?> receptorLocalMessageIds)
{
MessageInfo = messageInfo;
ReceptorLocalMessageIds = receptorLocalMessageIds;
}

public MessageInfo MessageInfo { get; set; }

/// <summary>
/// Key is receptor
/// Value is local message id
/// </summary>
public Dictionary<string, string?> ReceptorLocalMessageIds { get; set; }

public DateTime? Date { get; set; }
public bool Hide { get; set; }
}
25 changes: 25 additions & 0 deletions Code/Kavenegar.Core/Dto/Message/VerifyLookupRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Kavenegar.Core.Enums;

namespace Kavenegar.Core.Dto.Message;

public class VerifyLookupRequest
{
public VerifyLookupRequest(
string receptor,
string template,
string token1)
{
Receptor = receptor;
Template = template;
Token1 = token1;
}

public string Receptor { get; set; }
public string Template { get; set; }
public string Token1 { get; set; }
public string? Token2 { get; set; }
public string? Token3 { get; set; }
public string? Token4 { get; set; }
public string? Token5 { get; set; }
public VerifyLookupType? VerifyLookupType { get; set; } = null!;
}
32 changes: 32 additions & 0 deletions Code/Kavenegar.Core/Dto/Result/SendResultDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Text.Json.Serialization;
using Kavenegar.Core.Enums;
using Shared.Infrastructure;

namespace Kavenegar.Core.Dto.Result;

public class SendResultDto
{
public long MessageId { get; set; }
public string Message { get; set; } = null!;

[JsonPropertyName("Status")]
public int StatusNumber { get; set; }

public MessageStatus MessageStatus => StatusCaster(StatusNumber);
public string StatusText { get; set; } = null!;
public string Sender { get; set; } = null!;
public string Receptor { get; set; } = null!;

[JsonPropertyName("Date")]
public long UnixDate { get; set; }

public DateTime DateTime => UnixDate.ToDateTime();
public int Cost { get; set; }

private MessageStatus StatusCaster(
int status)
{
if (Enum.IsDefined(typeof(MessageStatus), StatusNumber)) return (MessageStatus)status;
return status == 5 ? MessageStatus.SentToCenter : MessageStatus.Unknown;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ namespace Kavenegar.Core.Enums;

public enum MessageStatus
{
Unknown = 0,
Queued = 1,
Scheduled = 2,
SentToCenter = 4,
SentToCenter2 = 5,
Delivered = 10,
Undelivered = 11,
Canceled = 13,
Expand Down
75 changes: 75 additions & 0 deletions Code/Kavenegar.Core/IKavenegarMessageSender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Kavenegar.Core.Dto.Message;
using Kavenegar.Core.Dto.Result;
using Kavenegar.Core.Enums;

namespace Kavenegar.Core;

public interface IKavenegarMessageSender
{
/// <summary>
/// Send one message for only the receptor
/// </summary>
/// <param name="message">Message text.</param>
/// <param name="receptor">Who receives message.</param>
/// <param name="sender">Number to send message if you set it empty the default number will be used.</param>
/// <param name="localMessageId">Unique id which you set for the id.</param>
/// <param name="dateTime">The date time you wand the message to be sent. If it is null message will be send asap.</param>
/// <param name="hide">If true receptor number will be hidden.</param>
/// <param name="messageType">Type of message.</param>
/// <param name="cancellationToken">Token to cancel request.</param>
/// <returns></returns>
Task<SendResultDto?> Send(
string message,
string receptor,
string sender = "",
string localMessageId = "",
DateTime? dateTime = null,
bool hide = false,
MessageType messageType = MessageType.AppMemory,
CancellationToken cancellationToken = default);

/// <summary>
/// Send message text for each receptor with the localMessageId
/// </summary>
/// <param name="message">Message text.</param>
/// <param name="receptors">Key for receptor and value is for local message id.</param>
/// <param name="sender">Number to send message if you set it empty the default number will be used.</param>
/// <param name="dateTime">The date time you wand the message to be sent. If it is null message will be send asap.</param>
/// <param name="hide">If true receptor number will be hidden.</param>
/// <param name="messageType">Type of message.</param>
/// <param name="cancellationToken">Token to cancel request.</param>
/// <returns></returns>
Task<List<SendResultDto>?> Send(
string message,
Dictionary<string, string?> receptors,
string sender = "",
DateTime? dateTime = null,
bool hide = false,
MessageType messageType = MessageType.AppMemory,
CancellationToken cancellationToken = default);

public Task<List<SendResultDto>?> Send(
SendSingleMessageRequest message,
CancellationToken cancellationToken = default);

Task<List<SendResultDto>?> Send(
IEnumerable<SendMessageInfo> sendMessageInfos,
bool hide = false,
DateTime? dateTime = null,
CancellationToken cancellationToken = default);

Task<List<SendResultDto>?> Send(
SendMultiMessageRequest messages,
CancellationToken cancellationToken = default);

Task<SendResultDto?> VerifyLookup(
string receptor,
string template,
string token1,
string? token2 = null,
string? token3 = null,
string? token4 = null,
string? token5 = null,
VerifyLookupType? type = null,
CancellationToken cancellationToken = default);
}
80 changes: 80 additions & 0 deletions Code/Kavenegar.Core/IKavenegarProfileApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Kavenegar.Core.Dto.Result;
using Kavenegar.Core.Enums;

namespace Kavenegar.Core;

public interface IKavenegarProfileApi
{
Task<StatusMessageDto?> Status(
string messageId,
CancellationToken cancellationToken = default);

Task<List<StatusMessageDto>?> Status(
IEnumerable<string> messageIds,
CancellationToken cancellationToken = default);

Task<LocalStatusDto?> StatusLocalMessageId(
string messageId,
CancellationToken cancellationToken = default);

Task<List<LocalStatusDto>?> StatusLocalMessageId(
IEnumerable<string> messageIds,
CancellationToken cancellationToken = default);

Task<SendResultDto?> Select(
string messageId,
CancellationToken cancellationToken = default);

Task<List<SendResultDto>?> Select(
IEnumerable<string> messageIds,
CancellationToken cancellationToken = default);

Task<List<SendResultDto>?> SelectOutbox(
DateTime startDate,
DateTime? endDate = null,
string? sender = null,
CancellationToken cancellationToken = default);

Task<List<SendResultDto>?> LatestOutbox(
int? pageSize = null,
string? sender = null,
CancellationToken cancellationToken = default);

Task<CountOutboxDto?> CountOutbox(
DateTime startDate,
DateTime? endDate = null,
MessageStatus? status = null,
CancellationToken cancellationToken = default);

Task<StatusMessageDto?> Cancel(
string messageId,
CancellationToken cancellationToken = default);

Task<List<StatusMessageDto>?> Cancel(
IEnumerable<string> ids,
CancellationToken cancellationToken = default);

Task<List<ReceivedMessageDto>?> Receive(
string line,
bool isRead,
CancellationToken cancellationToken = default);

Task<CountInboxDto?> CountInbox(
DateTime startDate,
DateTime? endDate,
string? lineNumber,
bool? isRead,
CancellationToken cancellationToken = default);

Task<AccountInfoDto?> AccountInfo(
CancellationToken cancellationToken = default);

Task<AccountConfigDto?> AccountConfig(
string? apiLogs = null,
string? dailyReport = null,
string? debugMode = null,
string? defaultSender = null,
int? minCreditAlarm = null,
string? resendFailed = null,
CancellationToken cancellationToken = default);
}
Loading