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 : Adding helper libraries for organization api and support for bearer token auth and no auth rest calls #757

Closed
wants to merge 20 commits into from
Closed
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ var message = MessageResource.Create(
Console.WriteLine(message.Sid);
```

Examples on how to make rest calls with bearer token authentication is added [here](https://github.com/twilio/twilio-csharp/blob/orgs_api_uptake/examples/BearerTokenAuthentication.md)

## Specify Region and/or Edge

To take advantage of Twilio's [Global Infrastructure](https://www.twilio.com/docs/global-infrastructure), specify the target Region and/or Edge for the client:
Expand All @@ -82,7 +84,7 @@ TwilioClient.SetRegion("au1");
TwilioClient.SetEdge("sydney");
```

This will result in the `hostname` transforming from `api.twilio.com` to `api.sydney.au1.twilio.com`.
This will result in the `hostname` transforming from `api.twilio.com` to `api.sydney.au1.twilio.com`. Use appropriate client depending on the type of authentication used

## Enable debug logging

Expand Down
23 changes: 23 additions & 0 deletions examples/BearerTokenAuthentication.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
```csharp
using System;
using Twilio;
using Twilio.Base;
using Twilio.Exceptions;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Rest.PreviewIam.Organizations;

//Find client id, client secret and organisation sid from admin center of your organisation
//path account sid is the sid of the account withing the organisation
class Program
{
static void Main(string[] args)
{
TwilioOrgsTokenAuthClient.Init(GRANT_TYPE, CLIENT_ID, CLIENT_SECRET);
Twilio.Base.BearerToken.TokenResourceSet<Twilio.Rest.PreviewIam.Organizations.AccountResource> accountList = null;
accountList = Twilio.Rest.PreviewIam.Organizations.AccountResource.Read(pathOrganizationSid: ORGS_SID);
Console.WriteLine(accountList.ElementAt(0).FriendlyName);
var account = Twilio.Rest.PreviewIam.Organizations.AccountResource.Fetch(pathOrganizationSid: ORGS_SID, pathAccountSid: PATH_ACCOUNT_SID);
Console.WriteLine(account.FriendlyName);
}
}
```
15 changes: 15 additions & 0 deletions src/Twilio/Annotations/Beta.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace Twilio.Annotations
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class Beta : Attribute
{
public string Message { get; }

public Beta(string message = "This feature is in beta and may change in future versions.")
{
Message = message;
}
}
}
15 changes: 15 additions & 0 deletions src/Twilio/Annotations/Preview.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace Twilio.Annotations
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = false)]
public sealed class Preview : Attribute
{
public string Value { get; }

public Preview(string value = "This class/method is under preview and is subject to change. Use with caution.")
{
Value = value;
}
}
}
122 changes: 122 additions & 0 deletions src/Twilio/Base/BearerToken/TokenResourceSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System;
using System.Reflection;
using System.Collections.Generic;
using Twilio.Clients;
using Twilio.Clients.BearerToken;

namespace Twilio.Base.BearerToken
{
/// <summary>
/// A collection of resources of type T
/// </summary>
///
/// <typeparam name="T">Resource Type</typeparam>
public class TokenResourceSet<T> : IEnumerable<T> where T : Resource
{
/// <summary>
/// Automatically iterate through pages of results
/// </summary>
public bool AutoPaging { get; set; }

private readonly TwilioOrgsTokenRestClient _client;
private readonly ReadOptions<T> _options;
private readonly long _pageLimit;

private long _pages;
private long _processed;
private Page<T> _page;
private IEnumerator<T> _iterator;

/// <summary>
/// Create a new resource set
/// </summary>
///
/// <param name="page">Page of resources</param>
/// <param name="options">Read options</param>
/// <param name="client">Client to make requests</param>
public TokenResourceSet(Page<T> page, ReadOptions<T> options, TwilioOrgsTokenRestClient client)
{
_page = page;
_options = options;
_client = client;

_iterator = page.Records.GetEnumerator();
_processed = 0;
_pages = 1;
_pageLimit = long.MaxValue;

AutoPaging = true;

if (_options.Limit != null)
{
_pageLimit = (long) (Math.Ceiling((double) _options.Limit.Value / page.PageSize));
}
}

/// <summary>
/// Get iterator for resources
/// </summary>
///
/// <returns>IEnumerator of resources</returns>
public IEnumerator<T> GetEnumerator()
{
while (_page != null)
{
_iterator.Reset();
while (_iterator.MoveNext())
{
// Exit if we've reached item limit
if (_options.Limit != null && _processed > _options.Limit.Value)
{
yield break;
}

_processed++;
yield return _iterator.Current;
}

if (AutoPaging && _page.HasNextPage())
{
FetchNextPage();
}
else
{
break;
}
}
}

/// <summary>
/// Get iterator for resources
/// </summary>
///
/// <returns>IEnumerator of resources</returns>
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

private void FetchNextPage()
{
if (!_page.HasNextPage() || _pages >= _pageLimit)
{
_page = null;
_iterator = null;
return;
}

_pages++;
_page = (Page<T>)GetNextPage().Invoke(null, new object[]{ _page, _client });
_iterator = _page.Records.GetEnumerator();
}

private static MethodInfo GetNextPage()
{
#if !NET35
return typeof(T).GetRuntimeMethod("NextPage", new[]{ typeof(Page<T>), typeof(TwilioOrgsTokenRestClient) });
#else
return typeof(T).GetMethod("NextPage", new[]{ typeof(Page<T>), typeof(TwilioOrgsTokenRestClient) });
#endif
}
}
}
53 changes: 30 additions & 23 deletions src/Twilio/Base/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,36 +125,43 @@ public static Page<T> FromJson(string recordKey, string json)
var parsedRecords = records.Children().Select(
record => JsonConvert.DeserializeObject<T>(record.ToString())
).ToList();

