twilio / twilio-csharp

Twilio C#/.NET Helper Library for .NET6+.
MIT License
663 stars 300 forks source link

[FeatureRequest] Add CancellationToken pass-through to async request methods #514

Open JosephIaquinto opened 4 years ago

JosephIaquinto commented 4 years ago

Issue Summary

I am currently building UiPath activities to wrap this SDK. UiPath is built on Windows Workflow Foundation. Workflow activities provide a cancellation token so that activities can be interrupted/ continued at a later time/ provide general async support. It would be nice if the async twilio activities exposed this parameter and passed it through to HttpClient so that requests can be cancelled. Is this something that is in the backlog or is being planned in the future?

Generally, .net libraries with async methods support this parameter with a default of CancellationToken.None as to not cause breaking changes in existing code. The .NET HttpClient supports this parameter, but the Twilio SDK does not provide it.

Code Snippet

In TwilioRestClient.cs

 public async Task<Response> RequestAsync(Request request, CancellationToken cancellationToken)
        {
            request.SetAuth(_username, _password);
            Response response;
            try
            {
                response = await HttpClient.MakeRequestAsync(request, cancellationToken);
            }
            catch (Exception clientException)
            {
                throw new ApiConnectionException(
                    "Connection Error: " + request.Method + request.ConstructUrl(),
                    clientException
                );
            }
            return ProcessResponse(response);
        }

In SystemNetHttpClient.cs

public override async Task<Response> MakeRequestAsync(Request request, CancellationToken cancellationToken)
        {
            var httpRequest = BuildHttpRequest(request);
            if (!Equals(request.Method, HttpMethod.Get))
            {
                httpRequest.Content = new FormUrlEncodedContent(request.PostParams);
            }

            this.LastRequest = request;
            this.LastResponse = null;

            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
            // 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));
            this.LastResponse = response;
            return response;
        }

Each async version of the request functions would have this optional parameter added (Excluded here due to the fact there are a large number of them).

childish-sambino commented 4 years ago

This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.

gagandeepp commented 3 years ago

Interested and willing to work on this

guidospadavecchia commented 1 year ago

Any updates on this?

CrustyApplesniffer commented 1 year ago

Any news ?

3baaady07 commented 4 months ago

I'm trying to pass a cancellation token to

Twilio.Rest.Verify.V2.Service.VerificationCheckResource.CreateAsync(... params)

but, of course, CreateAsync does not accept one. Not even sure if the server will handle one anyways.

I thought of making a pull request, however here's a snippet from the Issues and Bugs in the Contributing to twilio-csharp guidlines.

If you find a bug in the source code or a mistake in the documentation, you can help us by submitting an issue. If the file in which the error exists has this header:

This code was generated by
\ / _    _  _|   _  _
 | (_)\/(_)(_|\/| |(/_  v1.0.0
      /       /

or

 * This code was generated by
 * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
 *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
 *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
 *
 * Twilio - XXXX
 * This is the public Twilio REST API.
 *
 * NOTE: This class is auto generated by OpenAPI Generator.
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */ 

then it is a generated file and the change will need to be made by us, but submitting an issue will help us track it and keep you up-to-date. If the file isn't generated, you can help us out even more by submitting a Pull Request with a fix.

Methods that could accept a cancellation token are in such files, and therefore, contributers cannot help in this particular issue.