Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*.cs]

# Default severity for analyzer diagnostics with category 'Style'
dotnet_analyzer_diagnostic.category-Style.severity = suggestion
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -25,6 +27,7 @@ public SendResponse RunExample()
message.ReplyTo.Email = "replyto@example.com";
message.To.Add("recipient1@example.com");

client.RequestTimeout = 50;
return client.Send(message);
}
}
Expand Down
14 changes: 6 additions & 8 deletions Example Projects/dotNetCoreExample/Examples/ExampleConfig.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
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 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";

public static int ServerId => 0; //your serverId
public static string ApiKey => "your api key";


}
}
}
2 changes: 1 addition & 1 deletion Example Projects/dotNetCoreExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static void Main()
quit = selection.ToLower().Trim() == "quit";
if (quit)
continue;

var exampleClassName = GetExampleName(selection);
if(exampleClassName == null)
continue;
Expand Down
9 changes: 7 additions & 2 deletions SocketLabs.sln
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/SocketLabs/InjectionApi/SendResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public string ResponseMessage

case SendResult.Timeout:
return "A timeout occurred sending the message";

case SendResult.Success:
return "Successful send of message";

Expand Down
24 changes: 16 additions & 8 deletions src/SocketLabs/InjectionApi/SocketLabsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,24 @@ namespace SocketLabs.InjectionApi
/// }
///</code>
/// </example>
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;

/// <summary>
/// The SocketLabs Injection API endpoint Url
/// </summary>
public string EndpointUrl { get; set; } = "https://inject.socketlabs.com/api/v1/email";

/// <summary>
/// A timeout period for the Injection API request (in Seconds). Default: 120s
/// </summary>
public int RequestTimeout { get; set; } = 120;

/// <summary>
/// Creates a new instance of the <c>SocketLabsClient</c>.
/// </summary>
Expand All @@ -53,18 +58,19 @@ public SocketLabsClient(int serverId, string apiKey)
_apiKey = apiKey;
_httpClient = BuildHttpClient(null);
}

/// <summary>
/// Creates a new instance of the <c>SocketLabsClient</c> with a proxy.
/// </summary>
/// <param name="serverId">Your SocketLabs ServerId number.</param>
/// <param name="apiKey">Your SocketLabs Injection API key.</param>
/// <param name="optionalProxy">The WebProxy you would like to use.</param>
public SocketLabsClient(int serverId, string apiKey, IWebProxy optionalProxy)
public SocketLabsClient(int serverId, string apiKey, IWebProxy optionalProxy)
{
_serverId = serverId;
_apiKey = apiKey;
_httpClient = BuildHttpClient(optionalProxy);

}

/// <summary>
Expand All @@ -84,7 +90,7 @@ 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;
}
Expand Down Expand Up @@ -216,20 +222,21 @@ public async Task<SendResponse> 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;
}

/// <summary>
/// Asynchronously sends a bulk email message and returns the response from the Injection API.
/// </summary>
Expand Down Expand Up @@ -274,6 +281,7 @@ public async Task<SendResponse> 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);
Expand Down
5 changes: 4 additions & 1 deletion test/SocketLabs.Test/SocketLabs.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<PropertyGroup Condition="'$(TeamCitySignAssembly)' != ''">
<SignAssembly>true</SignAssembly>
<DelaySign>false</DelaySign>
</PropertyGroup>
</PropertyGroup>
<ItemGroup>
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\..\packages\Castle.Core.4.3.1\lib\net45\Castle.Core.dll</HintPath>
Expand Down Expand Up @@ -70,6 +70,9 @@
<Compile Include="Validation\SendValidatorTest.cs" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\.editorconfig">
<Link>.editorconfig</Link>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
Expand Down