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: Added Cancellation Token #772

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions src/Twilio/Clients/BearerToken/TwilioOrgsTokenRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public bool isTokenExpired(string token){
///
/// <param name="request">request to make</param>
/// <returns>Task that resolves to the response of the request</returns>
public async Task<Response> RequestAsync(TokenRequest request)
public async Task<Response> RequestAsync(TokenRequest request, System.Threading.CancellationToken cancellationToken = default)
{
request.SetAuth(_accessToken);

Expand All @@ -252,7 +252,7 @@ public async Task<Response> RequestAsync(TokenRequest request)
Response response;
try
{
response = await HttpClient.MakeRequestAsync(request);
response = await HttpClient.MakeRequestAsync(request, cancellationToken);
}
catch (Exception clientException)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Twilio/Clients/ITwilioRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public interface ITwilioRestClient
///
/// <param name="request">Request to make</param>
/// <returns>response of the request</returns>
System.Threading.Tasks.Task<Response> RequestAsync(Request request);
System.Threading.Tasks.Task<Response> RequestAsync(Request request, System.Threading.CancellationToken cancellationToken = default);
#endif
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Twilio/Clients/TwilioRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public Response Request(Request request)
///
/// <param name="request">request to make</param>
/// <returns>Task that resolves to the response of the request</returns>
public async Task<Response> RequestAsync(Request request)
public async Task<Response> RequestAsync(Request request, System.Threading.CancellationToken cancellationToken = default)
{
if(_username != null && _password != null){
request.SetAuth(_username, _password);
Expand All @@ -190,7 +190,7 @@ public async Task<Response> RequestAsync(Request request)
Response response;
try
{
response = await HttpClient.MakeRequestAsync(request);
response = await HttpClient.MakeRequestAsync(request, cancellationToken);
}
catch (Exception clientException)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Twilio/Http/BearerToken/SystemNetTokenHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public override Response MakeRequest(TokenRequest request)
/// </summary>
/// <param name="request">Twilio response</param>
/// <returns>Task that resolves to the response</returns>
public override async Task<Response> MakeRequestAsync(TokenRequest request)
public override async Task<Response> MakeRequestAsync(TokenRequest request, System.Threading.CancellationToken cancellationToken = default)
{
var httpRequest = BuildHttpRequest(request);
if (!Equals(request.Method, HttpMethod.Get))
Expand All @@ -81,7 +81,7 @@ public override async Task<Response> MakeRequestAsync(TokenRequest request)
this.LastRequest = request;
this.LastResponse = null;

var httpResponse = await _httpClient.SendAsync(httpRequest).ConfigureAwait(false);
var httpResponse = await _httpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
var reader = new StreamReader(await httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false));

// Create and return a new Response. Keep a reference to the last
Expand Down
2 changes: 1 addition & 1 deletion src/Twilio/Http/BearerToken/TokenHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public abstract class TokenHttpClient
/// <param name="request">request to make</param>
/// <exception>throws exception on network or connection errors.</exception>
/// <returns>response of the request</returns>
public abstract System.Threading.Tasks.Task<Response> MakeRequestAsync(TokenRequest request);
public abstract System.Threading.Tasks.Task<Response> MakeRequestAsync(TokenRequest request, System.Threading.CancellationToken cancellationToken = default);
#endif

}
Expand Down
2 changes: 1 addition & 1 deletion src/Twilio/Http/HttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public abstract class HttpClient
/// <param name="request">request to make</param>
/// <exception>throws exception on network or connection errors.</exception>
/// <returns>response of the request</returns>
public abstract System.Threading.Tasks.Task<Response> MakeRequestAsync(Request request);
public abstract System.Threading.Tasks.Task<Response> MakeRequestAsync(Request request, System.Threading.CancellationToken cancellationToken = default);
#endif

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Twilio/Http/SystemNetHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public override Response MakeRequest(Request request)
/// </summary>
/// <param name="request">Twilio response</param>
/// <returns>Task that resolves to the response</returns>
public override async Task<Response> MakeRequestAsync(Request request)
public override async Task<Response> MakeRequestAsync(Request request, System.Threading.CancellationToken cancellationToken = default)
{
var httpRequest = BuildHttpRequest(request);
if (!Equals(request.Method, HttpMethod.Get))
Expand All @@ -76,7 +76,7 @@ public override async Task<Response> MakeRequestAsync(Request request)
this.LastRequest = request;
this.LastResponse = null;

var httpResponse = await _httpClient.SendAsync(httpRequest).ConfigureAwait(false);
var httpResponse = await _httpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
var reader = new StreamReader(await httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false));

// Create and return a new Response. Keep a reference to the last
Expand Down
12 changes: 7 additions & 5 deletions src/Twilio/Rest/Accounts/V1/AuthTokenPromotionResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ public static AuthTokenPromotionResource Update(UpdateAuthTokenPromotionOptions
/// <param name="client"> Client to make requests to Twilio </param>
/// <returns> Task that resolves to A single instance of AuthTokenPromotion </returns>
#if !NET35
public static async System.Threading.Tasks.Task<AuthTokenPromotionResource> UpdateAsync(UpdateAuthTokenPromotionOptions options,
ITwilioRestClient client = null)
public static async System.Threading.Tasks.Task<AuthTokenPromotionResource> UpdateAsync(UpdateAuthTokenPromotionOptions options,
ITwilioRestClient client = null,
System.Threading.CancellationToken cancellationToken = default)
{
client = client ?? TwilioClient.GetRestClient();
var response = await client.RequestAsync(BuildUpdateRequest(options, client));
var response = await client.RequestAsync(BuildUpdateRequest(options, client), cancellationToken);
return FromJson(response.Content);
}
#endif
Expand All @@ -90,10 +91,11 @@ public static AuthTokenPromotionResource Update(
/// <param name="client"> Client to make requests to Twilio </param>
/// <returns> Task that resolves to A single instance of AuthTokenPromotion </returns>
public static async System.Threading.Tasks.Task<AuthTokenPromotionResource> UpdateAsync(
ITwilioRestClient client = null)
ITwilioRestClient client = null,
System.Threading.CancellationToken cancellationToken = default)
{
var options = new UpdateAuthTokenPromotionOptions(){ };
return await UpdateAsync(options, client);
return await UpdateAsync(options, client, cancellationToken);
}
#endif

Expand Down
9 changes: 5 additions & 4 deletions src/Twilio/Rest/Accounts/V1/BulkConsentsResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ public static BulkConsentsResource Create(CreateBulkConsentsOptions options, ITw
/// <param name="options"> Create BulkConsents parameters </param>
/// <param name="client"> Client to make requests to Twilio </param>
/// <returns> Task that resolves to A single instance of BulkConsents </returns>
public static async System.Threading.Tasks.Task<BulkConsentsResource> CreateAsync(CreateBulkConsentsOptions options, ITwilioRestClient client = null)
public static async System.Threading.Tasks.Task<BulkConsentsResource> CreateAsync(CreateBulkConsentsOptions options, ITwilioRestClient client = null, System.Threading.CancellationToken cancellationToken = default)
{
client = client ?? TwilioClient.GetRestClient();
var response = await client.RequestAsync(BuildCreateRequest(options, client));
var response = await client.RequestAsync(BuildCreateRequest(options, client), cancellationToken);
return FromJson(response.Content);
}
#endif
Expand All @@ -93,10 +93,11 @@ public static BulkConsentsResource Create(
/// <returns> Task that resolves to A single instance of BulkConsents </returns>
public static async System.Threading.Tasks.Task<BulkConsentsResource> CreateAsync(
List<object> items,
ITwilioRestClient client = null)
ITwilioRestClient client = null,
System.Threading.CancellationToken cancellationToken = default)
{
var options = new CreateBulkConsentsOptions(items){ };
return await CreateAsync(options, client);
return await CreateAsync(options, client, cancellationToken);
}
#endif

Expand Down
9 changes: 5 additions & 4 deletions src/Twilio/Rest/Accounts/V1/BulkContactsResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ public static BulkContactsResource Create(CreateBulkContactsOptions options, ITw
/// <param name="options"> Create BulkContacts parameters </param>
/// <param name="client"> Client to make requests to Twilio </param>
/// <returns> Task that resolves to A single instance of BulkContacts </returns>
public static async System.Threading.Tasks.Task<BulkContactsResource> CreateAsync(CreateBulkContactsOptions options, ITwilioRestClient client = null)
public static async System.Threading.Tasks.Task<BulkContactsResource> CreateAsync(CreateBulkContactsOptions options, ITwilioRestClient client = null, System.Threading.CancellationToken cancellationToken = default)
{
client = client ?? TwilioClient.GetRestClient();
var response = await client.RequestAsync(BuildCreateRequest(options, client));
var response = await client.RequestAsync(BuildCreateRequest(options, client), cancellationToken);
return FromJson(response.Content);
}
#endif
Expand All @@ -93,10 +93,11 @@ public static BulkContactsResource Create(
/// <returns> Task that resolves to A single instance of BulkContacts </returns>
public static async System.Threading.Tasks.Task<BulkContactsResource> CreateAsync(
List<object> items,
ITwilioRestClient client = null)
ITwilioRestClient client = null,
System.Threading.CancellationToken cancellationToken = default)
{
var options = new CreateBulkContactsOptions(items){ };
return await CreateAsync(options, client);
return await CreateAsync(options, client, cancellationToken);
}
#endif

Expand Down
Loading
Loading