Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add regional and edge support #520

Merged
merged 3 commits into from
May 13, 2020
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ var message = MessageResource.Create(
Console.WriteLine(message.Sid);
```

### Specify Region and/or Edge

```csharp
TwilioClient.SetRegion("au1");
TwilioClient.SetEdge("sydney");
```

This will result in the `hostname` transforming from `api.twilio.com` to `api.sydney.au1.twilio.com`.

## Generating TwiML

To control phone calls, your application needs to output [TwiML][twiml].
Expand Down
47 changes: 19 additions & 28 deletions src/Twilio/Base/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public class Page<T> where T : Resource
private Page(
List<T> records,
int pageSize,
string uri=null,
string url=null,
string firstPageUri=null,
string firstPageUrl=null,
string previousPageUri=null,
string previousPageUrl=null,
string nextPageUri=null,
string nextPageUrl=null
string uri = null,
string url = null,
string firstPageUri = null,
string firstPageUrl = null,
string previousPageUri = null,
string previousPageUrl = null,
string nextPageUri = null,
string nextPageUrl = null
)
{
Records = records;
Expand All @@ -58,18 +58,9 @@ private Page(
_previousPageUrl = previousPageUrl;
}

private static string UrlFromUri(Domain domain, string region, string uri)
private static string UrlFromUri(Domain domain, string uri)
{
var b = new StringBuilder();
b.Append("https://").Append(domain);

if (!IsNullOrEmpty(region))
{
b.Append(".").Append(region);
}

b.Append(".twilio.com").Append(uri);
return b.ToString();
return "https://" + domain + ".twilio.com" + uri;
}

/// <summary>
Expand All @@ -78,9 +69,9 @@ private static string UrlFromUri(Domain domain, string region, string uri)
/// <param name="domain">Twilio subdomain</param>
/// <param name="region">Twilio region</param>
/// <returns>URL for the first page of results</returns>
public string GetFirstPageUrl(Domain domain, string region)
public string GetFirstPageUrl(Domain domain, string region = null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this just for backwards compatibility? Don't see region being used in this func.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it will be dropped soon but requires a yoyo change.

{
return _firstPageUrl ?? UrlFromUri(domain, region, _firstPageUri);
return _firstPageUrl ?? UrlFromUri(domain, _firstPageUri);
}

/// <summary>
Expand All @@ -89,9 +80,9 @@ public string GetFirstPageUrl(Domain domain, string region)
/// <param name="domain">Twilio subdomain</param>
/// <param name="region">Twilio region</param>
/// <returns>URL for the next page of results</returns>
public string GetNextPageUrl(Domain domain, string region)
public string GetNextPageUrl(Domain domain, string region = null)
{
return _nextPageUrl ?? UrlFromUri(domain, region, _nextPageUri);
return _nextPageUrl ?? UrlFromUri(domain, _nextPageUri);
}

/// <summary>
Expand All @@ -100,9 +91,9 @@ public string GetNextPageUrl(Domain domain, string region)
/// <param name="domain">Twilio subdomain</param>
/// <param name="region">Twilio region</param>
/// <returns>URL for the previous page of results</returns>
public string GetPreviousPageUrl(Domain domain, string region)
public string GetPreviousPageUrl(Domain domain, string region = null)
{
return _previousPageUrl ?? UrlFromUri(domain, region, _previousPageUri);
return _previousPageUrl ?? UrlFromUri(domain, _previousPageUri);
}

/// <summary>
Expand All @@ -111,9 +102,9 @@ public string GetPreviousPageUrl(Domain domain, string region)
/// <param name="domain">Twilio subdomain</param>
/// <param name="region">Twilio region</param>
/// <returns>URL for the current page of results</returns>
public string GetUrl(Domain domain, string region)
public string GetUrl(Domain domain, string region = null)
{
return _url ?? UrlFromUri(domain, region, _uri);
return _url ?? UrlFromUri(domain, _uri);
}

/// <summary>
Expand Down Expand Up @@ -170,4 +161,4 @@ record => JsonConvert.DeserializeObject<T>(record.ToString())
);
}
}
}
}
21 changes: 19 additions & 2 deletions src/Twilio/Clients/TwilioRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public class TwilioRestClient : ITwilioRestClient
/// </summary>
public string Region { get; }
eshanholtz marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Twilio edge to make requests to
/// </summary>
public string Edge { get; set; }

private readonly string _username;
private readonly string _password;

Expand All @@ -47,20 +52,24 @@ public class TwilioRestClient : ITwilioRestClient
/// <param name="accountSid">account sid to make requests for</param>
/// <param name="region">region to make requests for</param>
/// <param name="httpClient">http client used to make the requests</param>
/// <param name="edge">edge to make requests for</param>
public TwilioRestClient(
string username,
string password,
string accountSid = null,
string region = null,
HttpClient httpClient = null
HttpClient httpClient = null,
string edge = null
)
{
_username = username;
_password = password;

AccountSid = accountSid ?? username;
HttpClient = httpClient ?? DefaultClient();

Region = region;
Edge = edge;
}

/// <summary>
Expand All @@ -72,6 +81,13 @@ public TwilioRestClient(
public Response Request(Request request)
{
request.SetAuth(_username, _password);

if (Region != null)
request.Region = Region;

if (Edge != null)
request.Edge = Edge;

Response response;
try
{
Expand Down Expand Up @@ -191,7 +207,8 @@ public static void ValidateSslCertificate(HttpClient client)
);
}
}
catch (CertificateValidationException e) {
catch (CertificateValidationException e)
{
throw e;
}
catch (Exception e)
Expand Down
Loading