diff --git a/Piplapis/Search/SearchAPIRequest.cs b/Piplapis/Search/SearchAPIRequest.cs index 4465eef..b5c6229 100644 --- a/Piplapis/Search/SearchAPIRequest.cs +++ b/Piplapis/Search/SearchAPIRequest.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Net; using Newtonsoft.Json; @@ -16,7 +16,9 @@ namespace Pipl.APIs.Search { - /** + using System.Threading; + + /** * A request to Pipl's Search API. *

* Sending the request and getting the response is very simple and can be done @@ -326,20 +328,22 @@ public SearchAPIResponse Send(bool strictValidation = true) * Send the request and return the response or raise SearchAPIError. *

* + * @param cancellationToken A CancellationToken to be used to cancel the operation. * @param strictValidation A bool argument that's passed to the * validateQueryParams method. * @return A task that runs the request asyncronously * @throws ArgumentException Raises ArgumentException (raised from validateQueryParams) * @throws IOException IOException */ - public Task SendAsync(bool strictValidation = true) + public Task SendAsync(CancellationToken cancellationToken, bool strictValidation = true) { - ValidateQueryParams(strictValidation); + ValidateQueryParams(strictValidation); TaskCompletionSource taskCompletionSource = new TaskCompletionSource(); using (WebClient client = new WebClient()) { - client.Headers.Add("User-Agent", ClientUserAgent); + cancellationToken.Register(client.CancelAsync); + client.Headers.Add("User-Agent", ClientUserAgent); Uri uri = new Uri(Url); client.UploadValuesCompleted += (s, e) => { @@ -350,6 +354,24 @@ public Task SendAsync(bool strictValidation = true) return taskCompletionSource.Task; } + + /** + * Overload of SendAsync() to support older code without CancellationToken. + *

+ * + * @param strictValidation A bool argument that's passed to the + * validateQueryParams method. + * @return A task that runs the request asyncronously + * @throws ArgumentException Raises ArgumentException (raised from validateQueryParams) + * @throws IOException IOException + */ + public Task SendAsync(bool strictValidation = true) + { + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + return SendAsync(cancellationTokenSource.Token, strictValidation); + } + } private void _searchUploadValuesCompletedEventHandler(WebClient client, UploadValuesCompletedEventArgs e, TaskCompletionSource taskCompletionSource) {