Skip to content

Commit e48f62c

Browse files
Added timeout value
1 parent 0be2f2b commit e48f62c

File tree

8 files changed

+55
-20
lines changed

8 files changed

+55
-20
lines changed

.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[*.cs]
2+
3+
# Default severity for analyzer diagnostics with category 'Style'
4+
dotnet_analyzer_diagnostic.category-Style.severity = suggestion

Example Projects/dotNetCoreExample/Examples/Basic/BasicSendWithProxy.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ public class BasicSendWithProxy : IExample
88
{
99
public SendResponse RunExample()
1010
{
11-
var proxy = new WebProxy("http://localhost.:8888", false);
11+
// var proxy = new WebProxy("http://localhost.:8888", false);
12+
13+
var proxy = new WebProxy("http://localhost:4433", false);
1214

1315
var client = new SocketLabsClient(ExampleConfig.ServerId, ExampleConfig.ApiKey, proxy)
1416
{
@@ -25,6 +27,8 @@ public SendResponse RunExample()
2527
message.ReplyTo.Email = "replyto@example.com";
2628
message.To.Add("recipient1@example.com");
2729

30+
client.RequestTimeout = 50;
31+
2832
return client.Send(message);
2933
}
3034
}
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
namespace dotNetCoreExample.Examples
1+
using System;
2+
namespace dotNetCoreExample.Examples
23
{
34
public static class ExampleConfig
45
{
56
//public static int ServerId => Environment.GetEnvironmentVariable("SocketlabsServerId", EnvironmentVariableTarget.User);
67
//public static string ApiKey => Environment.GetEnvironmentVariable("SocketlabsApiPassword", EnvironmentVariableTarget.User);
78
public static string TargetApi = "https://inject.socketlabs.com/api/v1/email";
8-
9-
public static int ServerId => 0; //your serverId
10-
public static string ApiKey => "your api key";
119

10+
11+
12+
public static int ServerId => int.Parse(Environment.GetEnvironmentVariable("SocketlabsServerId", EnvironmentVariableTarget.User));
13+
public static string ApiKey => Environment.GetEnvironmentVariable("SocketlabsApiPassword", EnvironmentVariableTarget.User);
14+
15+
1216
}
13-
}
17+
}

Example Projects/dotNetCoreExample/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static void Main()
2626
quit = selection.ToLower().Trim() == "quit";
2727
if (quit)
2828
continue;
29-
29+
3030
var exampleClassName = GetExampleName(selection);
3131
if(exampleClassName == null)
3232
continue;

SocketLabs.sln

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.27703.2047
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30907.101
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SocketLabs.UnitTests", "test\SocketLabs.Test\SocketLabs.UnitTests.csproj", "{C6E1FF08-07E8-47A9-B04B-F2F91468F045}"
77
EndProject
@@ -11,6 +11,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotNetCoreExample", "Exampl
1111
EndProject
1212
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SocketLabs", "src\SocketLabs\SocketLabs.csproj", "{5FE88690-970A-4B7C-A8E9-BD8FF42FA01B}"
1313
EndProject
14+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{58D953F4-41BE-42B0-B3E6-9A6978A74630}"
15+
ProjectSection(SolutionItems) = preProject
16+
.editorconfig = .editorconfig
17+
EndProjectSection
18+
EndProject
1419
Global
1520
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1621
Debug|Any CPU = Debug|Any CPU

src/SocketLabs/InjectionApi/SendResponse.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public string ResponseMessage
3737
case SendResult.Timeout:
3838
return "A timeout occurred sending the message";
3939

40+
41+
4042
case SendResult.Success:
4143
return "Successful send of message";
4244

src/SocketLabs/InjectionApi/SocketLabsClient.cs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,28 @@ namespace SocketLabs.InjectionApi
2929
/// }
3030
///</code>
3131
/// </example>
32-
public class SocketLabsClient : ISocketLabsClient , IDisposable
32+
public class SocketLabsClient : ISocketLabsClient, IDisposable
3333
{
34+
35+
3436
private string UserAgent { get; } = $"SocketLabs-csharp/{typeof(SocketLabsClient).GetTypeInfo().Assembly.GetName().Version}";
3537

3638
private readonly int _serverId;
3739
private readonly string _apiKey;
3840
private readonly HttpClient _httpClient;
39-
41+
4042
/// <summary>
4143
/// The SocketLabs Injection API endpoint Url
4244
/// </summary>
4345
public string EndpointUrl { get; set; } = "https://inject.socketlabs.com/api/v1/email";
4446

47+
/// <summary>
48+
/// A timeout occurred sending the message
49+
/// </summary>
50+
51+
public int RequestTimeout { get; set; } = 120;
52+
53+
4554
/// <summary>
4655
/// Creates a new instance of the <c>SocketLabsClient</c>.
4756
/// </summary>
@@ -54,18 +63,21 @@ public SocketLabsClient(int serverId, string apiKey)
5463
_httpClient = BuildHttpClient(null);
5564
}
5665

