Skip to content

Commit 00116ee

Browse files
Merge pull request #35 from StuartFerguson/task/#10_smtptogoinjectedclient
Change SMTP proxy to use injected Http Client
2 parents 092ab01 + 4491716 commit 00116ee

1 file changed

Lines changed: 40 additions & 35 deletions

File tree

  • MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go

MessagingService.BusinessLogic/Services/EmailServices/Smtp2Go/Smtp2GoProxy.cs

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class Smtp2GoProxy : IEmailServiceProxy
3232
#region Constructors
3333

3434
/// <summary>
35-
/// Initializes a new instance of the <see cref="Smtp2GoProxy"/> class.
35+
/// Initializes a new instance of the <see cref="Smtp2GoProxy" /> class.
3636
/// </summary>
3737
/// <param name="httpClient">The HTTP client.</param>
3838
public Smtp2GoProxy(HttpClient httpClient)
@@ -83,27 +83,25 @@ public async Task<EmailServiceProxyResponse> SendEmail(Guid messageId,
8383

8484
StringContent content = new StringContent(requestSerialised, Encoding.UTF8, "application/json");
8585

86-
using(HttpClient client = new HttpClient())
87-
{
88-
client.BaseAddress = new Uri(ConfigurationReader.GetValue("SMTP2GoBaseAddress"));
89-
90-
HttpResponseMessage httpResponse = await client.PostAsync("email/send", content, cancellationToken);
86+
String requestUri = $"{ConfigurationReader.GetValue("SMTP2GoBaseAddress")}/email/send";
87+
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri);
88+
requestMessage.Content = content;
9189

92-
Smtp2GoSendEmailResponse apiResponse = JsonConvert.DeserializeObject<Smtp2GoSendEmailResponse>(await httpResponse.Content.ReadAsStringAsync());
90+
HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(requestMessage, cancellationToken);
9391

94-
Logger.LogDebug($"Response Message Received from Email Provider [SMTP2Go] {JsonConvert.SerializeObject(apiResponse)}");
92+
Smtp2GoSendEmailResponse apiResponse = JsonConvert.DeserializeObject<Smtp2GoSendEmailResponse>(await httpResponse.Content.ReadAsStringAsync());
9593

96-
// Translate the Response
97-
response = new EmailServiceProxyResponse
98-
{
99-
ApiStatusCode = httpResponse.StatusCode,
100-
EmailIdentifier = apiResponse.Data.EmailId,
101-
Error = apiResponse.Data.Error,
102-
ErrorCode = apiResponse.Data.ErrorCode,
103-
RequestIdentifier = apiResponse.RequestId
104-
};
105-
}
94+
Logger.LogDebug($"Response Message Received from Email Provider [SMTP2Go] {JsonConvert.SerializeObject(apiResponse)}");
10695

96+
// Translate the Response
97+
response = new EmailServiceProxyResponse
98+
{
99+
ApiStatusCode = httpResponse.StatusCode,
100+
EmailIdentifier = apiResponse.Data.EmailId,
101+
Error = apiResponse.Data.Error,
102+
ErrorCode = apiResponse.Data.ErrorCode,
103+
RequestIdentifier = apiResponse.RequestId
104+
};
107105
return response;
108106
}
109107

@@ -125,40 +123,47 @@ public async Task<MessageStatusResponse> GetMessageStatus(String providerReferen
125123
Smtp2GoEmailSearchRequest apiRequest = new Smtp2GoEmailSearchRequest
126124
{
127125
ApiKey = ConfigurationReader.GetValue("SMTP2GoAPIKey"),
128-
EmailId = new List<String>{providerReference},
126+
EmailId = new List<String>
127+
{
128+
providerReference
129+
},
129130
StartDate = startDate.ToString("yyyy-MM-dd"),
130131
EndDate = endDate.ToString("yyyy-MM-dd"),
131-
};
132+
};
132133

133134
String requestSerialised = JsonConvert.SerializeObject(apiRequest);
134135

135136
Logger.LogDebug($"Request Message Sent to Email Provider [SMTP2Go] {requestSerialised}");
136137

137138
StringContent content = new StringContent(requestSerialised, Encoding.UTF8, "application/json");
138139

139-
using (HttpClient client = new HttpClient())
140-
{
141-
client.BaseAddress = new Uri(ConfigurationReader.GetValue("SMTP2GoBaseAddress"));
140+
String requestUri = $"{ConfigurationReader.GetValue("SMTP2GoBaseAddress")}/email/search";
141+
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri);
142+
requestMessage.Content = content;
142143

143-
HttpResponseMessage httpResponse = await client.PostAsync("email/search", content, cancellationToken);
144+
HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(requestMessage, cancellationToken);
144145

145-
Smtp2GoEmailSearchResponse apiResponse = JsonConvert.DeserializeObject<Smtp2GoEmailSearchResponse>(await httpResponse.Content.ReadAsStringAsync());
146+
Smtp2GoEmailSearchResponse apiResponse = JsonConvert.DeserializeObject<Smtp2GoEmailSearchResponse>(await httpResponse.Content.ReadAsStringAsync());
146147

147-
Logger.LogDebug($"Response Message Received from Email Provider [SMTP2Go] {JsonConvert.SerializeObject(apiResponse)}");
148+
Logger.LogDebug($"Response Message Received from Email Provider [SMTP2Go] {JsonConvert.SerializeObject(apiResponse)}");
148149

149-
// Translate the Response
150-
response = new MessageStatusResponse
151-
{
152-
ApiStatusCode = httpResponse.StatusCode,
153-
MessageStatus = this.TranslateMessageStatus(apiResponse.Data.EmailDetails.Single().Status),
154-
ProviderStatusDescription = apiResponse.Data.EmailDetails.Single().Status,
155-
Timestamp = apiResponse.Data.EmailDetails.Single().EmailStatusDate
156-
};
157-
}
150+
// Translate the Response
151+
response = new MessageStatusResponse
152+
{
153+
ApiStatusCode = httpResponse.StatusCode,
154+
MessageStatus = this.TranslateMessageStatus(apiResponse.Data.EmailDetails.Single().Status),
155+
ProviderStatusDescription = apiResponse.Data.EmailDetails.Single().Status,
156+
Timestamp = apiResponse.Data.EmailDetails.Single().EmailStatusDate
157+
};
158158

159159
return response;
160160
}
161161

162+
/// <summary>
163+
/// Translates the message status.
164+
/// </summary>
165+
/// <param name="status">The status.</param>
166+
/// <returns></returns>
162167
private MessageStatus TranslateMessageStatus(String status)
163168
{
164169
MessageStatus result;

0 commit comments

Comments
 (0)