Skip to content

Commit

Permalink
Merge branch 'feature/multiple_webhook_settings' into develop
Browse files Browse the repository at this point in the history
Resolves #512, #513, #514, #516, #517, #518, #519
  • Loading branch information
Jericho committed Apr 17, 2024
2 parents 4e9a570 + 8cd93e0 commit 509a695
Show file tree
Hide file tree
Showing 7 changed files with 518 additions and 112 deletions.
16 changes: 8 additions & 8 deletions Source/StrongGrid.IntegrationTests/Tests/WebhookSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ public async Task RunAsync(IBaseClient client, TextWriter log, CancellationToken

await log.WriteLineAsync("\n***** WEBHOOK SETTINGS *****\n").ConfigureAwait(false);

// GET THE EVENT SETTINGS
var eventWebhookSettings = await client.WebhookSettings.GetEventWebhookSettingsAsync(null, cancellationToken).ConfigureAwait(false);
await log.WriteLineAsync("The event webhook settings have been retrieved.").ConfigureAwait(false);
// GET ALL THE EVENT SETTINGS
var eventWebhookSettings = await client.WebhookSettings.GetAllEventWebhookSettingsAsync(null, cancellationToken).ConfigureAwait(false);
await log.WriteLineAsync($"All the event webhook settings have been retrieved. There are {eventWebhookSettings.Length} configured events.").ConfigureAwait(false);

// GET THE INBOUND PARSE SETTINGS
// GET ALL THE INBOUND PARSE SETTINGS
var inboundParseWebhookSettings = await client.WebhookSettings.GetAllInboundParseWebhookSettingsAsync(null, cancellationToken).ConfigureAwait(false);
await log.WriteLineAsync("The inbound parse webhook settings have been retrieved.").ConfigureAwait(false);
await log.WriteLineAsync($"All the inbound parse webhook settings have been retrieved. There are {inboundParseWebhookSettings.Length} configured inbound parse.").ConfigureAwait(false);

// GET THE SIGNED EVENTS PUBLIC KEY
var publicKey = await client.WebhookSettings.GetSignedEventsPublicKeyAsync(cancellationToken).ConfigureAwait(false);
var publicKey = await client.WebhookSettings.GetSignedEventsPublicKeyAsync(null, cancellationToken).ConfigureAwait(false);
await log.WriteLineAsync($"The signed events public key is: {publicKey}").ConfigureAwait(false);

// ==================================================
// DEBUGGING

//const string desiredUrl = "https://4934-2001-18c0-41d-f200-ac79-2199-f1fc-c831.ngrok.io/StrongGrid";
//const string desiredUrl = "https://3928-2001-18c0-41c-3f00-00-632b.ngrok-free.app/StrongGrid";
//const string inboundHostName = "api.stronggrid.com";

// Uncomment the following line to configure all webhook events to be sent to your desired URL
//var updatedEventWebhookSettings = await client.WebhookSettings.UpdateEventWebhookSettingsAsync(true, desiredUrl, true, true, true, true, true, true, true, true, true, true, true, null, cancellationToken).ConfigureAwait(false);
//var updatedEventWebhookSettings = await client.WebhookSettings.UpdateEventWebhookSettingsAsync(true, desiredUrl, true, true, true, true, true, true, true, true, true, true, true, "My friendly Name", null, null, null, null, cancellationToken).ConfigureAwait(false);

// Uncomment the following line to receive a sample webhook at your desired URL
//await client.WebhookSettings.SendEventTestAsync(desiredUrl).ConfigureAwait(false);
Expand Down
8 changes: 5 additions & 3 deletions Source/StrongGrid.UnitTests/Resources/WebhookSettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public async Task UpdateEventWebhookSettingsAsync()
var processed = true;
var spamReport = true;
var unsubscribe = true;
var friendlyName = "My friendly name";

var apiResponse = @"{
""enabled"": true,
Expand All @@ -163,7 +164,8 @@ public async Task UpdateEventWebhookSettingsAsync()
""processed"": true,
""open"": true,
""click"": true,
""dropped"": true
""dropped"": true,
""friendly_name"": ""My friendly name""
}";

var mockHttp = new MockHttpMessageHandler();
Expand All @@ -173,7 +175,7 @@ public async Task UpdateEventWebhookSettingsAsync()
var webhooks = new WebhookSettings(client);

