Guest User

Untitled

a guest
Feb 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. public static List<Label> GetLabels()
  2. {
  3. /*
  4. * use linq's lazy loading feature to enumerate an unlimited number of page requests. so i created AggregateUntil() to
  5. * take in a Func() which will inform when to stop iterating - in this case, we want to stop after the entries count
  6. * exceeds the totalNumEntries value, which Google Adwords returns from the first query
  7. */
  8. var labels = Enumerable
  9. .Range(0, Int32.MaxValue) //range generates a list of integers, from 1 to practically infinity
  10. .AggregateUntil(
  11. new LabelPage()
  12. {
  13. entries = new Label[] { },
  14. totalNumEntries = 0,
  15. },
  16. (accumulatedLabelPage, pageIndex) =>
  17. {
  18. /*
  19. * also use select() and first() to mitigate rate limiting; setup 5 identical requests and while the
  20. * returned page is null (meaning there was a caught exception), keep retrying. after first() stops
  21. * the evaluation, or you've attempted up to the max number of times to fetch, there will be one
  22. * non-empty page result. if there is no result at all, Single() will throw an error, which is what we
  23. * want to happen
  24. */
  25. var thisLabelsPage = Enumerable
  26. .Range(0, pageMaxFetchAttempts)
  27. .Select(pageAttempt =>
  28. {
  29. // this is the exponential backoff
  30. Thread.Sleep(2500 * pageAttempt * pageAttempt);
  31. try
  32. {
  33. var labelsAwql =
  34. $"select LabelId,LabelName,LabelStatus limit {(pageIndex * pageSize)},{pageSize}";
  35. var attemptedlabelsPage = Config.SupplyLabelService.query(labelsAwql);
  36.  
  37. return attemptedlabelsPage;
  38. }
  39. catch (Exception e)
  40. {
  41. return new LabelPage();
  42. }
  43. })
  44. .First(page => page != null);
  45.  
  46. return new LabelPage()
  47. {
  48. entries = accumulatedLabelPage
  49. .entries
  50. .ToList()
  51. .Concat(thisLabelsPage.entries)
  52. .ToArray(),
  53. totalNumEntries = thisLabelsPage.totalNumEntries,
  54. totalNumEntriesSpecified = thisLabelsPage.totalNumEntriesSpecified,
  55. PageType = thisLabelsPage.PageType,
  56. };
  57. },
  58. (accumulatedLabelPage) =>
  59. {
  60. var shouldStop = accumulatedLabelPage.entries.Length >= accumulatedLabelPage.totalNumEntries;
  61. return shouldStop;
  62. })
  63. .entries
  64. .ToList();
  65.  
  66. return labels;
  67. }
Add Comment
Please, Sign In to add comment