Last active
August 23, 2022 14:35
-
-
Save jmorrice/f7e4c08e9b5d73f8f3523621cf036ff5 to your computer and use it in GitHub Desktop.
Power Query script to load data from the Perdoo 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
let | |
// Enter your API token here from https://web.perdoo.com/settings/user/api-tokens | |
token = "sampleApiToken", | |
// Fetch data | |
vUrl = "https://api-eu.perdoo.com/graphql/", | |
vHeaders =[ | |
#"Method"="POST", | |
#"Content-Type"="application/json", | |
#"Authorization"=Text.Combine({"Bearer ", token}) | |
], | |
acc = {}, | |
GetRecords = (Page as text, accumulator) => | |
let | |
queryText = Text.Format("{""query"": ""query {objectives(first: 50, after: \""#[page]\"") { pageInfo { endCursor hasNextPage } edges {node {name status progress timeframe {name} company {name} groups {edges {node {name}}} results {edges {node {name type normalizedValue status}}}}}}}""}", [page = Page]), | |
Query = Text.ToBinary(queryText), | |
RawData = Web.Contents(vUrl, [Headers=vHeaders, Content=Query]), | |
FormatAsJson = Json.Document(RawData), | |
data = FormatAsJson[data], | |
goals = data[objectives], | |
edges = goals[edges], | |
hasNextPage = goals[pageInfo][hasNextPage], | |
nextPage = goals[pageInfo][endCursor], | |
// Add data to our Table variable which will accumulate the results | |
accumulatedEdges = List.Combine({accumulator, edges}), | |
Check = if hasNextPage = true then @GetRecords(nextPage, accumulatedEdges) else accumulatedEdges | |
in Check, | |
finalRecords = GetRecords("null", acc), | |
AsTable = Table.FromList(finalRecords, Splitter.SplitByNothing(), null, null, ExtraValues.Error) | |
in | |
AsTable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment