Esempio n. 1
0
        private async Task <T> PatchPostPutReadStream <T>(string url, T content, HttpMethod method)
        {
            using (var request = new HttpRequestMessage(method, url))
                using (var httpContent = CreateHttpStreamContent(content))
                {
                    request.Content = httpContent;

                    using (var response = await Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false))
                    {
                        var stream = await response.Content.ReadAsStreamAsync();

                        if (response.IsSuccessStatusCode)
                        {
                            return(StreamHandler.DeserializeJsonFromStream <T>(stream));
                        }

                        var resultContent = await response.Content.ReadAsStringAsync();

                        throw new ApiException
                              {
                                  StatusCode = (int)response.StatusCode,
                                  Content    = resultContent
                              };
                    }
                }
        }
Esempio n. 2
0
        public async Task <T> GetStreamAsync <T>(string url)
        {
            var cancelToken = new CancellationTokenSource();

            using (var request = new HttpRequestMessage(HttpMethod.Get, url))
                using (var response = await Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancelToken.Token))
                {
                    var stream = await response.Content.ReadAsStreamAsync();

                    if (response.IsSuccessStatusCode)
                    {
                        return(StreamHandler.DeserializeJsonFromStream <T>(stream));
                    }

                    var content = await StreamHandler.StreamToStringAsync(stream);

                    throw new ApiException
                          {
                              StatusCode = (int)response.StatusCode,
                              Content    = content
                          };
                }
        }