Ejemplo n.º 1
0
        public async Task <T> DownloadWithCancel <K, T>(string url, K postData, ApiKeyCombo apiKey, CancellationToken cancellationToken)
        {
            if (IsCancelled)
            {
                return(default(T));
            }

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

                    cancellationToken.ThrowIfCancellationRequested();
                }

                if (apiKey != null)
                {
                    request.Headers.Add("HashedKey", apiKey.HashedKey);
                }

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

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

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

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

                    // If non-200 status code, throws exception
                    response.EnsureSuccessStatusCode();

                    Stream responseStream = await response.Content.ReadAsStreamAsync();


                    cancellationToken.ThrowIfCancellationRequested();

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


                    return(readResponse <T>(responseStream));
                }
            }
        }
Ejemplo n.º 2
0
 public static async Task <T> Download <K, T>(string url, K postData, ApiKeyCombo apiKey, CancellationToken cancellationToken)
 {
     return(await new WebHelper().DownloadWithCancel <K, T>(url, postData, apiKey, cancellationToken));
 }
Ejemplo n.º 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)
 {
     return(await DownloadWithCancel <K, T>(url, postData, apiKey, CancellationToken.None));
 }
Ejemplo n.º 4
0
        private static StreamContent GeneratePostData <K>(HttpRequestMessage request, K postData, ApiKeyCombo apiKey)
        {
            Stream postStream = new MemoryStream();
            string hashedData = null;

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

                else
                {
                    serialize(postStream, postData);
                    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 = EncryptionHelper.Sha1(bytes);
                    hashedData = EncryptionHelper.Sha256(apiKey.ApiKey + hashedData);
                }
            }

            catch
            {
                postStream.Dispose();
                throw;
            }

            StreamContent content = new StreamContent(postStream);

            if (hashedData != null)
            {
                request.Headers.Add("HashedData", hashedData);
            }

            // If we serialized as JSON
            if (!(postData is Stream))
            {
                content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");
            }

            return(content);
        }