// Act
var result = await webhooks.UpdateEventWebhookSettingsAsync(enabled, url, bounce, click, deferred, delivered, dropped, groupResubscribe, groupUnsubscribe, open, processed, spamReport, unsubscribe, null, CancellationToken.None);
var result = await webhooks.UpdateEventWebhookSettingsAsync(enabled, url, bounce, click, deferred, delivered, dropped, groupResubscribe, groupUnsubscribe, open, processed, spamReport, unsubscribe, friendlyName, null, null, null, null, CancellationToken.None);

// Assert
mockHttp.VerifyNoOutstandingExpectation();
Expand All @@ -194,7 +196,7 @@ public async Task SendEventTestAsync()
var webhooks = new WebhookSettings(client);

// Act
await webhooks.SendEventTestAsync(url, null, CancellationToken.None);
await webhooks.SendEventTestAsync(url, null, null, null, null, CancellationToken.None);

// Assert
mockHttp.VerifyNoOutstandingExpectation();
Expand Down
14 changes: 14 additions & 0 deletions Source/StrongGrid/Extensions/Internal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,20 @@ internal static string ToExactLength(this string source, int totalWidth, string
return result;
}

internal static StrongGridJsonObject ToStrongGridJsonObject<T>(this T source, bool ignoreDefault = true)
{
var jsonObject = new StrongGridJsonObject();
foreach (var property in source.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
var propertyName = ((JsonPropertyNameAttribute)property.GetCustomAttribute(typeof(JsonPropertyNameAttribute))).Name;
var propertyValue = property.GetValue(source);

jsonObject.AddProperty(propertyName, propertyValue, ignoreDefault);
}

return jsonObject;
}

/// <summary>Asynchronously converts the JSON encoded content and convert it to an object of the desired type.</summary>
/// <typeparam name="T">The response model to deserialize into.</typeparam>
/// <param name="httpContent">The content.</param>
Expand Down
111 changes: 111 additions & 0 deletions Source/StrongGrid/Extensions/Public.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1539,5 +1539,116 @@ convert public keys when we stop suporting .NET framework and .NET standard.
var webHookEvents = parser.ParseEventsWebhook(requestBody);
return webHookEvents;
}

/// <summary>
/// Get the current event webhook settings.
/// </summary>
/// <param name="webhookSettings">The webhook settings resource.</param>
/// <param name="onBehalfOf">The user to impersonate.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The <see cref="EventWebhookSettings" />.
/// </returns>
public static Task<EventWebhookSettings> GetEventWebhookSettingsAsync(this IWebhookSettings webhookSettings, string onBehalfOf = null, CancellationToken cancellationToken = default)
{
return webhookSettings.GetEventWebhookSettingsAsync(null, onBehalfOf, cancellationToken);
}