if(root["uri"] != null){
var uriNode = root["uri"];
if (uriNode != null)
{
JToken pageSize;
JToken firstPageUri;
JToken nextPageUri;
JToken previousPageUri;

var uriNode = root["uri"];
if (uriNode != null)
{
JToken pageSize;
JToken firstPageUri;
JToken nextPageUri;
JToken previousPageUri;
// v2010 API
return new Page<T>(
parsedRecords,
root.TryGetValue("page_size", out pageSize) ? root["page_size"].Value<int>() : parsedRecords.Count,
uri: uriNode.Value<string>(),
firstPageUri: root.TryGetValue("first_page_uri", out firstPageUri) ? root["first_page_uri"].Value<string>() : null,
nextPageUri: root.TryGetValue("next_page_uri", out nextPageUri) ? root["next_page_uri"].Value<string>() : null,
previousPageUri: root.TryGetValue("previous_page_uri", out previousPageUri) ? root["previous_page_uri"].Value<string>() : null
);
}
}

// v2010 API
// next-gen API
if(root["meta"] != null){
var meta = root["meta"];
return new Page<T>(
parsedRecords,
root.TryGetValue("page_size", out pageSize) ? root["page_size"].Value<int>() : parsedRecords.Count,
uri: uriNode.Value<string>(),
firstPageUri: root.TryGetValue("first_page_uri", out firstPageUri) ? root["first_page_uri"].Value<string>() : null,
nextPageUri: root.TryGetValue("next_page_uri", out nextPageUri) ? root["next_page_uri"].Value<string>() : null,
previousPageUri: root.TryGetValue("previous_page_uri", out previousPageUri) ? root["previous_page_uri"].Value<string>() : null
meta["page_size"].Value<int>(),
url: meta["url"].Value<string>(),
firstPageUrl: meta["first_page_url"].Value<string>(),
nextPageUrl: meta["next_page_url"].Value<string>(),
previousPageUrl: meta["previous_page_url"].Value<string>()
);
}

// next-gen API
var meta = root["meta"];
return new Page<T>(
parsedRecords,
meta["page_size"].Value<int>(),
url: meta["url"].Value<string>(),
firstPageUrl: meta["first_page_url"].Value<string>(),
nextPageUrl: meta["next_page_url"].Value<string>(),
previousPageUrl: meta["previous_page_url"].Value<string>()
);
return new Page<T>(parsedRecords, 0, null, null, null, null);

}
}
}
30 changes: 30 additions & 0 deletions src/Twilio/Clients/Base64UrlEncoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#if NET35
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Script.Serialization;

namespace Twilio.Clients{

public abstract class Base64UrlEncoder
{
public static string Decode(string base64Url)
{
// Replace URL-safe characters with Base64 characters
string base64 = base64Url
.Replace('-', '+')
.Replace('_', '/');

// Add padding if necessary
switch (base64.Length % 4)
{
case 2: base64 += "=="; break;
case 3: base64 += "="; break;
}

byte[] bytes = Convert.FromBase64String(base64);
return Encoding.UTF8.GetString(bytes);
}
}
}
#endif
Loading
Loading