From 8212aa3a05255ffe5ebdd81c5e659649698785e0 Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Tue, 14 Oct 2025 12:50:56 +0100 Subject: [PATCH] method refactoring --- .../EmailServices/Smtp2Go/Smtp2GoProxy.cs | 103 ++++++------------ 1 file changed, 34 insertions(+), 69 deletions(-) diff --git a/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoProxy.cs b/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoProxy.cs index e5d95c0..7559602 100644 --- a/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoProxy.cs +++ b/MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoProxy.cs @@ -51,34 +51,22 @@ public async Task SendEmail(Guid messageId, List attachments, CancellationToken cancellationToken) { // Translate the request message - Smtp2GoSendEmailRequest apiRequest = new() - { - ApiKey = ConfigurationReader.GetValue("SMTP2GoAPIKey"), - HTMLBody = isHtml ? body : string.Empty, - TextBody = isHtml ? string.Empty : body, - Sender = fromAddress, - Subject = subject, - TestMode = false, - To = toAddresses.ToArray() - }; - if (attachments != null && attachments.Any()) - { + Smtp2GoSendEmailRequest apiRequest = new() { + ApiKey = ConfigurationReader.GetValue("SMTP2GoAPIKey"), + HTMLBody = isHtml ? body : string.Empty, + TextBody = isHtml ? string.Empty : body, + Sender = fromAddress, + Subject = subject, + TestMode = false, + To = toAddresses.ToArray() + }; + + if (attachments != null && attachments.Any()) { apiRequest.Attachments = new List(); - foreach (EmailAttachment emailAttachment in attachments) - { - apiRequest.Attachments.Add(new Smtp2GoAttachment - { - FileBlob = emailAttachment.FileData, - FileName = emailAttachment.Filename, - MimeType = this.ConvertFileType(emailAttachment.FileType) - }); - } + attachments.ForEach(a => apiRequest.Attachments.Add(new Smtp2GoAttachment { FileBlob = a.FileData, FileName = a.Filename, MimeType = this.ConvertFileType(a.FileType) })); } - String requestSerialised = JsonConvert.SerializeObject(apiRequest, Formatting.Indented, new JsonSerializerSettings - { - TypeNameHandling = TypeNameHandling.None - }); + String requestSerialised = JsonConvert.SerializeObject(apiRequest, Formatting.Indented, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None }); Logger.LogDebug($"Request Message Sent to Email Provider [SMTP2Go] {requestSerialised}"); @@ -90,31 +78,25 @@ public async Task SendEmail(Guid messageId, HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(requestMessage, cancellationToken); - Smtp2GoSendEmailResponse apiResponse = new() { Data = new Smtp2GoSendEmailResponseData() }; - if (httpResponse.IsSuccessStatusCode){ - apiResponse = JsonConvert.DeserializeObject(await httpResponse.Content.ReadAsStringAsync(cancellationToken)); - } - else { - apiResponse = new Smtp2GoSendEmailResponse { Data = new Smtp2GoSendEmailResponseData { Error = httpResponse.StatusCode.ToString(), ErrorCode = ((Int32)httpResponse.StatusCode).ToString() } }; - } + Smtp2GoSendEmailResponse apiResponse = httpResponse.IsSuccessStatusCode switch { + true => JsonConvert.DeserializeObject(await httpResponse.Content.ReadAsStringAsync(cancellationToken)), + _ => new Smtp2GoSendEmailResponse { Data = new Smtp2GoSendEmailResponseData { Error = httpResponse.StatusCode.ToString(), ErrorCode = ((Int32)httpResponse.StatusCode).ToString() } } + }; Logger.LogDebug($"Response Message Received from Email Provider [SMTP2Go] {JsonConvert.SerializeObject(apiResponse)}"); // Translate the Response - return new EmailServiceProxyResponse - { - ApiCallSuccessful = httpResponse.IsSuccessStatusCode && String.IsNullOrEmpty(apiResponse.Data.ErrorCode), - EmailIdentifier = apiResponse.Data.EmailId, - Error = apiResponse.Data.Error, - ErrorCode = apiResponse.Data.ErrorCode, - RequestIdentifier = apiResponse.RequestId - }; + return new EmailServiceProxyResponse { + ApiCallSuccessful = httpResponse.IsSuccessStatusCode && String.IsNullOrEmpty(apiResponse.Data.ErrorCode), + EmailIdentifier = apiResponse.Data.EmailId, + Error = apiResponse.Data.Error, + ErrorCode = apiResponse.Data.ErrorCode, + RequestIdentifier = apiResponse.RequestId + }; } - - private String ConvertFileType(FileType emailAttachmentFileType) - { - switch(emailAttachmentFileType) - { + + private String ConvertFileType(FileType emailAttachmentFileType) { + switch (emailAttachmentFileType) { case FileType.PDF: return "application/pdf"; default: @@ -125,23 +107,12 @@ private String ConvertFileType(FileType emailAttachmentFileType) public async Task GetMessageStatus(String providerReference, DateTime startDate, 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"), - }; - - String requestSerialised = JsonConvert.SerializeObject(apiRequest, new JsonSerializerSettings - { - TypeNameHandling = TypeNameHandling.None - }); + 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"), + }; + + String requestSerialised = JsonConvert.SerializeObject(apiRequest, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None }); Logger.LogDebug($"Request Message Sent to Email Provider [SMTP2Go] {requestSerialised}"); @@ -158,13 +129,7 @@ public async Task GetMessageStatus(String providerReferen Logger.LogDebug($"Response Message Received from Email Provider [SMTP2Go] {JsonConvert.SerializeObject(apiResponse)}"); // Translate the Response - return new MessageStatusResponse - { - ApiStatusCode = httpResponse.StatusCode, - MessageStatus = this.TranslateMessageStatus(apiResponse.Data.EmailDetails.Single().Status), - ProviderStatusDescription = apiResponse.Data.EmailDetails.Single().Status, - Timestamp = apiResponse.Data.EmailDetails.Single().EmailStatusDate - }; + return new MessageStatusResponse { ApiStatusCode = httpResponse.StatusCode, MessageStatus = this.TranslateMessageStatus(apiResponse.Data.EmailDetails.Single().Status), ProviderStatusDescription = apiResponse.Data.EmailDetails.Single().Status, Timestamp = apiResponse.Data.EmailDetails.Single().EmailStatusDate }; } private MessageStatus TranslateMessageStatus(String status) {