Created
March 7, 2018 20:55
-
-
Save vector623/6c7372bcad22c2a1c5e29c9bba4e5937 to your computer and use it in GitHub Desktop.
example usage of AggregateUntil() with Google's AdWords API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static List<CriterionBidLandscape> GetCriterionBidLandscapes(DataService dataService, string awql) | |
{ | |
var bidLandscapes = Enumerable | |
.Range(0, Int32.MaxValue) | |
.AggregateUntil( | |
new CriterionBidLandscapePage() | |
{ | |
entries = new CriterionBidLandscape[] { }, | |
totalNumEntries = 0, | |
}, | |
(accumulatedCriterionBidLandscapePage, pageIndex) => | |
{ | |
var awqlLimited = $"{awql} LIMIT {pageIndex * pageSize},{pageSize}"; | |
var theseKeywordsPage = Enumerable | |
.Range(0, pageMaxFetchAttempts) | |
.Select(pageAttempt => | |
{ | |
Thread.Sleep(2500 * pageAttempt * pageAttempt); | |
try | |
{ | |
var page = dataService.queryCriterionBidLandscape(awqlLimited); | |
return page; | |
} | |
catch (AdWordsApiException e) | |
{ | |
return null; | |
} | |
}) | |
.First(page => page != null); //TODO: catch the error that this could throw | |
return new CriterionBidLandscapePage() | |
{ | |
entries = accumulatedCriterionBidLandscapePage | |
.entries | |
.ToList() | |
.Concat(theseKeywordsPage.entries) | |
.ToArray(), | |
totalNumEntries = theseKeywordsPage.totalNumEntries, | |
totalNumEntriesSpecified = theseKeywordsPage.totalNumEntriesSpecified, | |
PageType = theseKeywordsPage.PageType, | |
}; | |
}, | |
(accumulatedKeywordPage) => | |
{ | |
var shouldStop = accumulatedKeywordPage.entries.Length >= accumulatedKeywordPage.totalNumEntries; | |
return shouldStop; | |
}) | |
.entries | |
.ToList(); | |
return bidLandscapes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment