Skip to content

Instantly share code, notes, and snippets.

@vector623
Created March 7, 2018 20:55
Show Gist options
  • Save vector623/6c7372bcad22c2a1c5e29c9bba4e5937 to your computer and use it in GitHub Desktop.
Save vector623/6c7372bcad22c2a1c5e29c9bba4e5937 to your computer and use it in GitHub Desktop.
example usage of AggregateUntil() with Google's AdWords API
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