Forum Discussion

Grant_Reid's avatar
Grant_Reid
Frequent Visitor
3 years ago
Solved

REST API Data Source refreshes in Desktop but not in Service

I’ve been tasked to roll out a solution provided by a third party. Everything works perfectly in Desktop and the expected number of records are returned. However when published to the Service I get t...
  • Grant_Reid's avatar
    Grant_Reid
    3 years ago

    Hi All

     

    Many thanks Imke for helping me resolve this. Working code below.

    let
        // Build the URL for the API call
        #"Header" = Binary.ToText(Text.ToBinary(#"User Email" & ":" & #"API Token")), 
        mainURL = "https://auvikapi.eu1.my.auvik.com/v1/inventory/device/info", 
        startQuery = [#"page[first]" = "50"], 
        /*  Description: Fetches one page of data
            url - URL to fetch data
          Output Parameter:
            ret - record consisting of two elements
            retData - data for current page
            retNext - URL for next page
      */
        getOnePage = (query) as record =>
            let
                // Get the specified URL and parse the JSON response
                devicePage = Json.Document(
                    Web.Contents(
                        mainURL, 
                        [
                            Query = query, 
                            Headers = [
                                #"Authorization" = "Basic " & #"Header", 
                                #"content-type"  = "application/json"
                            ]
                        ]
                    )
                ), 
                // Extract the data and links fields and return it
                deviceData = try devicePage[data] otherwise null, 
                next = try devicePage[links][next] otherwise null, 
                ret = [retData = deviceData, retNext = next]
            in
                ret, 
        // Fetch each page until there are no more pages by following the next link. Append subsequent pages.  
        deviceList = List.Generate(
            // Fetch the first page
            () => [ret = getOnePage(startQuery)], 
            // Stop when there is no more data
            each try [ret][retData] <> null otherwise false, 
            // Get the next page using the next link - numbers in the query parameters must be formatted as text, hence the gymnastics
            each [
                ret = getOnePage(
                    Record.FromTable(
                        Table.TransformColumnTypes(
                            Record.ToTable(Uri.Parts([ret][retNext])[Query]), 
                            {{"Value", type text}}
                        )
                    )
                )
            ], 
            // Return only the data
            each [ret][retData]
        ),
        Custom1 = deviceList
    in
        Custom1