From e48f62c31e9c0fe31eb05fe8be6b2aa224dabf26 Mon Sep 17 00:00:00 2001 From: Saranya Date: Wed, 17 Mar 2021 18:37:09 -0400 Subject: [PATCH 1/4] Added timeout value --- .editorconfig | 4 +++ .../Examples/Basic/BasicSendWithProxy.cs | 6 +++- .../Examples/ExampleConfig.cs | 14 +++++--- Example Projects/dotNetCoreExample/Program.cs | 2 +- SocketLabs.sln | 9 +++-- src/SocketLabs/InjectionApi/SendResponse.cs | 2 ++ .../InjectionApi/SocketLabsClient.cs | 33 +++++++++++++------ .../SocketLabs.UnitTests.csproj | 5 ++- 8 files changed, 55 insertions(+), 20 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..d4eab18 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,4 @@ +[*.cs] + +# Default severity for analyzer diagnostics with category 'Style' +dotnet_analyzer_diagnostic.category-Style.severity = suggestion diff --git a/Example Projects/dotNetCoreExample/Examples/Basic/BasicSendWithProxy.cs b/Example Projects/dotNetCoreExample/Examples/Basic/BasicSendWithProxy.cs index f297ce0..d61d490 100644 --- a/Example Projects/dotNetCoreExample/Examples/Basic/BasicSendWithProxy.cs +++ b/Example Projects/dotNetCoreExample/Examples/Basic/BasicSendWithProxy.cs @@ -8,7 +8,9 @@ public class BasicSendWithProxy : IExample { public SendResponse RunExample() { - var proxy = new WebProxy("http://localhost.:8888", false); + // var proxy = new WebProxy("http://localhost.:8888", false); + + var proxy = new WebProxy("http://localhost:4433", false); var client = new SocketLabsClient(ExampleConfig.ServerId, ExampleConfig.ApiKey, proxy) { @@ -25,6 +27,8 @@ public SendResponse RunExample() message.ReplyTo.Email = "replyto@example.com"; message.To.Add("recipient1@example.com"); + client.RequestTimeout = 50; + return client.Send(message); } } diff --git a/Example Projects/dotNetCoreExample/Examples/ExampleConfig.cs b/Example Projects/dotNetCoreExample/Examples/ExampleConfig.cs index d58c4d3..c92256d 100644 --- a/Example Projects/dotNetCoreExample/Examples/ExampleConfig.cs +++ b/Example Projects/dotNetCoreExample/Examples/ExampleConfig.cs @@ -1,13 +1,17 @@ -namespace dotNetCoreExample.Examples +using System; +namespace dotNetCoreExample.Examples { public static class ExampleConfig { //public static int ServerId => Environment.GetEnvironmentVariable("SocketlabsServerId", EnvironmentVariableTarget.User); //public static string ApiKey => Environment.GetEnvironmentVariable("SocketlabsApiPassword", EnvironmentVariableTarget.User); public static string TargetApi = "https://inject.socketlabs.com/api/v1/email"; - - public static int ServerId => 0; //your serverId - public static string ApiKey => "your api key"; + + + public static int ServerId => int.Parse(Environment.GetEnvironmentVariable("SocketlabsServerId", EnvironmentVariableTarget.User)); + public static string ApiKey => Environment.GetEnvironmentVariable("SocketlabsApiPassword", EnvironmentVariableTarget.User); + + } -} +} \ No newline at end of file diff --git a/Example Projects/dotNetCoreExample/Program.cs b/Example Projects/dotNetCoreExample/Program.cs index 3365842..10b3c87 100644 --- a/Example Projects/dotNetCoreExample/Program.cs +++ b/Example Projects/dotNetCoreExample/Program.cs @@ -26,7 +26,7 @@ public static void Main() quit = selection.ToLower().Trim() == "quit"; if (quit) continue; - + var exampleClassName = GetExampleName(selection); if(exampleClassName == null) continue; diff --git a/SocketLabs.sln b/SocketLabs.sln index a12b4f9..df0a8bb 100644 --- a/SocketLabs.sln +++ b/SocketLabs.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.27703.2047 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30907.101 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SocketLabs.UnitTests", "test\SocketLabs.Test\SocketLabs.UnitTests.csproj", "{C6E1FF08-07E8-47A9-B04B-F2F91468F045}" EndProject @@ -11,6 +11,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotNetCoreExample", "Exampl EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SocketLabs", "src\SocketLabs\SocketLabs.csproj", "{5FE88690-970A-4B7C-A8E9-BD8FF42FA01B}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{58D953F4-41BE-42B0-B3E6-9A6978A74630}" + ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/src/SocketLabs/InjectionApi/SendResponse.cs b/src/SocketLabs/InjectionApi/SendResponse.cs index 3237bae..48cba75 100644 --- a/src/SocketLabs/InjectionApi/SendResponse.cs +++ b/src/SocketLabs/InjectionApi/SendResponse.cs @@ -37,6 +37,8 @@ public string ResponseMessage case SendResult.Timeout: return "A timeout occurred sending the message"; + + case SendResult.Success: return "Successful send of message"; diff --git a/src/SocketLabs/InjectionApi/SocketLabsClient.cs b/src/SocketLabs/InjectionApi/SocketLabsClient.cs index 58196fb..20f5564 100644 --- a/src/SocketLabs/InjectionApi/SocketLabsClient.cs +++ b/src/SocketLabs/InjectionApi/SocketLabsClient.cs @@ -29,19 +29,28 @@ namespace SocketLabs.InjectionApi /// } /// /// - public class SocketLabsClient : ISocketLabsClient , IDisposable + public class SocketLabsClient : ISocketLabsClient, IDisposable { + + private string UserAgent { get; } = $"SocketLabs-csharp/{typeof(SocketLabsClient).GetTypeInfo().Assembly.GetName().Version}"; private readonly int _serverId; private readonly string _apiKey; private readonly HttpClient _httpClient; - + /// /// The SocketLabs Injection API endpoint Url /// public string EndpointUrl { get; set; } = "https://inject.socketlabs.com/api/v1/email"; + /// + /// A timeout occurred sending the message + /// + + public int RequestTimeout { get; set; } = 120; + + /// /// Creates a new instance of the SocketLabsClient. /// @@ -54,18 +63,21 @@ public SocketLabsClient(int serverId, string apiKey) _httpClient = BuildHttpClient(null); } + /// /// Creates a new instance of the SocketLabsClient with a proxy. /// /// Your SocketLabs ServerId number. /// Your SocketLabs Injection API key. /// The WebProxy you would like to use. + /* /// The RequestTimeout you would like to specify. */ public SocketLabsClient(int serverId, string apiKey, IWebProxy optionalProxy) { _serverId = serverId; _apiKey = apiKey; _httpClient = BuildHttpClient(optionalProxy); - } + + } /// /// Creates a new instance of the SocketLabsClient with a provided HttpClient. @@ -84,7 +96,8 @@ public SocketLabsClient(int serverId, string apiKey, HttpClient httpClient) private HttpClient BuildHttpClient(IWebProxy optionalProxy) { - var httpClient = optionalProxy != null ? new HttpClient(new HttpClientHandler() {UseProxy = true, Proxy = optionalProxy}) : new HttpClient(); + var httpClient = optionalProxy != null ? new HttpClient(new HttpClientHandler() { UseProxy = true, Proxy = optionalProxy}) : new HttpClient(); + ConfigureHttpClient(httpClient); return httpClient; } @@ -217,19 +230,20 @@ public async Task SendAsync(IBasicMessage message) var validationResult = validator.ValidateCredentials(_serverId, _apiKey); if (validationResult.Result != SendResult.Success) return validationResult; + validationResult = validator.ValidateMessage(message); if(validationResult.Result != SendResult.Success) return validationResult; var factory = new InjectionRequestFactory(_serverId, _apiKey); var injectionRequest = factory.GenerateRequest(message); var json = injectionRequest.GetAsJson(); - - var httpResponse = await _httpClient.PostAsync(EndpointUrl, json); - + _httpClient.Timeout = TimeSpan.FromSeconds(RequestTimeout); + var httpResponse = await _httpClient.PostAsync(EndpointUrl,json); var response = new InjectionResponseParser().Parse(httpResponse); + return response; } - + /// /// Asynchronously sends a bulk email message and returns the response from the Injection API. /// @@ -273,9 +287,8 @@ public async Task SendAsync(IBulkMessage message) var factory = new InjectionRequestFactory(_serverId, _apiKey); var injectionRequest = factory.GenerateRequest(message); - + _httpClient.Timeout = TimeSpan.FromSeconds(RequestTimeout); var httpResponse = await _httpClient.PostAsync(EndpointUrl, injectionRequest.GetAsJson()); - var response = new InjectionResponseParser().Parse(httpResponse); return response; } diff --git a/test/SocketLabs.Test/SocketLabs.UnitTests.csproj b/test/SocketLabs.Test/SocketLabs.UnitTests.csproj index ef0cbdb..81ee08c 100644 --- a/test/SocketLabs.Test/SocketLabs.UnitTests.csproj +++ b/test/SocketLabs.Test/SocketLabs.UnitTests.csproj @@ -40,7 +40,7 @@ true false - + ..\..\packages\Castle.Core.4.3.1\lib\net45\Castle.Core.dll @@ -70,6 +70,9 @@ + + .editorconfig + From e0b39b1ecb8b58366a55b432792bee5f21b15799 Mon Sep 17 00:00:00 2001 From: Saranya Date: Wed, 17 Mar 2021 18:52:35 -0400 Subject: [PATCH 2/4] Added timeout value --- .../Examples/Basic/BasicSendWithProxy.cs | 3 +-- .../dotNetCoreExample/Examples/ExampleConfig.cs | 8 +++----- src/SocketLabs/InjectionApi/SocketLabsClient.cs | 2 +- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Example Projects/dotNetCoreExample/Examples/Basic/BasicSendWithProxy.cs b/Example Projects/dotNetCoreExample/Examples/Basic/BasicSendWithProxy.cs index d61d490..b1e390a 100644 --- a/Example Projects/dotNetCoreExample/Examples/Basic/BasicSendWithProxy.cs +++ b/Example Projects/dotNetCoreExample/Examples/Basic/BasicSendWithProxy.cs @@ -8,7 +8,7 @@ public class BasicSendWithProxy : IExample { public SendResponse RunExample() { - // var proxy = new WebProxy("http://localhost.:8888", false); + // var proxy = new WebProxy("http://localhost.:8888", false); var proxy = new WebProxy("http://localhost:4433", false); @@ -28,7 +28,6 @@ public SendResponse RunExample() message.To.Add("recipient1@example.com"); client.RequestTimeout = 50; - return client.Send(message); } } diff --git a/Example Projects/dotNetCoreExample/Examples/ExampleConfig.cs b/Example Projects/dotNetCoreExample/Examples/ExampleConfig.cs index c92256d..66c403c 100644 --- a/Example Projects/dotNetCoreExample/Examples/ExampleConfig.cs +++ b/Example Projects/dotNetCoreExample/Examples/ExampleConfig.cs @@ -5,13 +5,11 @@ public static class ExampleConfig { //public static int ServerId => Environment.GetEnvironmentVariable("SocketlabsServerId", EnvironmentVariableTarget.User); //public static string ApiKey => Environment.GetEnvironmentVariable("SocketlabsApiPassword", EnvironmentVariableTarget.User); - public static string TargetApi = "https://inject.socketlabs.com/api/v1/email"; + public static string TargetApi = "https://inject.socketlabs.com/api/v1/email"; - - public static int ServerId => int.Parse(Environment.GetEnvironmentVariable("SocketlabsServerId", EnvironmentVariableTarget.User)); public static string ApiKey => Environment.GetEnvironmentVariable("SocketlabsApiPassword", EnvironmentVariableTarget.User); - - + + } } \ No newline at end of file diff --git a/src/SocketLabs/InjectionApi/SocketLabsClient.cs b/src/SocketLabs/InjectionApi/SocketLabsClient.cs index 20f5564..692acf9 100644 --- a/src/SocketLabs/InjectionApi/SocketLabsClient.cs +++ b/src/SocketLabs/InjectionApi/SocketLabsClient.cs @@ -47,10 +47,10 @@ public class SocketLabsClient : ISocketLabsClient, IDisposable /// /// A timeout occurred sending the message /// - public int RequestTimeout { get; set; } = 120; + /// /// Creates a new instance of the SocketLabsClient. /// From 1b5b5c8a898ada6a1c0d69a48cb9852dbb8932f8 Mon Sep 17 00:00:00 2001 From: Saranya Date: Wed, 17 Mar 2021 19:03:05 -0400 Subject: [PATCH 3/4] Removed commented line --- src/SocketLabs/InjectionApi/SocketLabsClient.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/SocketLabs/InjectionApi/SocketLabsClient.cs b/src/SocketLabs/InjectionApi/SocketLabsClient.cs index 692acf9..19b963e 100644 --- a/src/SocketLabs/InjectionApi/SocketLabsClient.cs +++ b/src/SocketLabs/InjectionApi/SocketLabsClient.cs @@ -70,8 +70,7 @@ public SocketLabsClient(int serverId, string apiKey) /// Your SocketLabs ServerId number. /// Your SocketLabs Injection API key. /// The WebProxy you would like to use. - /* /// The RequestTimeout you would like to specify. */ - public SocketLabsClient(int serverId, string apiKey, IWebProxy optionalProxy) + public SocketLabsClient(int serverId, string apiKey, IWebProxy optionalProxy) { _serverId = serverId; _apiKey = apiKey; From af60d5a922d4f8780655c05ab7e18e82729cd509 Mon Sep 17 00:00:00 2001 From: "david.schrenker" Date: Thu, 18 Mar 2021 09:23:26 -0400 Subject: [PATCH 4/4] code cleanup --- .../Examples/ExampleConfig.cs | 8 ++----- src/SocketLabs/InjectionApi/SendResponse.cs | 4 +--- .../InjectionApi/SocketLabsClient.cs | 22 ++++++++----------- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/Example Projects/dotNetCoreExample/Examples/ExampleConfig.cs b/Example Projects/dotNetCoreExample/Examples/ExampleConfig.cs index 66c403c..d5afb8c 100644 --- a/Example Projects/dotNetCoreExample/Examples/ExampleConfig.cs +++ b/Example Projects/dotNetCoreExample/Examples/ExampleConfig.cs @@ -3,13 +3,9 @@ namespace dotNetCoreExample.Examples { public static class ExampleConfig { - //public static int ServerId => Environment.GetEnvironmentVariable("SocketlabsServerId", EnvironmentVariableTarget.User); - //public static string ApiKey => Environment.GetEnvironmentVariable("SocketlabsApiPassword", EnvironmentVariableTarget.User); - public static string TargetApi = "https://inject.socketlabs.com/api/v1/email"; - public static int ServerId => int.Parse(Environment.GetEnvironmentVariable("SocketlabsServerId", EnvironmentVariableTarget.User)); public static string ApiKey => Environment.GetEnvironmentVariable("SocketlabsApiPassword", EnvironmentVariableTarget.User); - - + public static string TargetApi = "https://inject.socketlabs.com/api/v1/email"; + } } \ No newline at end of file diff --git a/src/SocketLabs/InjectionApi/SendResponse.cs b/src/SocketLabs/InjectionApi/SendResponse.cs index 48cba75..b9712dc 100644 --- a/src/SocketLabs/InjectionApi/SendResponse.cs +++ b/src/SocketLabs/InjectionApi/SendResponse.cs @@ -36,9 +36,7 @@ public string ResponseMessage case SendResult.Timeout: return "A timeout occurred sending the message"; - - - + case SendResult.Success: return "Successful send of message"; diff --git a/src/SocketLabs/InjectionApi/SocketLabsClient.cs b/src/SocketLabs/InjectionApi/SocketLabsClient.cs index 19b963e..2d34395 100644 --- a/src/SocketLabs/InjectionApi/SocketLabsClient.cs +++ b/src/SocketLabs/InjectionApi/SocketLabsClient.cs @@ -31,8 +31,6 @@ namespace SocketLabs.InjectionApi /// public class SocketLabsClient : ISocketLabsClient, IDisposable { - - private string UserAgent { get; } = $"SocketLabs-csharp/{typeof(SocketLabsClient).GetTypeInfo().Assembly.GetName().Version}"; private readonly int _serverId; @@ -45,12 +43,10 @@ public class SocketLabsClient : ISocketLabsClient, IDisposable public string EndpointUrl { get; set; } = "https://inject.socketlabs.com/api/v1/email"; /// - /// A timeout occurred sending the message + /// A timeout period for the Injection API request (in Seconds). Default: 120s /// public int RequestTimeout { get; set; } = 120; - - - + /// /// Creates a new instance of the SocketLabsClient. /// @@ -62,8 +58,7 @@ public SocketLabsClient(int serverId, string apiKey) _apiKey = apiKey; _httpClient = BuildHttpClient(null); } - - + /// /// Creates a new instance of the SocketLabsClient with a proxy. /// @@ -76,7 +71,7 @@ public SocketLabsClient(int serverId, string apiKey, IWebProxy optionalProxy) _apiKey = apiKey; _httpClient = BuildHttpClient(optionalProxy); - } + } /// /// Creates a new instance of the SocketLabsClient with a provided HttpClient. @@ -96,7 +91,6 @@ public SocketLabsClient(int serverId, string apiKey, HttpClient httpClient) private HttpClient BuildHttpClient(IWebProxy optionalProxy) { var httpClient = optionalProxy != null ? new HttpClient(new HttpClientHandler() { UseProxy = true, Proxy = optionalProxy}) : new HttpClient(); - ConfigureHttpClient(httpClient); return httpClient; } @@ -228,18 +222,18 @@ public async Task SendAsync(IBasicMessage message) var validationResult = validator.ValidateCredentials(_serverId, _apiKey); if (validationResult.Result != SendResult.Success) return validationResult; - - + validationResult = validator.ValidateMessage(message); if(validationResult.Result != SendResult.Success) return validationResult; var factory = new InjectionRequestFactory(_serverId, _apiKey); var injectionRequest = factory.GenerateRequest(message); var json = injectionRequest.GetAsJson(); + _httpClient.Timeout = TimeSpan.FromSeconds(RequestTimeout); var httpResponse = await _httpClient.PostAsync(EndpointUrl,json); - var response = new InjectionResponseParser().Parse(httpResponse); + var response = new InjectionResponseParser().Parse(httpResponse); return response; } @@ -286,8 +280,10 @@ public async Task SendAsync(IBulkMessage message) var factory = new InjectionRequestFactory(_serverId, _apiKey); var injectionRequest = factory.GenerateRequest(message); + _httpClient.Timeout = TimeSpan.FromSeconds(RequestTimeout); var httpResponse = await _httpClient.PostAsync(EndpointUrl, injectionRequest.GetAsJson()); + var response = new InjectionResponseParser().Parse(httpResponse); return response; }