Skip to content

Instantly share code, notes, and snippets.

@jmorrice
Last active August 23, 2022 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmorrice/f7e4c08e9b5d73f8f3523621cf036ff5 to your computer and use it in GitHub Desktop.
Save jmorrice/f7e4c08e9b5d73f8f3523621cf036ff5 to your computer and use it in GitHub Desktop.
Power Query script to load data from the Perdoo API
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