Skip to content

Commit

Permalink
adds a setter for data residency + dotnet formats
Browse files Browse the repository at this point in the history
  • Loading branch information
shrutiburman committed Nov 24, 2023
1 parent e66576c commit a84cf9e
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/SendGrid/Permissions/ScopeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public enum ScopeOptions
/// <summary>
/// Read-only scopes. When fitlering scopes this will include only those that end with ".read"
/// </summary>
ReadOnly
ReadOnly
}
}
60 changes: 30 additions & 30 deletions src/SendGrid/Permissions/SendGridPermissionsBuilder.Scopes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,17 +296,17 @@ partial class SendGridPermissionsBuilder
#endregion
#region Mail
,{ SendGridPermission.Mail, new[]
{
{
"mail.batch.create",
"mail.batch.delete",
"mail.batch.read",
"mail.batch.update",
"mail.send"
}}
}}
#endregion
#region Mail Settings
,{ SendGridPermission.MailSettings, new[]
{
{
"mail_settings.address_whitelist.read",
"mail_settings.address_whitelist.update",
"mail_settings.bounce_purge.read",
Expand All @@ -319,56 +319,56 @@ partial class SendGridPermissionsBuilder
"mail_settings.forward_spam.update",
"mail_settings.template.read",
"mail_settings.template.update"
}}
}}
#endregion
#region Marketing Campaigns
,{ SendGridPermission.MarketingCampaigns, new[]
{
{
"marketing_campaigns.create",
"marketing_campaigns.delete",
"marketing_campaigns.read",
"marketing_campaigns.update"
}}
}}
#endregion
#region Newsletter
,{ SendGridPermission.Newsletter, new[]
{
{
"newsletter.create",
"newsletter.delete",
"newsletter.read",
"newsletter.update"
}}
}}
#endregion
#region PartnerSettings
,{ SendGridPermission.PartnerSettings, new[]
{
{
"partner_settings.new_relic.read",
"partner_settings.new_relic.update",
"partner_settings.read"
}}
}}
#endregion
#region Reverse DNS
,{ SendGridPermission.ReverseDNS, new[]
{
{
"access_settings.activity.read",
"access_settings.whitelist.create",
"access_settings.whitelist.delete",
"access_settings.whitelist.read",
"access_settings.whitelist.update"
}}
}}
#endregion
#region Scheduled Sends
,{ SendGridPermission.ScheduledSends, new[]
{
{
"user.scheduled_sends.create",
"user.scheduled_sends.delete",
"user.scheduled_sends.read",
"user.scheduled_sends.update"
}}
}}
#endregion
#region Stats
,{ SendGridPermission.Stats, new[]
{
{
"email_activity.read",
"stats.read",
"stats.global.read",
Expand All @@ -381,11 +381,11 @@ partial class SendGridPermissionsBuilder
"clients.stats.read",
"clients.tablet.stats.read",
"clients.webmail.stats.read"
}}
}}
#endregion
#region Subusers
,{ SendGridPermission.Subusers, new[]
{
{
"subusers.create",
"subusers.delete",
"subusers.read",
Expand All @@ -407,11 +407,11 @@ partial class SendGridPermissionsBuilder
"subusers.stats.monthly.read",
"subusers.stats.sums.read",
"subusers.summary.read"
}}
}}
#endregion
#region Suppressions
,{ SendGridPermission.Suppressions, new[]
{
{
"suppression.create",
"suppression.delete",
"suppression.read",
Expand All @@ -436,20 +436,20 @@ partial class SendGridPermissionsBuilder
"suppression.unsubscribes.read",
"suppression.unsubscribes.update",
"suppression.unsubscribes.delete"
}}
}}
#endregion
#region Teammates
,{ SendGridPermission.Teammates, new[]
{
{
"teammates.create",
"teammates.read",
"teammates.update",
"teammates.delete"
}}
}}
#endregion
#region Templates
,{ SendGridPermission.Templates, new[]
{
{
"templates.create",
"templates.delete",
"templates.read",
Expand All @@ -462,11 +462,11 @@ partial class SendGridPermissionsBuilder
"templates.versions.delete",
"templates.versions.read",
"templates.versions.update"
}}
}}
#endregion
#region Tracking
,{ SendGridPermission.Tracking, new[]
{
{
"tracking_settings.click.read",
"tracking_settings.click.update",
"tracking_settings.google_analytics.read",
Expand All @@ -476,11 +476,11 @@ partial class SendGridPermissionsBuilder
"tracking_settings.read",
"tracking_settings.subscription.read",
"tracking_settings.subscription.update"
}}
}}
#endregion
#region User Settings
,{ SendGridPermission.UserSettings, new[]
{
{
"user.account.read",
"user.credits.read",
"user.email.create",
Expand All @@ -501,11 +501,11 @@ partial class SendGridPermissionsBuilder
"user.timezone.update",
"user.username.read",
"user.username.update"
}}
}}
#endregion
#region Webhooks
,{ SendGridPermission.Webhook, new[]
{
{
"user.webhooks.event.settings.read",
"user.webhooks.event.settings.update",
"user.webhooks.event.test.create",
Expand All @@ -516,7 +516,7 @@ partial class SendGridPermissionsBuilder
"user.webhooks.parse.settings.read",
"user.webhooks.parse.settings.update",
"user.webhooks.parse.stats.read"
}}
}}
#endregion
};
}
Expand Down
4 changes: 3 additions & 1 deletion src/SendGrid/SendGridClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;

