Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/Twilio/Clients/TwilioRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Newtonsoft.Json;
using Twilio.Exceptions;
using Twilio.AuthStrategies;
using System.Collections.Generic;

#if !NET35
using System.Threading.Tasks;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -248,7 +261,7 @@ private static Response ProcessResponse(Response response)
}

/// <summary>
/// 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().
/// </summary>
Expand Down
25 changes: 25 additions & 0 deletions src/Twilio/Constant/GlobalConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections.Generic;

namespace Twilio
{
public static class GlobalConstants
{
public static readonly Dictionary<string, string> RegionToEdgeMap = new Dictionary<string, string>

Check warning on line 7 in src/Twilio/Constant/GlobalConstants.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use an immutable collection or reduce the accessibility of the public static field 'RegionToEdgeMap'.

See more on https://sonarcloud.io/project/issues?id=twilio_twilio-csharp&issues=AZrZmM0uQlruwxUV25IF&open=AZrZmM0uQlruwxUV25IF&pullRequest=810

Check warning on line 7 in src/Twilio/Constant/GlobalConstants.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use an immutable collection or reduce the accessibility of the non-private readonly field 'RegionToEdgeMap'.

See more on https://sonarcloud.io/project/issues?id=twilio_twilio-csharp&issues=AZrZmM0uQlruwxUV25IE&open=AZrZmM0uQlruwxUV25IE&pullRequest=810
{
{ "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));
}
}
}
15 changes: 13 additions & 2 deletions src/Twilio/Twilio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Twilio.Credential;
using Twilio.AuthStrategies;
using Twilio.Annotations;
using System.Collections.Generic;
using System;

namespace Twilio
{
Expand All @@ -13,7 +15,7 @@
{
private static string _username;
private static string _password;
private static AuthStrategy _authstrategy;

Check warning on line 18 in src/Twilio/Twilio.cs

View workflow job for this annotation

GitHub Actions / Test

The field 'TwilioClient._authstrategy' is never used

Check warning on line 18 in src/Twilio/Twilio.cs

View workflow job for this annotation

GitHub Actions / Test

The field 'TwilioClient._authstrategy' is never used
private static string _accountSid;
private static string _region;
private static string _edge;
Expand All @@ -22,6 +24,7 @@
private static string _logLevel;
private static CredentialProvider _credentialProvider;


private TwilioClient() { }

/// <summary>
Expand Down Expand Up @@ -218,6 +221,15 @@
/// <returns>The rest client</returns>
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;
Expand Down Expand Up @@ -245,7 +257,6 @@
LogLevel = _logLevel
};
}

return _restClient;
}

Expand Down Expand Up @@ -292,7 +303,7 @@
}

/// <summary>
/// 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
/// </summary>
public static void ValidateSslCertificate()
Expand Down
27 changes: 27 additions & 0 deletions test/Twilio.Test/Clients/TwilioRestClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,33 @@ public async Task RequestAsyncWithUserAgentExtensions()

Assert.AreEqual(request.UserAgentExtensions, userAgentExtensions);
}

[Test]
public async Task EdgeWhenRegionIsSet()
{
client.MakeRequestAsync(Arg.Any<Request>()).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<Request>()).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
}
}
44 changes: 44 additions & 0 deletions test/Twilio.Test/Constant/GlobalConstantsTests.cs
Original file line number Diff line number Diff line change
@@ -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"));
}
}

}
Loading