Beispiel #1
0
        public static async Task <T> Download <K, T>(string message, string url, K postData, ApiKeyCombo apiKey)
        {
            LoadingPopup popup = new LoadingPopup()
            {
                Text = message
            };

            popup.Show();


            try
            {
                return(await ToolsPortable.WebHelper.Download <K, T>(url, postData, apiKey));
            }

            finally
            {
                popup.Close();
            }
        }
Beispiel #2
0
        public async Task <T> DownloadWithCancel <K, T>(string url, K postData, ApiKeyCombo apiKey, Serializer serializer, CancellationToken cancellationToken)
        {
            if (IsCancelled)
            {
                return(default(T));
            }

            using (HttpRequestMessage request = new HttpRequestMessage(
                       method: postData != null ? HttpMethod.Post : HttpMethod.Get,
                       uri: new Uri(url)))
            {
                if (postData != null)
                {
                    request.Content = GeneratePostData(request, postData, apiKey, serializer);

                    cancellationToken.ThrowIfCancellationRequested();
                }

                else
                {
                    request.Content = new HttpFormUrlEncodedContent(new KeyValuePair <string, string> [0]);
                }

                if (apiKey != null)
                {
                    request.Headers["HashedKey"] = apiKey.HashedKey;
                }

                //if we'll be deserializing data, set accept type
                if (typeof(K) != typeof(Stream) && typeof(K) != typeof(string))
                {
                    request.Headers["Accept"] = "application/json";
                }

                if (IsCancelled)
                {
                    return(default(T));
                }


                using (HttpResponseMessage response = await _client.SendRequestAsync(request))
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    if (IsCancelled)
                    {
                        return(default(T));
                    }

                    IInputStream responseStream = await response.Content.ReadAsInputStreamAsync();


                    cancellationToken.ThrowIfCancellationRequested();

                    if (IsCancelled)
                    {
                        return(default(T));
                    }


                    return(readResponse <T>(responseStream.AsStreamForRead()));
                }
            }
        }
Beispiel #3
0
 /// <summary>
 /// Supports streams and strings.
 /// </summary>
 /// <typeparam name="K"></typeparam>
 /// <typeparam name="T"></typeparam>
 /// <param name="url"></param>
 /// <param name="postData"></param>
 /// <param name="apiKey"></param>
 /// <returns></returns>
 public async Task <T> DownloadWithCancel <K, T>(string url, K postData, ApiKeyCombo apiKey, Serializer serializer = Serializer.DataContractJson)
 {
     return(await DownloadWithCancel <K, T>(url, postData, apiKey, serializer, CancellationToken.None));
 }
Beispiel #4
0
 public static async Task <T> Download <K, T>(string url, K postData, ApiKeyCombo apiKey, Serializer serializer, CancellationToken cancellationToken)
 {
     return(await new WebHelper().DownloadWithCancel <K, T>(url, postData, apiKey, serializer, cancellationToken));
 }
Beispiel #5
0
 public static async Task <T> Download <K, T>(string url, K postData, ApiKeyCombo apiKey, CancellationToken cancellationToken)
 {
     return(await Download <K, T>(url, postData, apiKey, Serializer.DataContractJson, cancellationToken));
 }
Beispiel #6
0
        private static HttpStreamContent GeneratePostData <K>(HttpRequestMessage request, K postData, ApiKeyCombo apiKey, Serializer serializer)
        {
            Stream postStream = new MemoryStream();
            string hashedData = null;

            try
            {
                if (postData is Stream)
                {
                    (postData as Stream).CopyTo(postStream);
                    postStream.Position = 0;
                }

                else
                {
                    serialize(postStream, postData, serializer);
                    postStream.Position = 0;
                }


                if (apiKey != null)
                {
                    // Turn it into bytes
                    byte[] bytes = new byte[postStream.Length];
                    postStream.Read(bytes, 0, bytes.Length);
                    postStream.Position = 0;

                    //hash the bytes, then hash ApiKey + Bytes
                    hashedData = EncryptionWin.Sha1(bytes);
                    hashedData = EncryptionWin.Sha256(apiKey.ApiKey + hashedData);
                }
            }

            catch
            {
                postStream.Dispose();
                throw;
            }

            HttpStreamContent content = new HttpStreamContent(postStream.AsInputStream());

            if (hashedData != null)
            {
                request.Headers["HashedData"] = hashedData;
            }
            //content.Headers["HashedData"] = hashedData;

            // If we serialized as JSON
            if (!(postData is Stream))
            {
                content.Headers.ContentType = new Windows.Web.Http.Headers.HttpMediaTypeHeaderValue("application/json");
            }

            return(content);
        }