Skip to content

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

Open
@JosephIaquinto

Description

@JosephIaquinto

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).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions