public async Task <Tuple <string, Listing> > Search(string query, int?limit, bool reddits, string restrictedToSubreddit) { var maxLimit = _userState.IsGold ? 1500 : 100; var guardedLimit = Math.Min(maxLimit, limit ?? maxLimit); string targetUri = null; string afterUri = null; if (reddits) { targetUri = string.Format("http://www.reddit.com/subreddits/search.json?limit={0}&q={1}", guardedLimit, query); afterUri = string.Format("/subreddits/search.json?q={0}", query); } else if (string.IsNullOrWhiteSpace(restrictedToSubreddit)) { targetUri = string.Format("http://www.reddit.com/search.json?limit={0}&q={1}", guardedLimit, query); afterUri = string.Format("/search.json&q={0}", query); } else { targetUri = string.Format("http://www.reddit.com/r/{2}/search.json?limit={0}&q={1}&restrict_sr=on", guardedLimit, query, restrictedToSubreddit); afterUri = string.Format("/subreddits/r/{1}/search.json?q={0}&restrict_sr=on", query, restrictedToSubreddit); } await ThrottleRequests(); await EnsureRedditCookie(); var listing = await _httpClient.GetStringAsync(targetUri); var newListing = JsonConvert.DeserializeObject <Listing>(listing); return(Tuple.Create(afterUri, await _listingFilter.Filter(newListing))); }
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; })); }