JSON主体(非标题)中的API分页 - 如何使用自定义连接器中的电源查询访问?

发布于 2025-02-04 06:07:26 字数 2801 浏览 3 评论 0原文

我已经建立了一个客户连接器,可以通过OAuth2连接到Vimeo API。一切都很好,但是看来我需要提出一个解决分页的解决方案,因为我只能在每个页面上拿回25个项目。

在此处查看有关如何使用表的文档。

我 /trippin/5-pagging/readme#tablegenerateBypage“ rel =” nofollow noreferrer“> https://learn.microsoft.com/en-us/power-popery/power-query/samples/trippin/5-paging/5-paging/readme.trippin/readme#tablegenerateBenerateBypage

以及示例中的实现github自定义连接器

https://github.com/microsoft/dataconnectors/blob/master/samples/github/github/github.pq

示例的功能示例示例

Github.Contents = (url as text) =>
    let
        content = Web.Contents(url),
        link = GetNextLink(content),
        json = Json.Document(content),
        table = Table.FromList(json, Splitter.SplitByNothing())
    in
        table meta [Next=link];

Github.PagedTable = (url as text) => Table.GenerateByPage((previous) =>
    let
        // If we have a previous page, get its Next link from metadata on the page.
        next = if (previous <> null) then Value.Metadata(previous)[Next] else null,
        // If we have a next link, use it, otherwise use the original URL that was passed in.
        urlToUse = if (next <> null) then next else url,
        // If we have a previous page, but don't have a next link, then we're done paging.
        // Otherwise retrieve the next page.
        current = if (previous <> null and next = null) then null else Github.Contents(urlToUse),
        // If we got data back from the current page, get the link for the next page
        link = if (current <> null) then Value.Metadata(current)[Next] else null
    in
        current meta [Next=link]);


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;
 

该 Vimeo API正在通过JSON主体响应而不是在标题内进行,如文档和示例所假定。电源查询/M中是否有简单的方法或辅助功能可以使我能够查看JSON响应的主体,抓住分页JSON对象(如下),然后从那里构建我的代码?

这是关于JSON主体中Vimeo API的分页的回归:

    "total": 1012,
    "page": 1,
    "per_page": 25,
    "paging": {
        "next": "/users/{our-user-id}/videos?page=2",
        "previous": null,
        "first": "/users/{our-user-id}/videos?page=1",
        "last": "/users/{our-user-id}/videos?page=41"
    },
 

非常感谢您的任何帮助 - 非常感谢!

最好,

-josh

I have built a customer connector to connect to the Vimeo API via OAuth2. Everything is working well, but it appears I need to come up with a solution to deal with pagination, as I am only getting back 25 items on each page.

I see the documentation on how to use Table.GenerateByPage and getNextPage here:

https://learn.microsoft.com/en-us/power-query/samples/trippin/5-paging/readme#tablegeneratebypage

As well as the implementation within the example GitHub custom connector

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

A sample of functions from that example:

Github.Contents = (url as text) =>
    let
        content = Web.Contents(url),
        link = GetNextLink(content),
        json = Json.Document(content),
        table = Table.FromList(json, Splitter.SplitByNothing())
    in
        table meta [Next=link];

Github.PagedTable = (url as text) => Table.GenerateByPage((previous) =>
    let
        // If we have a previous page, get its Next link from metadata on the page.
        next = if (previous <> null) then Value.Metadata(previous)[Next] else null,
        // If we have a next link, use it, otherwise use the original URL that was passed in.
        urlToUse = if (next <> null) then next else url,
        // If we have a previous page, but don't have a next link, then we're done paging.
        // Otherwise retrieve the next page.
        current = if (previous <> null and next = null) then null else Github.Contents(urlToUse),
        // If we got data back from the current page, get the link for the next page
        link = if (current <> null) then Value.Metadata(current)[Next] else null
    in
        current meta [Next=link]);


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;
 

However, my issue is that the metadata on pagination that returns from the Vimeo API is coming through the JSON body response instead of within the headers, as is assumed in the documentation and examples. Is there an easy way or helper function within Power Query/M that would allow me to look into the body of the JSON response, grab the pagination JSON objects (as below), and built out my code from there?

Here is what comes back regarding pagination from Vimeo's API within the JSON body:

    "total": 1012,
    "page": 1,
    "per_page": 25,
    "paging": {
        "next": "/users/{our-user-id}/videos?page=2",
        "previous": null,
        "first": "/users/{our-user-id}/videos?page=1",
        "last": "/users/{our-user-id}/videos?page=41"
    },
 

Many thanks for any help - it is very much appreciated!

Best,

-Josh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2025-02-11 06:07:26

从该信息中很难将一些内容放在一起,但是看看这是否有帮助

GetNextLink = (response) =>
// response is data already run through Web.Contents()
// looks for a row that contains     "first":  
// x would evaluate to be            "first": "/users/{our-user-id}/videos?page=1",
// y would parse x to get            /users/{our-user-id}/videos?page=1
Source = Lines.FromBinary(response),
x = List.RemoveNulls(List.Transform(List.Positions(Source), each if Text.Contains(Source{_},"""first"":") then  Source{_} else null)){0},
y=Text.BetweenDelimiters( x,": ""","""")
in y

Its hard to put something together from just that info, but see if this helps

GetNextLink = (response) =>
// response is data already run through Web.Contents()
// looks for a row that contains     "first":  
// x would evaluate to be            "first": "/users/{our-user-id}/videos?page=1",
// y would parse x to get            /users/{our-user-id}/videos?page=1
Source = Lines.FromBinary(response),
x = List.RemoveNulls(List.Transform(List.Positions(Source), each if Text.Contains(Source{_},"""first"":") then  Source{_} else null)){0},
y=Text.BetweenDelimiters( x,": ""","""")
in y
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文