示例#1
0
        private async Task <T> GetResponseAsync <T>(Uri uri, Func <string, string> rawDataCallback)
        {
            string data = await GetRawResponseAsync(uri);


            if (data == null)
            {
                throw new InvalidDataException("The query return no data");
            }

            if (data.StartsWith("{\"error\":", StringComparison.OrdinalIgnoreCase))
            {
                ErrorResult error = JsonConvert.DeserializeObject <ErrorResult>(data);
                throw ErrorCodeException.CreateFromErrorCode(error);
            }

            if (rawDataCallback != null)
            {
                data = rawDataCallback(data);
            }

            try
            {
                T result = JsonConvert.DeserializeObject <T>(data);
                return(result);
            }
            catch (Exception ex)
            {
                throw new InvalidDataException(data, ex);
            }
        }
示例#2
0
        public Stream DownloadTorrent(int id)
        {
            EnsuresInitToken();

            string uri = string.Format(System.Globalization.CultureInfo.InvariantCulture, "/torrents/download/{0}", id);

            using (var handler = new HttpClientHandler())
            {
                if (handler.SupportsAutomaticDecompression)
                {
                    handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                }
                using (var client = new HttpClient(handler)
                {
                    BaseAddress = new Uri(BaseAddress)
                })
                    using (HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, uri))
                    {
                        requestMessage.Headers.TryAddWithoutValidation("Authorization", _token);

                        using (var response = client.SendAsync(requestMessage).Result)
                        {
                            MemoryStream ms = null;
                            try
                            {
                                ms = new MemoryStream();

                                using (var msLocal = new MemoryStream())
                                {
                                    response.Content.CopyToAsync(msLocal).Wait();
                                    msLocal.Position = 0;
                                    StreamReader sr = new StreamReader(msLocal);

                                    string data = sr.ReadToEnd();
                                    if (data.StartsWith("{\"error\":", StringComparison.OrdinalIgnoreCase))
                                    {
                                        ErrorResult error = JsonConvert.DeserializeObject <ErrorResult>(data);
                                        throw ErrorCodeException.CreateFromErrorCode(error);
                                    }
                                    msLocal.Position = 0;
                                    msLocal.CopyTo(ms);
                                }

                                ms.Position = 0;
                                return(ms);
                            }
                            catch
                            {
                                ms?.Dispose();
                                throw;
                            }
                        }
                    }
            }
        }