Expand Down Expand Up @@ -90,5 +91,6 @@ private static SendGridClientOptions buildOptions(string apiKey, string host, Di
HttpErrorAsException = httpErrorAsException
};
}

}
}
28 changes: 28 additions & 0 deletions src/SendGrid/SendGridClientOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Net.Http.Headers;

namespace SendGrid
Expand All @@ -8,6 +9,12 @@ namespace SendGrid
/// </summary>
public class SendGridClientOptions : BaseClientOptions
{
Dictionary<string, string> REGION_HOST_MAP = new Dictionary<string, string>
{
{"eu", "https://api.eu.sendgrid.com/"},
{"global", "https://api.sendgrid.com/"}
};

/// <summary>
/// Initializes a new instance of the <see cref="SendGridClientOptions"/> class.
/// </summary>
Expand Down Expand Up @@ -36,5 +43,26 @@ public string ApiKey
Auth = new AuthenticationHeaderValue("Bearer", apiKey);
}
}

/// <summary>
/// Sets the data residency for the SendGrid client.
/// </summary>
/// <param name="region">The desired data residency region ("global" or "eu").</param>
/// <returns>The updated SendGridClientOptions instance.</returns>
public SendGridClientOptions SetDataResidency(string region)
{
if (string.IsNullOrWhiteSpace(region))
{
throw new ArgumentNullException(nameof(region));
}

if (!REGION_HOST_MAP.ContainsKey(region))
{
Console.WriteLine("Region can only be 'global' or 'eu'.");
}
string result = REGION_HOST_MAP.ContainsKey(region) ? REGION_HOST_MAP[region] : "https://api.sendgrid.com";
Host = result;
return this;
}
}
}
2 changes: 1 addition & 1 deletion tests/SendGrid.Tests/Integration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6123,7 +6123,7 @@ public async Task TestRetryBehaviourSucceedsOnSecondAttempt()
[Fact]
public async void TestHttpErrorAsExceptionWhenSetInOptions()
{
await TestHttpErrorAsException((client, apiKey) => new SendGridClient(client, new SendGridClientOptions {ApiKey = apiKey, HttpErrorAsException = true}));
await TestHttpErrorAsException((client, apiKey) => new SendGridClient(client, new SendGridClientOptions { ApiKey = apiKey, HttpErrorAsException = true }));
}

[Fact]
Expand Down
10 changes: 5 additions & 5 deletions tests/SendGrid.Tests/LicenseTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace SendGrid.Tests
{
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Xunit;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Xunit;

public class LicenseTests
{
Expand Down
2 changes: 1 addition & 1 deletion tests/SendGrid.Tests/PermissionsBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void IncludeThrowsIfAnyScopeParamIsInvalid()
public void IncludeThrowsIfAnyScopeIsInvalid()
{
var sb = new SendGridPermissionsBuilder();
Assert.Throws<InvalidOperationException>(() => sb.Include(new [] {"alert.create", "bad.scope" }));
Assert.Throws<InvalidOperationException>(() => sb.Include(new[] { "alert.create", "bad.scope" }));
}

[Fact]
Expand Down
4 changes: 2 additions & 2 deletions tests/SendGrid.Tests/Reliability/ReliabilitySettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class ReliabilitySettingsTests
[Fact]
public void ShouldNotAllowNegativeRetryCount()
{
var exception = Assert.Throws<ArgumentOutOfRangeException>(() =>
new ReliabilitySettings(-1,
var exception = Assert.Throws<ArgumentOutOfRangeException>(() =>
new ReliabilitySettings(-1,
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(1)));
Expand Down
3 changes: 2 additions & 1 deletion tests/SendGrid.Tests/RequiredFilesExistTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public class TestRequiredFilesExist
{

// ./Docker or docker/Docker
public void checkDockerExists() {
public void checkDockerExists()
{
bool dockerExists = File.Exists("./Dockerfile") ||
File.Exists("./docker/Dockerfile");
Assert.True(dockerExists);
Expand Down
48 changes: 48 additions & 0 deletions tests/SendGrid.Tests/SendgridEmailClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace SendGrid.Tests
{
using System;
using System.Threading.Tasks;
using SendGrid.Helpers.Mail;
using Xunit;

public class SendgridEmailClientTests
{
[Fact]
public void TestClientOptionsConstruction()
{
var options = new SendGridClientOptions();
Assert.Equal("https://api.sendgrid.com", options.Host);
}
[Fact]
public void TestClientOptionsSetDataResidency()
{
var options = new SendGridClientOptions();
options.SetDataResidency("eu");
Assert.Equal("https://api.eu.sendgrid.com/", options.Host);
}
[Fact]
public void TestClientOptionsSetDataResidencyEU()
{
var options = new SendGridClientOptions();
options.SetDataResidency("eu");
Assert.Equal("https://api.eu.sendgrid.com/", options.Host);
}

[Fact]
public void TestClientOptionsSetViaSendgridClient()
{
var options = new SendGridClientOptions();
options.SetDataResidency("eu");
var sg = new SendGridClient(options);
Assert.Equal("https://api.eu.sendgrid.com/", options.Host);
}

[Fact]
public void TestErrorClientOptions()
{
var options = new SendGridClientOptions();
Assert.Throws<ArgumentNullException>(() => options.SetDataResidency(""));
Assert.Throws<ArgumentNullException>(() => options.SetDataResidency(null));
}
}
}

0 comments on commit a84cf9e

Please sign in to comment.