diff --git a/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoProxy.cs b/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoProxy.cs index 7559602..3048ef0 100644 --- a/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoProxy.cs +++ b/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoProxy.cs @@ -15,6 +15,11 @@ using System.Threading; using System.Threading.Tasks; + public record Smtp2GConfig { + public String BaseAddress { get; set; } + public String APIKey { get; set; } + } + /// /// /// @@ -29,13 +34,15 @@ public class Smtp2GoProxy : IEmailServiceProxy /// private readonly HttpClient HttpClient; + private readonly Smtp2GConfig Configuration; + #endregion #region Constructors - public Smtp2GoProxy(HttpClient httpClient) - { + public Smtp2GoProxy(HttpClient httpClient, Smtp2GConfig configuration) { this.HttpClient = httpClient; + this.Configuration = configuration; } #endregion @@ -52,7 +59,7 @@ public async Task SendEmail(Guid messageId, CancellationToken cancellationToken) { // Translate the request message Smtp2GoSendEmailRequest apiRequest = new() { - ApiKey = ConfigurationReader.GetValue("SMTP2GoAPIKey"), + ApiKey = Configuration.APIKey, HTMLBody = isHtml ? body : string.Empty, TextBody = isHtml ? string.Empty : body, Sender = fromAddress, @@ -72,7 +79,7 @@ public async Task SendEmail(Guid messageId, StringContent content = new(requestSerialised, Encoding.UTF8, "application/json"); - String requestUri = $"{ConfigurationReader.GetValue("SMTP2GoBaseAddress")}email/send"; + String requestUri = $"{Configuration.BaseAddress}email/send"; HttpRequestMessage requestMessage = new(HttpMethod.Post, requestUri); requestMessage.Content = content; @@ -109,7 +116,7 @@ public async Task GetMessageStatus(String providerReferen DateTime endDate, CancellationToken cancellationToken) { Smtp2GoEmailSearchRequest apiRequest = new() { - ApiKey = ConfigurationReader.GetValue("SMTP2GoAPIKey"), EmailId = new List { providerReference }, StartDate = startDate.ToString("yyyy-MM-dd"), EndDate = endDate.ToString("yyyy-MM-dd"), + ApiKey = Configuration.APIKey, EmailId = new List { providerReference }, StartDate = startDate.ToString("yyyy-MM-dd"), EndDate = endDate.ToString("yyyy-MM-dd"), }; String requestSerialised = JsonConvert.SerializeObject(apiRequest, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None }); @@ -118,13 +125,13 @@ public async Task GetMessageStatus(String providerReferen StringContent content = new(requestSerialised, Encoding.UTF8, "application/json"); - String requestUri = $"{ConfigurationReader.GetValue("SMTP2GoBaseAddress")}email/search"; + String requestUri = $"{Configuration.BaseAddress}email/search"; HttpRequestMessage requestMessage = new(HttpMethod.Post, requestUri); requestMessage.Content = content; HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(requestMessage, cancellationToken); - Smtp2GoEmailSearchResponse apiResponse = JsonConvert.DeserializeObject(await httpResponse.Content.ReadAsStringAsync()); + Smtp2GoEmailSearchResponse apiResponse = JsonConvert.DeserializeObject(await httpResponse.Content.ReadAsStringAsync(cancellationToken)); Logger.LogDebug($"Response Message Received from Email Provider [SMTP2Go] {JsonConvert.SerializeObject(apiResponse)}"); diff --git a/MessagingService.BusinessLogic/Services/SMSServices/TheSMSWorks/TheSmsWorksProxy.cs b/MessagingService.BusinessLogic/Services/SMSServices/TheSMSWorks/TheSmsWorksProxy.cs index f54b2f9..be2dbe8 100644 --- a/MessagingService.BusinessLogic/Services/SMSServices/TheSMSWorks/TheSmsWorksProxy.cs +++ b/MessagingService.BusinessLogic/Services/SMSServices/TheSMSWorks/TheSmsWorksProxy.cs @@ -16,20 +16,24 @@ namespace MessagingService.BusinessLogic.Services.SMSServices.TheSMSWorks using Shared.Extensions; using Shared.General; + public record SmsWorksConfig { + public String BaseAddress { get; set; } + public String CustomerId { get; set; } + public String Key { get; set; } + public String Secret { get; set; } + } + [ExcludeFromCodeCoverage] public class TheSmsWorksProxy : ISMSServiceProxy { private readonly HttpClient HttpClient; + private readonly SmsWorksConfig Configuration; - public TheSmsWorksProxy(HttpClient httpClient) { + public TheSmsWorksProxy(HttpClient httpClient, SmsWorksConfig configuration) { this.HttpClient = httpClient; + this.Configuration = configuration; } - - private const String TheSMSWorksBaseAddressKey = "TheSMSWorksBaseAddress"; - private const String TheSMSWorksCustomerIdKey = "TheSMSWorksCustomerId"; - private const String TheSMSWorksKeyKey = "TheSMSWorksKey"; - private const String TheSMSWorksSecretKey = "TheSMSWorksSecret"; - + /// /// Sends the SMS. /// @@ -47,17 +51,16 @@ public async Task SendSMS(Guid messageId, SMSServiceProxyResponse response = null; // Create the Auth Request - TheSmsWorksTokenRequest apiTokenRequest = new() { CustomerId = ConfigurationReader.GetValue(TheSMSWorksCustomerIdKey), - Key = ConfigurationReader.GetValue(TheSMSWorksKeyKey), Secret = ConfigurationReader.GetValue(TheSMSWorksSecretKey) }; + TheSmsWorksTokenRequest apiTokenRequest = new() { CustomerId = Configuration.CustomerId, Key = Configuration.Key, Secret = Configuration.Secret }; String apiTokenRequestSerialised = JsonConvert.SerializeObject(apiTokenRequest).ToLower(); StringContent content = new(apiTokenRequestSerialised, Encoding.UTF8, "application/json"); // First do the authentication - HttpResponseMessage apiTokenHttpResponse = await this.HttpClient.PostAsync($"{ConfigurationReader.GetValue(TheSMSWorksBaseAddressKey)}auth/token", content, cancellationToken); + HttpResponseMessage apiTokenHttpResponse = await this.HttpClient.PostAsync($"{Configuration.BaseAddress}auth/token", content, cancellationToken); if (apiTokenHttpResponse.IsSuccessStatusCode) { - TheSmsWorksTokenResponse apiTokenResponse = JsonConvert.DeserializeObject(await apiTokenHttpResponse.Content.ReadAsStringAsync()); + TheSmsWorksTokenResponse apiTokenResponse = JsonConvert.DeserializeObject(await apiTokenHttpResponse.Content.ReadAsStringAsync(cancellationToken)); // Now do the actual send TheSmsWorksSendSMSRequest apiSendSmsRequest = new() { @@ -73,11 +76,11 @@ public async Task SendSMS(Guid messageId, content = new StringContent(apiSendSMSMessageRequestSerialised, Encoding.UTF8, "application/json"); this.HttpClient.DefaultRequestHeaders.Add("Authorization", apiTokenResponse.Token); - HttpResponseMessage apiSendSMSMessageHttpResponse = await this.HttpClient.PostAsync($"{ConfigurationReader.GetValue(TheSMSWorksBaseAddressKey)}message/send", content, cancellationToken); + HttpResponseMessage apiSendSMSMessageHttpResponse = await this.HttpClient.PostAsync($"{Configuration.BaseAddress}message/send", content, cancellationToken); if (apiSendSMSMessageHttpResponse.IsSuccessStatusCode) { // Message has been sent - TheSmsWorksSendSMSResponse apiTheSmsWorksSendSmsResponse = JsonConvert.DeserializeObject(await apiSendSMSMessageHttpResponse.Content.ReadAsStringAsync()); + TheSmsWorksSendSMSResponse apiTheSmsWorksSendSmsResponse = JsonConvert.DeserializeObject(await apiSendSMSMessageHttpResponse.Content.ReadAsStringAsync(cancellationToken)); response = new SMSServiceProxyResponse { ApiCallSuccessful = true, SMSIdentifier = apiTheSmsWorksSendSmsResponse.MessageId, }; } @@ -97,26 +100,23 @@ public async Task GetMessageStatus(String providerReferen MessageStatusResponse response = new(); // Create the Auth Request - TheSmsWorksTokenRequest apiTokenRequest = new() { CustomerId = ConfigurationReader.GetValue(TheSMSWorksCustomerIdKey), - Key = ConfigurationReader.GetValue(TheSMSWorksKeyKey), - Secret = ConfigurationReader.GetValue(TheSMSWorksSecretKey) - }; + TheSmsWorksTokenRequest apiTokenRequest = new() { CustomerId = Configuration.CustomerId, Key = Configuration.Key, Secret = Configuration.Secret }; String apiTokenRequestSerialised = JsonConvert.SerializeObject(apiTokenRequest).ToLower(); StringContent content = new(apiTokenRequestSerialised, Encoding.UTF8, "application/json"); // First do the authentication - HttpResponseMessage apiTokenHttpResponse = await this.HttpClient.PostAsync($"{ConfigurationReader.GetValue(TheSMSWorksBaseAddressKey)}auth/token", content, cancellationToken); + HttpResponseMessage apiTokenHttpResponse = await this.HttpClient.PostAsync($"{Configuration.BaseAddress}auth/token", content, cancellationToken); if (apiTokenHttpResponse.IsSuccessStatusCode) { TheSmsWorksTokenResponse apiTokenResponse = JsonConvert.DeserializeObject(await apiTokenHttpResponse.Content.ReadAsStringAsync(cancellationToken)); this.HttpClient.DefaultRequestHeaders.Add("Authorization", apiTokenResponse.Token); - HttpResponseMessage apiGetSMSMessageHttpResponse = await this.HttpClient.GetAsync($"{ConfigurationReader.GetValue(TheSMSWorksBaseAddressKey)}messages/{providerReference}", cancellationToken); + HttpResponseMessage apiGetSMSMessageHttpResponse = await this.HttpClient.GetAsync($"{Configuration.BaseAddress}messages/{providerReference}", cancellationToken); if (apiGetSMSMessageHttpResponse.IsSuccessStatusCode) { // Message has been sent - TheSMSWorksGetMessageResponse apiSmsWorksGetMessageResponse = JsonConvert.DeserializeObject(await apiGetSMSMessageHttpResponse.Content.ReadAsStringAsync()); + TheSMSWorksGetMessageResponse apiSmsWorksGetMessageResponse = JsonConvert.DeserializeObject(await apiGetSMSMessageHttpResponse.Content.ReadAsStringAsync(cancellationToken)); response = new MessageStatusResponse { ApiStatusCode = apiGetSMSMessageHttpResponse.StatusCode, MessageStatus = this.TranslateMessageStatus(apiSmsWorksGetMessageResponse.Status), ProviderStatusDescription = apiSmsWorksGetMessageResponse.Status, Timestamp = DateTime.Parse(apiSmsWorksGetMessageResponse.Modified) }; } diff --git a/MessagingService/Bootstrapper/MessagingProxyRegistry.cs b/MessagingService/Bootstrapper/MessagingProxyRegistry.cs index ac431c2..493aa4c 100644 --- a/MessagingService/Bootstrapper/MessagingProxyRegistry.cs +++ b/MessagingService/Bootstrapper/MessagingProxyRegistry.cs @@ -5,7 +5,9 @@ using BusinessLogic.Services.SMSServices; using BusinessLogic.Services.SMSServices.TheSMSWorks; using ClientProxyBase; +using Google.Api; using Lamar; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Service.Services.Email.IntegrationTest; using Service.Services.SMSServices.IntegrationTest; @@ -27,32 +29,30 @@ public MessagingProxyRegistry() this.RegisterSMSProxy(); } - private void RegisterEmailProxy() - { + private void RegisterEmailProxy() { // read the config setting String emailProxy = ConfigurationReader.GetValue("AppSettings", "EmailProxy"); - if (emailProxy == "Smtp2Go") - { + if (emailProxy == "Smtp2Go") { + Smtp2GConfig config = Startup.Configuration.GetSection("AppSettings:Smtp2Go").Get() ?? throw new InvalidOperationException("Smtp2Go config missing"); + this.AddSingleton(config); this.RegisterHttpClient(); } - else - { + else { this.AddSingleton(); } } - private void RegisterSMSProxy() - { + private void RegisterSMSProxy() { // read the config setting - String emailProxy = ConfigurationReader.GetValue("AppSettings", "SMSProxy"); + String smsProxy = ConfigurationReader.GetValue("AppSettings", "SMSProxy"); - if (emailProxy == "TheSMSWorks") - { + if (smsProxy == "TheSMSWorks") { + SmsWorksConfig config = Startup.Configuration.GetSection("AppSettings:TheSmsWorks").Get() ?? throw new InvalidOperationException("SmsWorks config missing"); + this.AddSingleton(config); this.RegisterHttpClient(); } - else - { + else { this.AddSingleton(); } } diff --git a/MessagingService/appsettings.json b/MessagingService/appsettings.json index 274d8ec..e27b842 100644 --- a/MessagingService/appsettings.json +++ b/MessagingService/appsettings.json @@ -21,12 +21,16 @@ }, "EmailProxy": "Smtp2Go", "SMSProxy": "TheSMSWorks", - "SMTP2GoBaseAddress": "https://api.smtp2go.com/v3/", - "SMTP2GoAPIKey": "api-FC9777E0E53611E6A2F3F23C91BBF4A0", - "TheSMSWorksBaseAddress": "https://api.thesmsworks.co.uk/v1/", - "TheSMSWorksCustomerId": "5400-d666-5990-402f-aa48-a1fa9f45fd02", - "TheSMSWorksKey": "55f1d327-f759-44aa-b535-208bd2c934c8", - "TheSMSWorksSecret": "5e832a0b1cf3d9cd08e2808e3535e204bd7ec20ba2cb17a1930cd4986cf208ef", + "Smtp2Go": { + "BaseAddress": "https://api.smtp2go.com/v3/", + "APIKey": "api-FC9777E0E53611E6A2F3F23C91BBF4A0" + }, + "TheSmsWorks": { + "BaseAddress": "https://api.thesmsworks.co.uk/v1/", + "CustomerId": "5400-d666-5990-402f-aa48-a1fa9f45fd02", + "Key": "55f1d327-f759-44aa-b535-208bd2c934c8", + "Secret": "5e832a0b1cf3d9cd08e2808e3535e204bd7ec20ba2cb17a1930cd4986cf208ef" + }, "EventHandlerConfiguration": { "MessagingService.BusinessLogic.EventHandling.EmailDomainEventHandler, MessagingService.BusinessLogic": [ "ResponseReceivedFromEmailProviderEvent"