Overfloweth
Overfloweth

Reputation: 41

Accessing a Record from API Source with Power Query (Custom Connector)

This is a follow up to my issue at API Pagination in JSON Body (not Header) - How to Access with Power Query in Custom Connector?

I have built a custom connector for an API that sends its pagination data back in the body of the response, rather than the header. The way to use GetNextLink using header is documented in the GitHub custom connector example:

https://github.com/microsoft/DataConnectors/blob/master/samples/Github/github.pq

GetNextLink = (response, optional request) =>
    let
        // extract the "Link" header if it exists
        link = Value.Metadata(response)[Headers][#"Link"]?,
        links = Text.Split(link, ","),
        splitLinks = List.Transform(links, each Text.Split(Text.Trim(_), ";")),
        next = List.Select(splitLinks, each Text.Trim(_{1}) = "rel=""next"""),
        first = List.First(next),
        removedBrackets = Text.Range(first{0}, 1, Text.Length(first{0}) - 2)
    in
        try removedBrackets otherwise null;

Getting to the pagination data from the body of the response should be easier than the header, I've learned. But I am stumped. In the images, you can see the record I am trying to access that comes back through Web.Contents()

This is my code for the GetNextLink function. I am trying to get the "next" record above.

GetNextLink = (response) =>
// response is data already run through Web.Contents()

let
   Source = Lines.FromBinary(response),
   nextPage = Record.Field(Source[paging], "next")
in 
   try nextPage otherwise null;

I get the error back "We cannot convert a value of type Record to type Text". I feel like I am not getting the 'Source" correctly? I could certainly use help as I am a PowerQuery newbie!

Thanks!

Upvotes: 0

Views: 653

Answers (1)

horseyride
horseyride

Reputation: 21343

GetNextLink = (response) =>
// response is data already run through Web.Contents()
let Source = Lines.FromBinary(response),
nextpage = try Source[paging][next] otherwise null
in nextpage

or

GetNextLink = (response) =>
// response is data already run through Web.Contents()
let  Source = Lines.FromBinary(response)
nextpage = try Record.Field(Source[paging],"next") otherwise null
in nextpage

enter image description here

Upvotes: 0

Related Questions