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
Changes from 1 commit
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
Next Next commit
Added CancellationToken Pass through to Client
joewashear007 committed Jan 13, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit f7322681ab0db606734daf22a985f31cd58cbc26
2 changes: 1 addition & 1 deletion src/Twilio/Clients/ITwilioRestClient.cs
Original file line number Diff line number Diff line change
@@ -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, CancellationToken cancellationToken = default);
#endif
}
}
4 changes: 2 additions & 2 deletions src/Twilio/Clients/TwilioRestClient.cs
Original file line number Diff line number Diff line change
@@ -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, CancellationToken cancellationToken = default)
{
if(_username != null && _password != null){
request.SetAuth(_username, _password);
@@ -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)
{
2 changes: 1 addition & 1 deletion src/Twilio/Http/HttpClient.cs
Original file line number Diff line number Diff line change
@@ -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, CancellationToken cancellationToken = default);
#endif

/// <summary>
8 changes: 4 additions & 4 deletions src/Twilio/Http/SystemNetHttpClient.cs
Original file line number Diff line number Diff line change
@@ -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, CancellationToken cancellationToken = default)
{
var httpRequest = BuildHttpRequest(request);
if (!Equals(request.Method, HttpMethod.Get))
@@ -76,13 +76,13 @@ public override async Task<Response> MakeRequestAsync(Request request)
this.LastRequest = request;
this.LastResponse = null;

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

// Create and return a new Response. Keep a reference to the last
// response for debugging, but don't return it as it may be shared
// among threads.
var response = new Response(httpResponse.StatusCode, await reader.ReadToEndAsync().ConfigureAwait(false), httpResponse.Headers);
var response = new Response(httpResponse.StatusCode, await reader.ReadToEndAsync(cancellationToken).ConfigureAwait(false), httpResponse.Headers);
this.LastResponse = response;
return response;
}