Skip to content

Commit 4e60b3c

Browse files
Merge pull request #9 from SaranyaKavuri48/request-timeout
LU-40 C# Http Request Timeout
2 parents 0be2f2b + af60d5a commit 4e60b3c

File tree

8 files changed

+43
-22
lines changed

8 files changed

+43
-22
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: 4 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,7 @@ public SendResponse RunExample()
2527
message.ReplyTo.Email = "replyto@example.com";
2628
message.To.Add("recipient1@example.com");
2729

30+
client.RequestTimeout = 50;
2831
return client.Send(message);
2932
}
3033
}
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
namespace dotNetCoreExample.Examples
1+
using System;
2+
namespace dotNetCoreExample.Examples
23
{
34
public static class ExampleConfig
45
{
5-
//public static int ServerId => Environment.GetEnvironmentVariable("SocketlabsServerId", EnvironmentVariableTarget.User);
6-
//public static string ApiKey => Environment.GetEnvironmentVariable("SocketlabsApiPassword", EnvironmentVariableTarget.User);
6+
public static int ServerId => int.Parse(Environment.GetEnvironmentVariable("SocketlabsServerId", EnvironmentVariableTarget.User));
7+
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";
11-
9+
1210
}
13-
}
11+
}

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public string ResponseMessage
3636

3737
case SendResult.Timeout:
3838
return "A timeout occurred sending the message";
39-
39+
4040
case SendResult.Success:
4141
return "Successful send of message";
4242

src/SocketLabs/InjectionApi/SocketLabsClient.cs

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

3636
private readonly int _serverId;
3737
private readonly string _apiKey;
3838
private readonly HttpClient _httpClient;
39-
39+
4040
/// <summary>
4141
/// The SocketLabs Injection API endpoint Url
4242
/// </summary>
4343
public string EndpointUrl { get; set; } = "https://inject.socketlabs.com/api/v1/email";
4444

45+
/// <summary>
46+
/// A timeout period for the Injection API request (in Seconds). Default: 120s
47+
/// </summary>
48+
public int RequestTimeout { get; set; } = 120;
49+
4550
/// <summary>
4651
/// Creates a new instance of the <c>SocketLabsClient</c>.
4752
/// </summary>
@@ -53,18 +58,19 @@ public SocketLabsClient(int serverId, string apiKey)
5358
_apiKey = apiKey;
5459
_httpClient = BuildHttpClient(null);
5560
}
56-
61+
5762
/// <summary>
5863
/// Creates a new instance of the <c>SocketLabsClient</c> with a proxy.
5964
/// </summary>
6065
/// <param name="serverId">Your SocketLabs ServerId number.</param>
6166
/// <param name="apiKey">Your SocketLabs Injection API key.</param>
6267
/// <param name="optionalProxy">The WebProxy you would like to use.</param>
63-
public SocketLabsClient(int serverId, string apiKey, IWebProxy optionalProxy)
68+
public SocketLabsClient(int serverId, string apiKey, IWebProxy optionalProxy)
6469
{
6570
_serverId = serverId;
6671
_apiKey = apiKey;
6772
_httpClient = BuildHttpClient(optionalProxy);
73+
6874
}
6975

7076
/// <summary>
@@ -84,7 +90,7 @@ public SocketLabsClient(int serverId, string apiKey, HttpClient httpClient)
8490

8591
private HttpClient BuildHttpClient(IWebProxy optionalProxy)
8692
{
87-
var httpClient = optionalProxy != null ? new HttpClient(new HttpClientHandler() {UseProxy = true, Proxy = optionalProxy}) : new HttpClient();
93+
var httpClient = optionalProxy != null ? new HttpClient(new HttpClientHandler() { UseProxy = true, Proxy = optionalProxy}) : new HttpClient();
8894
ConfigureHttpClient(httpClient);
8995
return httpClient;
9096
}
@@ -216,20 +222,21 @@ public async Task<SendResponse> SendAsync(IBasicMessage message)
216222

217223
var validationResult = validator.ValidateCredentials(_serverId, _apiKey);
218224
if (validationResult.Result != SendResult.Success) return validationResult;
219-
225+
220226
validationResult = validator.ValidateMessage(message);
221227
if(validationResult.Result != SendResult.Success) return validationResult;
222228

223229
var factory = new InjectionRequestFactory(_serverId, _apiKey);
224230
var injectionRequest = factory.GenerateRequest(message);
225231
var json = injectionRequest.GetAsJson();
226232

227-
var httpResponse = await _httpClient.PostAsync(EndpointUrl, json);
233+
_httpClient.Timeout = TimeSpan.FromSeconds(RequestTimeout);
234+
var httpResponse = await _httpClient.PostAsync(EndpointUrl,json);
228235

229236
var response = new InjectionResponseParser().Parse(httpResponse);
230237
return response;
231238
}
232-
239+
233240
/// <summary>
234241
/// Asynchronously sends a bulk email message and returns the response from the Injection API.
235242
/// </summary>
@@ -274,6 +281,7 @@ public async Task<SendResponse> SendAsync(IBulkMessage message)
274281
var factory = new InjectionRequestFactory(_serverId, _apiKey);
275282
var injectionRequest = factory.GenerateRequest(message);
276283

284+
_httpClient.Timeout = TimeSpan.FromSeconds(RequestTimeout);
277285
var httpResponse = await _httpClient.PostAsync(EndpointUrl, injectionRequest.GetAsJson());
278286

279287
var response = new InjectionResponseParser().Parse(httpResponse);

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)