protected internal static TResponse QueryGoogleAPI(TRequest request, TimeSpan timeout) { if (request == null) { throw new ArgumentNullException("request"); } try { var uri = request.GetUri(); if (OnUriCreated != null) { uri = OnUriCreated(uri); } var data = new WebClientEx(timeout).DownloadData(uri); OnRawResponseRecivied?.Invoke(data); return(Deserialize(data)); } catch (WebException ex) { if (IndicatesAuthenticationFailed(ex)) { throw new AuthenticationException(AuthenticationFailedMessage, ex); } if (ex.Status == WebExceptionStatus.Timeout) { throw new TimeoutException(string.Format("The request has exceeded the timeout limit of {0} and has been aborted.", timeout)); } throw; } }
protected internal static async Task <TResponse> QueryGoogleAPI(TRequest request, TimeSpan timeout) { if (request == null) { throw new ArgumentNullException("request"); } var uri = request.GetUri(); if (OnUriCreated != null) { uri = OnUriCreated(uri); } var data = await new HttpClient().DownloadDataTaskAsync(uri, timeout); OnRawResponseRecivied?.Invoke(data); return(Deserialize(data)); }
private static void DownloadDataComplete(Task <byte[]> task, TaskCompletionSource <TResponse> completionSource) { if (task.IsCanceled) { completionSource.SetCanceled(); } else if (task.IsFaulted) { if (IndicatesAuthenticationFailed(task.Exception.InnerException)) { completionSource.SetException(new AuthenticationException(AuthenticationFailedMessage, task.Exception.InnerException)); } else { completionSource.SetException(task.Exception.InnerException); } } else { OnRawResponseRecivied?.Invoke(task.Result); completionSource.SetResult(Deserialize(task.Result)); } }