-
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.
Sms client implemented #GCPActive (#9)
* Sms client implemented * added stylecop to integration + core projects * Delete test/Altinn.Notifications.Sms.IntegrationTests/Altinn - Backup.Notifications.Sms.IntegrationTests.csproj * Update src/Altinn.Notifications.Sms.Core/Integrations/Interfaces/ISmsClient.cs * aded wrapper for sms gateway client * added more tests * merged main * Fixed namespaces * fixed stylecop + global supression
- Loading branch information
Showing
25 changed files
with
494 additions
and
60 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 was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
src/Altinn.Notifications.Sms.Core/Dependencies/ISmsClient.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,18 @@ | ||
using Altinn.Notifications.Sms.Core.Sending; | ||
using Altinn.Notifications.Sms.Core.Shared; | ||
|
||
namespace Altinn.Notifications.Sms.Core.Dependencies | ||
{ | ||
/// <summary> | ||
/// This interface describes the public interface of a client able to send sms messages through an sms service. | ||
/// </summary> | ||
public interface ISmsClient | ||
{ | ||
/// <summary> | ||
/// Method for requesting the sending on an sms message. | ||
/// </summary> | ||
/// <param name="sms">The sms to be sent</param> | ||
/// <returns>An id for tracing the sucess of the task or an <see cref="SmsClientErrorResponse"/> it the task fails</returns> | ||
public Task<Result<string, SmsClientErrorResponse>> SendAsync(Sending.Sms sms); | ||
} | ||
} |
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,26 @@ | ||
namespace Altinn.Notifications.Sms.Core.Sending | ||
{ | ||
/// <summary> | ||
/// Class representing an sms message | ||
/// </summary> | ||
public class Sms | ||
{ | ||
/// <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; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Altinn.Notifications.Sms.Core/Sending/SmsClientErrorResponse.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,20 @@ | ||
using Altinn.Notifications.Sms.Core.Status; | ||
|
||
namespace Altinn.Notifications.Sms.Core.Sending | ||
{ | ||
/// <summary> | ||
/// Class representing an error response from an sms client service | ||
/// </summary> | ||
public class SmsClientErrorResponse | ||
{ | ||
/// <summary> | ||
/// Result for the send operation | ||
/// </summary> | ||
public SmsSendResult SendResult { get; set; } | ||
|
||
/// <summary> | ||
/// The error message from the sms client service | ||
/// </summary> | ||
public string? ErrorMessage { get; set; } | ||
} | ||
} |
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,61 @@ | ||
namespace Altinn.Notifications.Sms.Core.Shared; | ||
|
||
/// <summary> | ||
/// A simple implementation of the Result class to handle success XOR failure as separate return | ||
/// values in a type safe way. | ||
/// </summary> | ||
/// <typeparam name="TValue">The type to be assigned to indicate success.</typeparam> | ||
/// <typeparam name="TError">The type to be assigned to indicate failure.</typeparam> | ||
public readonly struct Result<TValue, TError> | ||
{ | ||
private readonly TValue? _value; | ||
private readonly TError? _error; | ||
|
||
private Result(TValue value) | ||
{ | ||
IsError = false; | ||
_value = value; | ||
_error = default; | ||
} | ||
|
||
private Result(TError error) | ||
{ | ||
IsError = true; | ||
_value = default; | ||
_error = error; | ||
} | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether the Result contains an error value. | ||
/// </summary> | ||
public bool IsError { get; } | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether the Result contains a success value. | ||
/// </summary> | ||
public bool IsSuccess => !IsError; | ||
|
||
/// <summary> | ||
/// Implicit operator used when creating an instance of Result when assigning a success value. | ||
/// </summary> | ||
/// <param name="value">An object of the type indicating success.</param> | ||
public static implicit operator Result<TValue, TError>(TValue value) => new(value); | ||
|
||
/// <summary> | ||
/// Implicit operator used when creating an instance of Result when assigning an error value. | ||
/// </summary> | ||
/// <param name="error">An object of the type indicating failure.</param> | ||
public static implicit operator Result<TValue, TError>(TError error) => new(error); | ||
|
||
/// <summary> | ||
/// This method will call either the success OR the failure function based on it's error state. | ||
/// </summary> | ||
/// <typeparam name="TResult">The type to be returned by the given functions.</typeparam> | ||
/// <param name="success">The function to call if Result holds a success value.</param> | ||
/// <param name="failure">The function to call if Result holds an error value.</param> | ||
/// <returns>An instance of the defined type.</returns> | ||
public TResult Match<TResult>( | ||
Func<TValue, TResult> success, | ||
Func<TError, TResult> failure) => | ||
!IsError ? success(_value!) : failure(_error!); | ||
} |
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,13 @@ | ||
namespace Altinn.Notifications.Sms.Core.Status | ||
{ | ||
/// <summary> | ||
/// Enum describing sms send result types | ||
/// </summary> | ||
public enum SmsSendResult | ||
{ | ||
Sending, | ||
Accepted, | ||
Failed, | ||
Failed_InvalidReceiver | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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
32 changes: 32 additions & 0 deletions
32
src/Altinn.Notifications.Sms.Integrations/LinkMobility/AltinnGatewayClient.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,32 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
using LinkMobility.PSWin.Client; | ||
using LinkMobility.PSWin.Client.Model; | ||
using LinkMobility.PSWin.Client.Transports; | ||
|
||
using LinkMobilityModel = LinkMobility.PSWin.Client.Model; | ||
|
||
namespace Altinn.Notifications.Sms.Integrations.LinkMobility; | ||
|
||
/// <summary> | ||
/// Wrapper class for the LinkMobility SMS Gateway client to support DI | ||
/// </summary> | ||
[ExcludeFromCodeCoverage] | ||
public class AltinnGatewayClient : IAltinnGatewayClient | ||
{ | ||
private readonly GatewayClient _client; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AltinnGatewayClient"/> class. | ||
/// </summary> | ||
public AltinnGatewayClient(SmsGatewayConfiguration gatewayConfig) | ||
{ | ||
_client = new(new XmlTransport(gatewayConfig.Username, gatewayConfig.Password, new Uri(gatewayConfig.Endpoint))); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task<MessageResult> SendAsync(LinkMobilityModel.Sms message) | ||
{ | ||
return await _client.SendAsync(message); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Altinn.Notifications.Sms.Integrations/LinkMobility/IAltinnGatewayClient.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,17 @@ | ||
using LinkMobility.PSWin.Client.Model; | ||
|
||
using LinkMobilityModel = LinkMobility.PSWin.Client.Model; | ||
|
||
namespace Altinn.Notifications.Sms.Integrations.LinkMobility | ||
{ | ||
/// <summary> | ||
/// Interface for the gateway client | ||
/// </summary> | ||
public interface IAltinnGatewayClient | ||
{ | ||
/// <summary> | ||
/// Send sms async | ||
/// </summary> | ||
public Task<MessageResult> SendAsync(LinkMobilityModel.Sms message); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/Altinn.Notifications.Sms.Integrations/LinkMobility/SmsClient.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,46 @@ | ||
using Altinn.Notifications.Sms.Core.Dependencies; | ||
using Altinn.Notifications.Sms.Core.Sending; | ||
using Altinn.Notifications.Sms.Core.Shared; | ||
using Altinn.Notifications.Sms.Core.Status; | ||
using LinkMobility.PSWin.Client.Model; | ||
|
||
using LinkMobilityModel = global::LinkMobility.PSWin.Client.Model; | ||
|
||
namespace Altinn.Notifications.Sms.Integrations.LinkMobility | ||
{ | ||
/// <summary> | ||
/// Represents an implementation of <see cref="ISmsClient"/> that will use LinkMobility's | ||
/// SMS gateway to send text messages. | ||
/// </summary> | ||
public class SmsClient : ISmsClient | ||
{ | ||
private readonly IAltinnGatewayClient _client; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SmsClient"/> class. | ||
/// </summary> | ||
/// <param name="gatewayConfig">The configuration for the sms gateway</param> | ||
public SmsClient(IAltinnGatewayClient client) | ||
{ | ||
_client = client; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<Result<string, SmsClientErrorResponse>> SendAsync(Core.Sending.Sms sms) | ||
{ | ||
MessageResult result = await _client.SendAsync(new LinkMobilityModel.Sms(sms.Recipient, sms.Message, sms.Sender)); | ||
|
||
if (result.IsStatusOk) | ||
{ | ||
return result.GatewayReference; | ||
} | ||
|
||
if (result.StatusText.StartsWith("Invalid RCV")) | ||
{ | ||
return new SmsClientErrorResponse { SendResult = SmsSendResult.Failed_InvalidReceiver, ErrorMessage = result.StatusText }; | ||
} | ||
|
||
return new SmsClientErrorResponse { SendResult = SmsSendResult.Failed }; | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Altinn.Notifications.Sms.Integrations/LinkMobility/SmsGatewayConfiguration.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,23 @@ | ||
namespace Altinn.Notifications.Sms.Integrations.LinkMobility | ||
{ | ||
/// <summary> | ||
/// Configuration for the LinkMobility SMS gateway | ||
/// </summary> | ||
public class SmsGatewayConfiguration | ||
{ | ||
/// <summary> | ||
/// Username to use for authentication towards the SMS gateway | ||
/// </summary> | ||
public string Username { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Password to use for authentication towards the SMS gateway | ||
/// </summary> | ||
public string Password { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Url to the SMS gateway endpoint | ||
/// </summary> | ||
public string Endpoint { get; set; } = string.Empty; | ||
} | ||
} |
25 changes: 0 additions & 25 deletions
25
src/Altinn.Notifications.Sms/Controllers/SetupController.cs
This file was deleted.
Oops, something went wrong.
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.