66+
5767
/// <summary>
5868
/// Creates a new instance of the <c>SocketLabsClient</c> with a proxy.
5969
/// </summary>
6070
/// <param name="serverId">Your SocketLabs ServerId number.</param>
6171
/// <param name="apiKey">Your SocketLabs Injection API key.</param>
6272
/// <param name="optionalProxy">The WebProxy you would like to use.</param>
73+
/* /// <param name="RequestTimeout">The RequestTimeout you would like to specify.</param> */
6374
public SocketLabsClient(int serverId, string apiKey, IWebProxy optionalProxy)
6475
{
6576
_serverId = serverId;
6677
_apiKey = apiKey;
6778
_httpClient = BuildHttpClient(optionalProxy);
68-
}
79+
80+
}
6981

7082
/// <summary>
7183
/// Creates a new instance of the <c>SocketLabsClient</c> with a provided HttpClient.
@@ -84,7 +96,8 @@ public SocketLabsClient(int serverId, string apiKey, HttpClient httpClient)
8496

8597
private HttpClient BuildHttpClient(IWebProxy optionalProxy)
8698
{
87-
var httpClient = optionalProxy != null ? new HttpClient(new HttpClientHandler() {UseProxy = true, Proxy = optionalProxy}) : new HttpClient();
99+
var httpClient = optionalProxy != null ? new HttpClient(new HttpClientHandler() { UseProxy = true, Proxy = optionalProxy}) : new HttpClient();
100+
88101
ConfigureHttpClient(httpClient);
89102
return httpClient;
90103
}
@@ -217,19 +230,20 @@ public async Task<SendResponse> SendAsync(IBasicMessage message)
217230
var validationResult = validator.ValidateCredentials(_serverId, _apiKey);
218231
if (validationResult.Result != SendResult.Success) return validationResult;
219232

233+
220234
validationResult = validator.ValidateMessage(message);
221235
if(validationResult.Result != SendResult.Success) return validationResult;
222236

223237
var factory = new InjectionRequestFactory(_serverId, _apiKey);
224238
var injectionRequest = factory.GenerateRequest(message);
225239
var json = injectionRequest.GetAsJson();
226-
227-
var httpResponse = await _httpClient.PostAsync(EndpointUrl, json);
228-
240+
_httpClient.Timeout = TimeSpan.FromSeconds(RequestTimeout);
241+
var httpResponse = await _httpClient.PostAsync(EndpointUrl,json);
229242
var response = new InjectionResponseParser().Parse(httpResponse);
243+
230244
return response;
231245
}
232-
246+
233247
/// <summary>
234248
/// Asynchronously sends a bulk email message and returns the response from the Injection API.
235249
/// </summary>
@@ -273,9 +287,8 @@ public async Task<SendResponse> SendAsync(IBulkMessage message)
273287

274288
var factory = new InjectionRequestFactory(_serverId, _apiKey);
275289
var injectionRequest = factory.GenerateRequest(message);
276-
290+
_httpClient.Timeout = TimeSpan.FromSeconds(RequestTimeout);
277291
var httpResponse = await _httpClient.PostAsync(EndpointUrl, injectionRequest.GetAsJson());
278-
279292
var response = new InjectionResponseParser().Parse(httpResponse);
280293
return response;
281294
}

test/SocketLabs.Test/SocketLabs.UnitTests.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<PropertyGroup Condition="'$(TeamCitySignAssembly)' != ''">
4141
<SignAssembly>true</SignAssembly>
4242
<DelaySign>false</DelaySign>
43-
</PropertyGroup>
43+
</PropertyGroup>
4444
<ItemGroup>
4545
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
4646
<HintPath>..\..\packages\Castle.Core.4.3.1\lib\net45\Castle.Core.dll</HintPath>
@@ -70,6 +70,9 @@
7070
<Compile Include="Validation\SendValidatorTest.cs" />
7171
</ItemGroup>
7272
<ItemGroup>
73+
<None Include="..\..\.editorconfig">
74+
<Link>.editorconfig</Link>
75+
</None>
7376
<None Include="packages.config" />
7477
</ItemGroup>
7578
<ItemGroup>

0 commit comments

Comments
 (0)