diff --git a/src/Twilio/Clients/TwilioRestClient.cs b/src/Twilio/Clients/TwilioRestClient.cs index 55d6871e1..34ada8a57 100644 --- a/src/Twilio/Clients/TwilioRestClient.cs +++ b/src/Twilio/Clients/TwilioRestClient.cs @@ -4,6 +4,7 @@ using Newtonsoft.Json; using Twilio.Exceptions; using Twilio.AuthStrategies; +using System.Collections.Generic; #if !NET35 using System.Threading.Tasks; @@ -79,6 +80,9 @@ public TwilioRestClient( AccountSid = accountSid ?? username; HttpClient = httpClient ?? DefaultClient(); + if (GlobalConstants.IsOnlyOneSet(edge,region)) + Console.WriteLine("Deprecation Warning: For regional processing, DNS is of format product.edge.region.twilio.com;otherwise use product.twilio.com"); + Region = region; Edge = edge; } @@ -111,6 +115,9 @@ public TwilioRestClient( AccountSid = accountSid ?? username; HttpClient = httpClient ?? DefaultClient(); + if (GlobalConstants.IsOnlyOneSet(edge,region)) + Console.WriteLine("Deprecation Warning: For regional processing, DNS is of format product.edge.region.twilio.com;otherwise use product.twilio.com"); + Region = region; Edge = edge; } @@ -124,6 +131,12 @@ public TwilioRestClient( public Response Request(Request request) { + if (string.IsNullOrEmpty(Edge) && !string.IsNullOrEmpty(Region) && GlobalConstants.RegionToEdgeMap.TryGetValue(Region, out var edge)) + { + Console.WriteLine("Deprecation Warning: Setting default `edge` for provided `region`"); + Edge = edge; + } + if(_username != null && _password != null){ request.SetAuth(_username, _password); } @@ -248,7 +261,7 @@ private static Response ProcessResponse(Response response) } /// - /// Test if your environment is impacted by a TLS or certificate change + /// Test if your environment is impacted by a TLS or certificate change /// by sending an HTTP request to the test endpoint tls-test.twilio.com:443 /// It's a bit easier to call this method from TwilioClient.ValidateSslCertificate(). /// diff --git a/src/Twilio/Constant/GlobalConstants.cs b/src/Twilio/Constant/GlobalConstants.cs new file mode 100644 index 000000000..1774af422 --- /dev/null +++ b/src/Twilio/Constant/GlobalConstants.cs @@ -0,0 +1,25 @@ +using System.Collections.Generic; + +namespace Twilio +{ + public static class GlobalConstants + { + public static readonly Dictionary RegionToEdgeMap = new Dictionary + { + { "au1", "sydney" }, + { "br1", "sao-paulo" }, + { "de1", "frankfurt" }, + { "ie1", "dublin" }, + { "jp1", "tokyo" }, + { "jp2", "osaka" }, + { "sg1", "singapore" }, + { "us1", "ashburn" }, + { "us2", "umatilla" } + }; + public static bool IsOnlyOneSet(string edge, string region) + { + return (string.IsNullOrEmpty(edge) && !string.IsNullOrEmpty(region)) || + (string.IsNullOrEmpty(region) && !string.IsNullOrEmpty(edge)); + } + } +} diff --git a/src/Twilio/Twilio.cs b/src/Twilio/Twilio.cs index 4461ed565..551eb95de 100644 --- a/src/Twilio/Twilio.cs +++ b/src/Twilio/Twilio.cs @@ -3,6 +3,8 @@ using Twilio.Credential; using Twilio.AuthStrategies; using Twilio.Annotations; +using System.Collections.Generic; +using System; namespace Twilio { @@ -22,6 +24,7 @@ public class TwilioClient private static string _logLevel; private static CredentialProvider _credentialProvider; + private TwilioClient() { } /// @@ -218,6 +221,15 @@ public static ITwilioRestClient GetNoAuthRestClient() /// The rest client public static ITwilioRestClient GetRestClient() { + if (GlobalConstants.IsOnlyOneSet(_edge,_region)) + Console.WriteLine("Deprecation Warning: For regional processing, DNS is of format product.edge.region.twilio.com;otherwise use product.twilio.com"); + + if (string.IsNullOrEmpty(_edge) && !string.IsNullOrEmpty(_region) && GlobalConstants.RegionToEdgeMap.TryGetValue(_region, out var edge)) + { + Console.WriteLine("Deprecation Warning: Setting default `edge` for provided `region`"); + _edge = edge; + } + if (_restClient != null) { return _restClient; @@ -245,7 +257,6 @@ public static ITwilioRestClient GetRestClient() LogLevel = _logLevel }; } - return _restClient; } @@ -292,7 +303,7 @@ public static void InvalidateBasicCreds() } /// - /// Test if your environment is impacted by a TLS or certificate change + /// Test if your environment is impacted by a TLS or certificate change /// by sending an HTTP request to the test endpoint tls-test.twilio.com:443 /// public static void ValidateSslCertificate() diff --git a/test/Twilio.Test/Clients/TwilioRestClientTest.cs b/test/Twilio.Test/Clients/TwilioRestClientTest.cs index 28cc5361c..964b2d35d 100644 --- a/test/Twilio.Test/Clients/TwilioRestClientTest.cs +++ b/test/Twilio.Test/Clients/TwilioRestClientTest.cs @@ -189,6 +189,33 @@ public async Task RequestAsyncWithUserAgentExtensions() Assert.AreEqual(request.UserAgentExtensions, userAgentExtensions); } + + [Test] + public async Task EdgeWhenRegionIsSet() + { + client.MakeRequestAsync(Arg.Any()).Returns(new Response(HttpStatusCode.OK, "OK")); + Request request = new Request(HttpMethod.Get, "https://verify.twilio.com/"); + TwilioRestClient twilioClient = new TwilioRestClient("foo", "bar", region: "us1", httpClient: client); + + await twilioClient.RequestAsync(request); + + Assert.AreEqual("ashburn", request.Edge); + Assert.AreEqual("us1", request.Region); + } + + [Test] + public async Task EdgeWhenRegionIsSetAbc() + { + client.MakeRequestAsync(Arg.Any()).Returns(new Response(HttpStatusCode.OK, "OK")); + Request request = new Request(HttpMethod.Get, "https://verify.twilio.com/"); + TwilioRestClient twilioClient = new TwilioRestClient("foo", "bar", region: "abc", httpClient: client); + + await twilioClient.RequestAsync(request); + + Assert.AreEqual("", request.Edge); + Assert.AreEqual("abc", request.Region); + } + #endif } } diff --git a/test/Twilio.Test/Constant/GlobalConstantsTests.cs b/test/Twilio.Test/Constant/GlobalConstantsTests.cs new file mode 100644 index 000000000..f92ea679e --- /dev/null +++ b/test/Twilio.Test/Constant/GlobalConstantsTests.cs @@ -0,0 +1,44 @@ +using NUnit.Framework; +using Twilio.Constant; +using System.Collections.Generic; + +namespace Twilio.Tests +{ + [TestFixture] + public class GlobalConstantsTests + { + [TestCase(null, null, ExpectedResult = false)] + [TestCase("", "", ExpectedResult = false)] + [TestCase("edge", null, ExpectedResult = true)] + [TestCase(null, "region", ExpectedResult = true)] + [TestCase("edge", "", ExpectedResult = true)] + [TestCase("", "region", ExpectedResult = true)] + [TestCase("edge", "region", ExpectedResult = false)] + public bool IsOnlyOneSet_ReturnsExpected(string edge, string region) + { + return GlobalConstants.IsOnlyOneSet(edge, region); + } + } + + [TestFixture] + public class GlobalConstantsMapTests + { + [Test] + public void RegionToEdgeMap_ContainsExpectedEntries() + { + // Example: adjust key-value pairs to match your actual map + Assert.IsTrue(GlobalConstants.RegionToEdgeMap.ContainsKey("us1")); + Assert.AreEqual("ashburn", GlobalConstants.RegionToEdgeMap["us1"]); + + Assert.IsTrue(GlobalConstants.RegionToEdgeMap.ContainsKey("ie1")); + Assert.AreEqual("dublin", GlobalConstants.RegionToEdgeMap["ie1"]); + } + + [Test] + public void RegionToEdgeMap_DoesNotContainUnexpectedKey() + { + Assert.IsFalse(GlobalConstants.RegionToEdgeMap.ContainsKey("unexpected")); + } + } + +}