using System; using System.Net.Http; using System.Threading; using System.Threading.Tasks; namespace RestEase.Implementation { /// /// HttpClientHandler which uses a delegate to modify /// public class ModifyingClientHttpHandler : HttpClientHandler { private readonly RequestModifier requestModifier; /// /// Initialises a new instance of the class, /// using the given delegate to modify requests /// /// Delegate to use to modify requests public ModifyingClientHttpHandler(RequestModifier requestModifier) { this.requestModifier = requestModifier ?? throw new ArgumentNullException(nameof(requestModifier)); } /// /// Creates an instance of System.Net.Http.HttpResponseMessage based on the information /// provided in the System.Net.Http.HttpRequestMessage as an operation that will /// not block. /// /// The HTTP request message /// A cancellation token to cancel the operation /// Returns System.Threading.Tasks.Task{TResult}.The task object representing /// the asynchronous operation protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { await this.requestModifier(request, cancellationToken).ConfigureAwait(false); return await base.SendAsync(request, cancellationToken).ConfigureAwait(false); } } }