private static TaskCompletionSource <TResponse> QuerystringRequestAsync(Task <byte[]> task, TaskCompletionSource <TResponse> taskCompletionSource) { if (task == null) { throw new ArgumentNullException(nameof(task)); } if (taskCompletionSource == null) { throw new ArgumentNullException(nameof(taskCompletionSource)); } if (task.IsCanceled) { taskCompletionSource.SetCanceled(); } else if (task.IsFaulted) { var exception = task.Exception == null ? new NullReferenceException("_task.Exception") : task.Exception.InnerException ?? task.Exception; taskCompletionSource.SetException(exception); } else { var deserialize = GenericEngine <TRequest, TResponse> .Deserialize(task.Result); taskCompletionSource.SetResult(deserialize); } return(taskCompletionSource); }
/// <summary> /// Query Google Async. /// </summary> /// <param name="request"></param> /// <param name="timeout"></param> /// <param name="token"></param> /// <returns>Task[TResponse]</returns> protected internal static Task <TResponse> QueryGoogleApiAsync(TRequest request, TimeSpan timeout, CancellationToken token = default(CancellationToken)) { if (request == null) { throw new ArgumentNullException(nameof(request)); } var uri = request.GetUri(); var webClient = new WebClientTimeout(timeout); var taskCompletionSource = new TaskCompletionSource <TResponse>(); if (request is IJsonRequest) { webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json"); var jsonSerializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; var jsonString = JsonConvert.SerializeObject(request, jsonSerializerSettings); webClient.UploadStringTaskAsync(uri, WebRequestMethods.Http.Post, jsonString) .ContinueWith(x => GenericEngine <TRequest, TResponse> .JsonRequestAsync(x, taskCompletionSource), TaskContinuationOptions.ExecuteSynchronously); return(taskCompletionSource.Task); } if (request is IQueryStringRequest) { webClient.DownloadDataTaskAsync(uri, timeout, token) .ContinueWith(x => GenericEngine <TRequest, TResponse> .QuerystringRequestAsync(x, taskCompletionSource), TaskContinuationOptions.ExecuteSynchronously); return(taskCompletionSource.Task); } throw new InvalidOperationException("Invalid Request. Request class missing Request interface implementation."); }
/// <summary> /// Query the Google Maps API using the provided request and timeout period. /// </summary> /// <param name="request">The request that will be sent.</param> /// <param name="timeout">A TimeSpan specifying the amount of time to wait for a response before aborting the request. /// The specify an infinite timeout, pass a TimeSpan with a TotalMillisecond value of Timeout.Infinite. /// When a request is aborted due to a timeout an AggregateException will be thrown with an InnerException of type TimeoutException.</param> /// <returns>The response that was received.</returns> /// <exception cref="ArgumentNullException">Thrown when a null value is passed to the request parameter.</exception> /// <exception cref="AuthenticationException">Thrown when the provided Google client ID or signing key are invalid.</exception> /// <exception cref="TimeoutException">Thrown when the operation has exceeded the allotted time.</exception> /// <exception cref="WebException">Thrown when an error occurred while downloading data.</exception> public virtual TResponse Query(TRequest request, TimeSpan timeout) { if (request == null) { throw new ArgumentNullException(nameof(request)); } return(GenericEngine <TRequest, TResponse> .QueryGoogleApi(request, timeout)); }
/// <summary> /// Asynchronously query the Google Maps API using the provided request. /// </summary> /// <param name="request">The request that will be sent.</param> /// <param name="timeout">A TimeSpan specifying the amount of time to wait for a response before aborting the request. /// The specify an infinite timeout, pass a TimeSpan with a TotalMillisecond value of Timeout.Infinite. /// When a request is aborted due to a timeout the returned task will transition to the Faulted state with a TimeoutException.</param> /// <param name="token">A cancellation token that can be used to cancel the pending asynchronous task.</param> /// <returns>A Task with the future value of the response.</returns> /// <exception cref="ArgumentNullException">Thrown when a null value is passed to the request parameter.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown when the value of timeout is neither a positive value or infinite.</exception> public virtual Task <TResponse> QueryAsync(TRequest request, TimeSpan timeout, CancellationToken token) { if (request == null) { throw new ArgumentNullException(nameof(request)); } return(GenericEngine <TRequest, TResponse> .QueryGoogleApiAsync(request, timeout, token)); }
private static TResponse QuerystringRequest(TRequest request, TimeSpan timeout) { if (request == null) { throw new ArgumentNullException(nameof(request)); } var uri = request.GetUri(); var webClient = new WebClientTimeout(timeout); var downloadData = webClient.DownloadData(uri); return(GenericEngine <TRequest, TResponse> .Deserialize(downloadData)); }
private static TResponse Deserialize(byte[] serializedObject) { if (serializedObject == null) { throw new ArgumentNullException(nameof(serializedObject)); } // TODO: Hack (PlacesPhotosResponse). if (typeof(TResponse) == typeof(PlacesPhotosResponse)) { return(GenericEngine <TRequest, TResponse> .Deserialize("{\"photo\":\"" + Convert.ToBase64String(serializedObject) + "\"}")); } var serializer = new DataContractJsonSerializer(typeof(TResponse)); var memoryStream = new MemoryStream(serializedObject, false); return((TResponse)serializer.ReadObject(memoryStream)); }
/// <summary> /// Query Google. /// </summary> /// <param name="request"></param> /// <param name="timeout"></param> /// <returns>TResponse</returns> protected internal static TResponse QueryGoogleApi(TRequest request, TimeSpan timeout) { if (request == null) { throw new ArgumentNullException(nameof(request)); } if (request is IJsonRequest) { return(GenericEngine <TRequest, TResponse> .JsonRequest(request, timeout)); } if (request is IQueryStringRequest) { return(GenericEngine <TRequest, TResponse> .QuerystringRequest(request, timeout)); } throw new InvalidOperationException("Invalid Request. Request class missing Request interface implementation."); }
private static TResponse JsonRequest(TRequest request, TimeSpan timeout) { if (request == null) { throw new ArgumentNullException(nameof(request)); } var webClient = new WebClientTimeout(timeout); webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json"); var uri = request.GetUri(); var jsonSerializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; var jsonString = JsonConvert.SerializeObject(request, jsonSerializerSettings); var uploadString = webClient.UploadString(uri, WebRequestMethods.Http.Post, jsonString); return(GenericEngine <TRequest, TResponse> .Deserialize(uploadString)); }