itsnotaboutthecell

Web API Loop

Nov 13th, 2018
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. // Web API Loop
  2.  
  3. (FinalUrl as text) = >
  4. let
  5. GetTotalPages = () =>
  6. let
  7. Source = Xml.Tables(Web.Contents(FinalUrl & ".xml")),
  8. #"Attribute:per_page" = Number.From(Source{0}[#"Attribute:per_page"]),
  9. #"Attribute:per_entries" = Number.From(Source{0}[#"Attribute:total_entries"]),
  10. Count = Number.RoundUp(#"Attribute:per_entries" / #"Attribute:per_page")
  11. in Count,
  12.  
  13. // Pagination of API Results
  14. GetJson = (Url as text, Page as number) =>
  15. let
  16. RawData = Web.Contents(FinalUrl & ".json",
  17. [RelativePath="?page=" & Text.From(Page)]),
  18. Json = Json.Document(Binary.Buffer(RawData))
  19. in Json,
  20.  
  21. GetPage = (Index) =>
  22. let
  23. Json = GetJson(FinalUrl, Index)
  24. in Json,
  25.  
  26. Page_Count = GetTotalPages(),
  27. PageIndices = {1 .. Page_Count},
  28. Pages = List.Transform(PageIndices, each GetPage(_)),
  29. Records = List.Union(Pages),
  30. #"Converted to Table" = Table.FromList(Records, Splitter.SplitByNothing(), null, null, ExtraValues.Error)
  31. in
  32. #"Converted to Table"
Advertisement
Add Comment
Please, Sign In to add comment