/// <summary>
/// Change the events settings.
/// </summary>
/// <param name="webhookSettings">The webhook settings resource.</param>
/// <param name="enabled">if set to <c>true</c> [enabled].</param>
/// <param name="url">The webhook endpoint url.</param>
/// <param name="bounce">if set to <c>true</c> [bounce].</param>
/// <param name="click">if set to <c>true</c> [click].</param>
/// <param name="deferred">if set to <c>true</c> [deferred].</param>
/// <param name="delivered">if set to <c>true</c> [delivered].</param>
/// <param name="dropped">if set to <c>true</c> [dropped].</param>
/// <param name="groupResubscribe">if set to <c>true</c> [groupResubscribe].</param>
/// <param name="groupUnsubscribe">if set to <c>true</c> [groupUnsubscribe].</param>
/// <param name="open">if set to <c>true</c> [open].</param>
/// <param name="processed">if set to <c>true</c> [processed].</param>
/// <param name="spamReport">if set to <c>true</c> [spamReport].</param>
/// <param name="unsubscribe">if set to <c>true</c> [unsubscribe].</param>
/// <param name="friendlyName">The friendly name.</param>
/// <param name="oauthClientId">The OAuth client ID that SendGrid will pass to your OAuth server or service provider to generate an OAuth access token. When passing data in this parameter, you must also specify the oauthTokenUrl.</param>
/// <param name="oauthClientSecret">The OAuth client secret that SendGrid will pass to your OAuth server or service provider to generate an OAuth access token. This secret is needed only once to create an access token. SendGrid will store the secret, allowing you to update your client ID and Token URL without passing the secret to SendGrid again. When passing data in this parameter, you must also specify the oauthClientId and oauthTokenUrl.</param>
/// <param name="oAuthTokenUrl">The URL where SendGrid will send the OAuth client ID and client secret to generate an OAuth access token. This should be your OAuth server or service provider. When passing data in this parameter, you must also specify the oauthClientId.</param>
/// <param name="onBehalfOf">The user to impersonate.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The <see cref="EventWebhookSettings" />.
/// </returns>
public static Task<EventWebhookSettings> UpdateEventWebhookSettingsAsync(
this IWebhookSettings webhookSettings,
bool enabled,
string url,
bool bounce = default,
bool click = default,
bool deferred = default,
bool delivered = default,
bool dropped = default,
bool groupResubscribe = default,
bool groupUnsubscribe = default,
bool open = default,
bool processed = default,
bool spamReport = default,
bool unsubscribe = default,
string friendlyName = null,
string oauthClientId = null,
string oauthClientSecret = null,
string oAuthTokenUrl = null,
string onBehalfOf = null,
CancellationToken cancellationToken = default)
{
return webhookSettings.UpdateEventWebhookSettingsAsync(null, enabled, url, bounce, click, deferred, delivered, dropped, groupResubscribe, groupUnsubscribe, open, processed, spamReport, unsubscribe, friendlyName, oauthClientId, oauthClientSecret, oAuthTokenUrl, onBehalfOf, cancellationToken);
}

/// <summary>
/// Sends a fake event notification post to the provided URL.
/// </summary>
/// <param name="webhookSettings">The webhook settings resource.</param>
/// <param name="url">The URL where you would like the test notification to be sent.</param>
/// <param name="oAuthClientId">The client ID Twilio SendGrid sends to your OAuth server or service provider to generate an OAuth access token. When passing data in this parameter, you must also specify oauThokenUrl.</param>
/// <param name="oAuthClientSecret">This value is needed only once to create an access token. SendGrid will store this secret, allowing you to update your Client ID and Token URL without passing the secret to SendGrid again. When passing data in this field, you must also specify oAuthClientId and oAuthTokenUrl.</param>
/// <param name="oAuthTokenUrl">The URL where Twilio SendGrid sends the Client ID and Client Secret to generate an access token. This should be your OAuth server or service provider. When passing data in this parameter, you must also include oAuthClientId.</param>
/// <param name="onBehalfOf">The user to impersonate.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The async task.
/// </returns>
public static Task SendEventTestAsync(this IWebhookSettings webhookSettings, string url, string oAuthClientId = null, string oAuthClientSecret = null, string oAuthTokenUrl = null, string onBehalfOf = null, CancellationToken cancellationToken = default)
{
return webhookSettings.SendEventTestAsync(null, url, oAuthClientId, oAuthClientSecret, oAuthTokenUrl, onBehalfOf, cancellationToken);
}

/// <summary>
/// Enable or disable signature verification for a single Event Webhook.
/// </summary>
/// <param name="webhookSettings">The webhook settings resource.</param>
/// <param name="id">The ID of the Event Webhook you want to update.</param>
/// <param name="enabled">Indicates if the signature verification should be enbladle or not.</param>
/// <param name="onBehalfOf">The user to impersonate.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The async task.</returns>
public static Task ToggleEventWebhookSignatureVerificationAsync(this IWebhookSettings webhookSettings, string id, bool enabled, string onBehalfOf = null, CancellationToken cancellationToken = default)
{
return webhookSettings.ToggleEventWebhookSignatureVerificationAsync(null, enabled, onBehalfOf, cancellationToken);
}

/// <summary>
/// Get the signed events public key.
/// </summary>
/// <param name="webhookSettings">The webhook settings resource.</param>
/// <param name="onBehalfOf">The user to impersonate.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The public key.
/// </returns>
public static Task<string> GetSignedEventsPublicKeyAsync(this IWebhookSettings webhookSettings, string onBehalfOf = null, CancellationToken cancellationToken = default)
{
return webhookSettings.GetSignedEventsPublicKeyAsync(null, onBehalfOf, cancellationToken);
}
}
}
Loading

0 comments on commit 509a695

Please sign in to comment.