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 #759

Merged
merged 25 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6f6cd97
Changes for orgs api uptake
AsabuHere Jul 22, 2024
5667bec
uptake of review comments
AsabuHere Jul 31, 2024
a769eac
adding examples for bearer token authentication
AsabuHere Jul 31, 2024
4816bc8
Update documentation for bearer token auth rest calls
AsabuHere Jul 31, 2024
78ac734
Update README.md
AsabuHere Jul 31, 2024
388a4c3
Update README.md
AsabuHere Jul 31, 2024
c5b46b4
Update BearerTokenAuthentication.md
AsabuHere Jul 31, 2024
ed6f6b2
fix: Adding jwt token expiry check function (#754)
AsabuHere Sep 3, 2024
04e9e31
review comments uptake
AsabuHere Sep 3, 2024
692e59d
Renaming TwilioBearerTokenAuthClient to TwilioOrgsTokenAuthClient
AsabuHere Sep 4, 2024
3e03444
hardcoding client credentials
AsabuHere Sep 4, 2024
b4d23b1
invalidate rest clients on change of client id, client secret and tok…
AsabuHere Sep 4, 2024
e72b107
Rename Request classes
AsabuHere Sep 4, 2024
eb6a2bd
Renaming resourceset
AsabuHere Sep 4, 2024
cfa2eb9
renaming http clients
AsabuHere Sep 4, 2024
10c9f08
adding dependencies
AsabuHere Sep 4, 2024
1e5926c
Changes to make previewiam version less
AsabuHere Sep 23, 2024
5fda91d
Marking the new feature as Beta
AsabuHere Sep 23, 2024
0516f4a
Adding to domain file
AsabuHere Sep 23, 2024
0e04987
Adding to domain file
AsabuHere Sep 23, 2024
e44ed27
Merge branch 'main' into orgs_api_uptake
AsabuHere Sep 23, 2024
5227874
Marking all new classes as beta
AsabuHere Sep 23, 2024
383cdf8
Marking all new classes as beta
AsabuHere Sep 23, 2024
25a9ee5
Marking all new classes as beta
AsabuHere Sep 23, 2024
3ecb2d7
Updating readme regarding new annotations
AsabuHere Sep 23, 2024
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
12 changes: 11 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 Expand Up @@ -140,6 +142,14 @@ Console.WriteLine(response);

To use a custom HTTP client with this helper library, please see the [advanced example of how to do so](./advanced-examples/custom-http-client.md).

## Annotations

### Beta
Features marked with the `[Beta]` attribute are in a beta stage and may undergo changes in future releases. Use these features with caution as they may not be stable.

### Preview
Features marked with the `[Preview]` attribute are in a preview stage and are intended for evaluation purposes. They are subject to change and should not be used in production without thorough testing.

## Docker Image

The `Dockerfile` present in this repository and its respective `twilio/twilio-csharp` Docker image are used by Twilio for testing purposes.
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;
}
}
}
124 changes: 124 additions & 0 deletions src/Twilio/Base/BearerToken/TokenResourceSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Reflection;
using System.Collections.Generic;
using Twilio.Clients;
using Twilio.Clients.BearerToken;
using Twilio.Annotations;

namespace Twilio.Base.BearerToken
{
/// <summary>
/// A collection of resources of type T
/// </summary>
///
/// <typeparam name="T">Resource Type</typeparam>
[Beta]
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);

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

namespace Twilio.Clients{

[Beta]
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