9
9
using System . Threading . Tasks ;
10
10
using System . Web . Script . Serialization ;
11
11
using System . Web ;
12
+ using System . Threading ;
12
13
13
14
namespace SendGrid . CSharp . HTTP . Client
14
15
{
@@ -266,6 +267,7 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
266
267
267
268
if ( Enum . IsDefined ( typeof ( Methods ) , binder . Name . ToUpper ( ) ) )
268
269
{
270
+ CancellationToken cancellationToken = CancellationToken . None ;
269
271
string queryParams = null ;
270
272
string requestBody = null ;
271
273
int i = 0 ;
@@ -286,9 +288,13 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
286
288
{
287
289
AddRequestHeader ( ( Dictionary < string , string > ) obj ) ;
288
290
}
291
+ else if ( name == "cancellationToken" )
292
+ {
293
+ cancellationToken = ( CancellationToken ) obj ;
294
+ }
289
295
i ++ ;
290
296
}
291
- result = RequestAsync ( binder . Name . ToUpper ( ) , requestBody : requestBody , queryParams : queryParams ) . ConfigureAwait ( false ) ;
297
+ result = RequestAsync ( binder . Name . ToUpper ( ) , requestBody : requestBody , queryParams : queryParams , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
292
298
return true ;
293
299
}
294
300
else
@@ -304,22 +310,24 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
304
310
/// </summary>
305
311
/// <param name="client">Client object ready for communication with API</param>
306
312
/// <param name="request">The parameters for the API call</param>
313
+ /// <param name="cancellationToken">A token that allows cancellation of the http request</param>
307
314
/// <returns>Response object</returns>
308
- public async virtual Task < Response > MakeRequest ( HttpClient client , HttpRequestMessage request )
315
+ public async virtual Task < Response > MakeRequest ( HttpClient client , HttpRequestMessage request , CancellationToken cancellationToken = default ( CancellationToken ) )
309
316
{
310
317
311
- HttpResponseMessage response = await client . SendAsync ( request ) . ConfigureAwait ( false ) ;
318
+ HttpResponseMessage response = await client . SendAsync ( request , cancellationToken ) . ConfigureAwait ( false ) ;
312
319
return new Response ( response . StatusCode , response . Content , response . Headers ) ;
313
320
}
314
321
315
322
/// <summary>
316
323
/// Prepare for async call to the API server
317
324
/// </summary>
318
325
/// <param name="method">HTTP verb</param>
326
+ /// <param name="cancellationToken">A token that allows cancellation of the http request</param>
319
327
/// <param name="requestBody">JSON formatted string</param>
320
328
/// <param name="queryParams">JSON formatted queary paramaters</param>
321
329
/// <returns>Response object</returns>
322
- private async Task < Response > RequestAsync ( string method , String requestBody = null , String queryParams = null )
330
+ private async Task < Response > RequestAsync ( string method , String requestBody = null , String queryParams = null , CancellationToken cancellationToken = default ( CancellationToken ) )
323
331
{
324
332
using ( var client = new HttpClient ( ) )
325
333
{
@@ -367,9 +375,13 @@ private async Task<Response> RequestAsync(string method, String requestBody = nu
367
375
RequestUri = new Uri ( endpoint ) ,
368
376
Content = content
369
377
} ;
370
- return await MakeRequest ( client , request ) . ConfigureAwait ( false ) ;
378
+ return await MakeRequest ( client , request , cancellationToken ) . ConfigureAwait ( false ) ;
371
379
372
380
}
381
+ catch ( TaskCanceledException )
382
+ {
383
+ throw ;
384
+ }
373
385
catch ( Exception ex )
374
386
{
375
387
HttpResponseMessage response = new HttpResponseMessage ( ) ;
0 commit comments