-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SendingService, test, integrationtests++
- Loading branch information
Showing
29 changed files
with
793 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/Altinn.Notifications.Sms.Core/Configuration/TopicSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Altinn.Notifications.Sms.Core.Configuration; | ||
|
||
/// <summary> | ||
/// Configuration object used to hold topic names for core services to publish to in Kafka. | ||
/// </summary> | ||
public class TopicSettings | ||
{ | ||
/// <summary> | ||
/// The name of the sms status updated topic | ||
/// </summary> | ||
public string SmsStatusUpdatedTopicName { get; set; } = string.Empty; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Altinn.Notifications.Sms.Core/Sending/ISendingService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace Altinn.Notifications.Sms.Core.Sending; | ||
|
||
/// <summary> | ||
/// Describes the required public method of the sms service. | ||
/// </summary> | ||
public interface ISendingService | ||
{ | ||
/// <summary> | ||
/// Send an sms | ||
/// </summary> | ||
/// <param name="sms">The details for an sms to be sent.</param> | ||
/// <returns>A task representing the asynchronous operation</returns> | ||
Task SendAsync(Sms sms); | ||
} |
56 changes: 56 additions & 0 deletions
56
src/Altinn.Notifications.Sms.Core/Sending/SendingService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using Altinn.Notifications.Sms.Core.Configuration; | ||
using Altinn.Notifications.Sms.Core.Dependencies; | ||
using Altinn.Notifications.Sms.Core.Shared; | ||
using Altinn.Notifications.Sms.Core.Status; | ||
|
||
namespace Altinn.Notifications.Sms.Core.Sending; | ||
|
||
/// <summary> | ||
/// Service responsible for handling sms sending requests. | ||
/// </summary> | ||
public class SendingService : ISendingService | ||
{ | ||
private readonly ISmsClient _smsClient; | ||
private readonly TopicSettings _settings; | ||
private readonly ICommonProducer _producer; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SendingService"/> class. | ||
/// </summary> | ||
/// <param name="smsClient">A client that can perform actual sms sending.</param> | ||
/// <param name="producer">A kafka producer.</param> | ||
/// <param name="settings">The topic settings.</param> | ||
public SendingService(ISmsClient smsClient, ICommonProducer producer, TopicSettings settings) | ||
{ | ||
_smsClient = smsClient; | ||
_producer = producer; | ||
_settings = settings; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task SendAsync(Sms sms) | ||
{ | ||
Result<string, SmsClientErrorResponse> result = await _smsClient.SendAsync(sms); | ||
|
||
SendOperationResult operationResult = new SendOperationResult | ||
{ | ||
NotificationId = sms.NotificationId, | ||
}; | ||
|
||
await result.Match( | ||
async gatewayReference => | ||
{ | ||
operationResult.GatewayReference = gatewayReference; | ||
operationResult.SendResult = SmsSendResult.Accepted; | ||
|
||
await _producer.ProduceAsync(_settings.SmsStatusUpdatedTopicName, operationResult.Serialize()); | ||
}, | ||
async smsSendFailResponse => | ||
{ | ||
operationResult.GatewayReference = string.Empty; | ||
operationResult.SendResult = smsSendFailResponse.SendResult; | ||
|
||
await _producer.ProduceAsync(_settings.SmsStatusUpdatedTopicName, operationResult.Serialize()); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,83 @@ | ||
namespace Altinn.Notifications.Sms.Core.Sending | ||
using System.Text.Json; | ||
|
||
namespace Altinn.Notifications.Sms.Core.Sending; | ||
|
||
/// <summary> | ||
/// Class representing an sms message | ||
/// </summary> | ||
public class Sms | ||
{ | ||
/// <summary> | ||
/// Class representing an sms message | ||
/// Gets or sets the id of the sms. | ||
/// </summary> | ||
public Guid NotificationId { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the recipient of the sms message | ||
/// </summary> | ||
public string Recipient { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Gets or sets the sender of the sms message | ||
/// </summary> | ||
/// <remarks> | ||
/// Can be a literal string or a phone number | ||
/// </remarks> | ||
public string Sender { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Gets or sets the contents of the sms message | ||
/// </summary> | ||
public string Message { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Sms"/> class. | ||
/// </summary> | ||
public class Sms | ||
public Sms(Guid notificationId, string recipient, string sender, string message) | ||
{ | ||
/// <summary> | ||
/// Gets or sets the contents of the sms message | ||
/// </summary> | ||
public string Message { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Gets or sets the recipient of the sms message | ||
/// </summary> | ||
public string Recipient { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Gets or sets the sender of the sms message | ||
/// </summary> | ||
/// <remarks> | ||
/// Can be a literal string or a phone number | ||
/// </remarks> | ||
public string Sender { get; set; } = string.Empty; | ||
NotificationId = notificationId; | ||
Recipient = recipient; | ||
Sender = sender; | ||
Message = message; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Sms"/> class. | ||
/// </summary> | ||
public Sms() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Try to parse a json string into a<see cref="Sms"/> | ||
/// </summary> | ||
public static bool TryParse(string input, out Sms value) | ||
{ | ||
Sms? parsedOutput; | ||
value = new Sms(); | ||
|
||
if (string.IsNullOrEmpty(input)) | ||
{ | ||
return false; | ||
} | ||
|
||
try | ||
{ | ||
parsedOutput = JsonSerializer.Deserialize<Sms>( | ||
input!, | ||
new JsonSerializerOptions() | ||
{ | ||
PropertyNameCaseInsensitive = true | ||
}); | ||
|
||
value = parsedOutput!; | ||
return value.NotificationId != Guid.Empty; | ||
} | ||
catch | ||
{ | ||
// try parse, we simply return false if fails | ||
} | ||
|
||
return false; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/Altinn.Notifications.Sms.Core/Status/SendOperationResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Altinn.Notifications.Sms.Core.Status; | ||
|
||
/// <summary> | ||
/// A class representing a send operation update object | ||
/// </summary> | ||
public class SendOperationResult | ||
{ | ||
/// <summary> | ||
/// The notification id | ||
/// </summary> | ||
public Guid NotificationId { get; set; } | ||
|
||
/// <summary> | ||
/// The reference to the sending in sms gateway | ||
/// </summary> | ||
public string GatewayReference { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// The sms send result | ||
/// </summary> | ||
public SmsSendResult? SendResult { get; set; } | ||
|
||
/// <summary> | ||
/// Json serializes the <see cref="SendOperationResult"/> | ||
/// </summary> | ||
public string Serialize() | ||
{ | ||
return JsonSerializer.Serialize( | ||
this, | ||
new JsonSerializerOptions | ||
{ | ||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
Converters = { new JsonStringEnumConverter() } | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.