-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dfd43e4
commit 11510f5
Showing
6 changed files
with
227 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |