Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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));
        }
Exemplo n.º 3
0
        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));
        }
Exemplo n.º 4
0
        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));
        }