Skip to content

Commit

Permalink
feat: sdk dkims integration
Browse files Browse the repository at this point in the history
  • Loading branch information
blancfabian committed Oct 26, 2023
1 parent dfd43e4 commit 11510f5
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 0 deletions.
79 changes: 79 additions & 0 deletions GreenArrow.Engine/DKIMKeysApi/DKIMKeysApiClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using GreenArrow.Engine.Extensions;
using GreenArrow.Engine.RestApi;
using Microsoft.Extensions.Options;
using System.Net;
using System.Net.Http.Headers;

namespace GreenArrow.Engine.DKIMKeysApi
{
/// <summary>
/// DKIM Keys API client implementation
/// </summary>
public class DKIMKeysApiClient : IDKIMKeysApi
{
private readonly GreenArrowEngineSettings _settings;
private readonly IHttpClientFactory _httpFactory;

private readonly string _endpoint;

/// <summary>
/// Initializes a new instance of Green Arrow DKIM Keys API Client
/// </summary>
/// <param name="options">Green Arrow settings with API Url and Authorization Token</param>
/// <param name="httpFactory">HttpClienFactory for create HttpClient objects</param>
public DKIMKeysApiClient(
IOptions<GreenArrowEngineSettings> options,
IHttpClientFactory httpFactory)
{
_settings = options.Value;
_httpFactory = httpFactory;
_endpoint = GetEndPoint();
}

private HttpClient CreateHttpClient()
{
var client = _httpFactory.CreateClient();
return client;
}

private string GetEndPoint()
{
var baseUri = new Uri(_settings.ServerUri);
var endpointUri = new Uri(baseUri, _settings.DKIMKeysAPIEndpoint);
return endpointUri.ToString();
}

/// <inheritdoc />
public async Task<IRestApiResponse<DKIMKeysResponse>> PostAsync(DKIMKeysRequest request, CancellationToken cancellationToken = default)
{
try
{
var jsonContent = request.ToJson();

var client = CreateHttpClient();

var authenticationString = $"{request.Username}:{request.Password}";
var base64EncodedAuthenticationString = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(authenticationString));

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64EncodedAuthenticationString);

var httpContent = new StringContent(jsonContent, encoding: default, mediaType: "application/json");
var httpResponse = await client.PostAsync(_endpoint, httpContent, cancellationToken);

if (httpResponse.StatusCode == HttpStatusCode.OK)
{
var result = await httpResponse.Content.ReadAsStringAsync(cancellationToken);
var content = result.ToObject<DKIMKeysResponse>();
return new RestApiResponse<DKIMKeysResponse>(httpResponse.StatusCode, content);
}

return new RestApiResponse<DKIMKeysResponse>(httpResponse.StatusCode);

}
catch (Exception exception)
{
throw new RestApiException("Unexpected exception", exception);
}
}
}
}
29 changes: 29 additions & 0 deletions GreenArrow.Engine/DKIMKeysApi/DKIMKeysRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using GreenArrow.Engine.Model;
using GreenArrow.Engine.RestApi;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace GreenArrow.Engine.DKIMKeysApi
{
/// <summary>
/// Create a DKIM Key Request
/// </summary>
[JsonObject(NamingStrategyType = typeof(SnakeCaseNamingStrategy), ItemNullValueHandling = NullValueHandling.Ignore)]
public class DKIMKeysRequest : IRestApiModel
{
/// <summary>
/// Username that is authorized to log in to GreenArrow Engine’s web interface.
/// </summary>
public string Username { get; set; }

/// <summary>
/// Password that is authorized to log in to GreenArrow Engine’s web interface.
/// </summary>
public string Password { get; set; }

/// <summary>
/// To create the private key
/// </summary>
public DkimKey DkimKey { get; set; }
}
}
42 changes: 42 additions & 0 deletions GreenArrow.Engine/DKIMKeysApi/DKIMKeysResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using GreenArrow.Engine.Model;
using GreenArrow.Engine.RestApi;

namespace GreenArrow.Engine.DKIMKeysApi
{
/// <summary>
/// GreenArrow Response is the full DKIM Key record data
/// </summary>
public class DKIMKeysResponse : IRestApiModel
{
/// <summary>
/// When request was succesful created
/// </summary>
public bool Success { get; init; }

/// <summary>
/// Full DKIM Key record data
/// </summary>
public DKIMKeysResponseData Data { get; init; }

/// <summary>
/// Error code when request was not accepted
/// </summary>
public string ErrorCode { get; init; }

/// <summary>
/// Error message when request was not accepted
/// </summary>
public string ErrorMessages { get; init; }
}

/// <summary>
/// Full DKIM Key record data
/// </summary>
public class DKIMKeysResponseData
{
/// <summary>
/// Full DKIM Key record data
/// </summary>
public DkimKey DkimKey { get; init; }
}
}
19 changes: 19 additions & 0 deletions GreenArrow.Engine/DKIMKeysApi/IDKIMKeysApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using GreenArrow.Engine.RestApi;

namespace GreenArrow.Engine.DKIMKeysApi
{
/// <summary>
/// Represent the actions available in Green Arrow Engine DKIM Keys API
/// </summary>
/// <remarks><see href="https://www.greenarrowemail.com/docs/greenarrow-engine/API-V3/Engine/DKIM-Keys"/></remarks>
public interface IDKIMKeysApi
{
/// <summary>
/// Create a DKIM Key
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken">The cancellation toekn</param>
/// <returns>A generic rest api response with the deserialized DKIM Keys API response when success</returns>
Task<IRestApiResponse<DKIMKeysResponse>> PostAsync(DKIMKeysRequest request, CancellationToken cancellationToken = default);
}
}
5 changes: 5 additions & 0 deletions GreenArrow.Engine/GreenArrowEngineSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@ public class GreenArrowEngineSettings
/// The HTTP Submission API Endpoint
/// </summary>
public string HTTPSubmissionAPIEndpoint { get; set; } = "/api/v1/send.json";

/// <summary>
/// The DKIM API Endpoint
/// </summary>
public string DKIMKeysAPIEndpoint { get; set; } = "/api/v3/eng/dkim_keys";
}
}
53 changes: 53 additions & 0 deletions GreenArrow.Engine/Model/DkimKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace GreenArrow.Engine.Model
{
/// <summary>
/// Dkim Key Data
/// </summary>
[JsonObject(NamingStrategyType = typeof(SnakeCaseNamingStrategy), ItemNullValueHandling = NullValueHandling.Ignore)]
public class DkimKey
{
/// <summary>
/// The domain name associated with this DKIM Key.
/// </summary>
public string Domain { get; init; }

/// <summary>
/// The selector used to identify this DKIM Key.
/// </summary>
public string Selector { get; init; }

/// <summary>
/// Whether this is the default DKIM key for this domain.
/// </summary>
public string DefaultForDomain { get; init; }

/// <summary>
/// The key data
/// </summary>
public Key Key { get; init; }
}

/// <summary>
/// The key data
/// </summary>
public class Key
{
/// <summary>
/// The number of bits used to generate this key.
/// </summary>
public int Bits { get; init; }

/// <summary>
/// The PEM-encoded private key.
/// </summary>
public string Private { get; init; }

/// <summary>
/// The public key, derived from the private key. This is PEM-encoded with the header line, footer line, and line-breaks stripped.
/// </summary>
public string Public { get; init; }
}
}

0 comments on commit 11510f5

Please sign in to comment.