Exemple #1
0
        private async Task <Listing> GetListing(string url, CancellationToken token, IProgress <float> progress, bool ignoreCache, Dictionary <string, string> body)
        {
            return(await Task.Run(async() =>
            {
                if (!ignoreCache && _cacheProvider != null)
                {
                    var cacheResult = await _cacheProvider.GetListing(url);
                    if (cacheResult != null)
                    {
                        return cacheResult;
                    }
                }

                if (body == null)
                {
                    //need someplace to put the redirect url
                    body = new Dictionary <string, string>();
                }
                var resultString = await _networkLayer.Get(url, token, progress, body);
                Listing resultListing = null;
                if (resultString.StartsWith("["))
                {
                    var listings = JsonConvert.DeserializeObject <Listing[]>(resultString);
                    resultListing = new Listing {
                        Data = new ListingData {
                            Children = new List <Thing>()
                        }
                    };
                    foreach (var combinableListing in listings)
                    {
                        resultListing.Data.Children.AddRange(combinableListing.Data.Children);
                        resultListing.Kind = combinableListing.Kind;
                        resultListing.Data.After = combinableListing.Data.After;
                        resultListing.Data.Before = combinableListing.Data.Before;
                    }
                }
                else if (resultString == "\"{}\"")
                {
                    return new Listing {
                        Kind = "Listing", Data = new ListingData {
                            Children = new List <Thing>()
                        }
                    };
                }
                else
                {
                    resultListing = JsonConvert.DeserializeObject <Listing>(resultString);
                }

                if (body.ContainsKey("redirected-url"))
                {
                    url = body["redirected-url"];
                    resultListing.RedirectedUrl = url;
                }

                var filteredResult = await _listingFilter.Filter(resultListing);
                if (_cacheProvider != null)
                {
                    await _cacheProvider.SetListing(url, filteredResult);
                }
                return filteredResult;
            }));
        }