gpt4 book ai didi

powershell - Invoke-WebRequest Canvas LMS API 分页

转载 作者:行者123 更新时间:2023-12-02 23:25:37 28 4
gpt4 key购买 nike

我似乎找不到关于这个主题的任何例子,我想知道如何去做。 任何人都可以给我举个例子或给我一个关于如何在 powershell 中使用 Invoke web-request 进行分页的链接吗?我面临的挑战是我正在对只返回的服务器进行 API 调用一次 100 行。为了获得更多行,我将不得不再次调用服务器。我不知道该怎么做。如果有帮助,这里是 Canvas LMS 提供的链接和我目前拥有的代码。

Pagination :

Pagination

Requests that return multiple items will be paginated to 10 items by default. You can set a custom per-page amount with the ?per_page parameter. There is an unspecified limit to how big you can set per_page to, so be sure to always check for the Link header.

To retrieve additional pages, the returned Link headers should be used. These links should be treated as opaque. They will be absolute urls that include all parameters necessary to retrieve the desired current, next, previous, first, or last page. The one exception is that if an access_token parameter is sent for authentication, it will not be included in the returned links, and must be re-appended.

Pagination information is provided in the Link header:

Link:
<https://<canvas>/api/v1/courses/:id/discussion_topics.json?opaqueA>; rel="current",
<https://<canvas>/api/v1/courses/:id/discussion_topics.json?opaqueB>;> rel="next",
<https://<canvas>/api/v1/courses/:id/discussion_topics.json?opaqueC>;> rel="first",
<https://<canvas>/api/v1/courses/:id/discussion_topics.json?opaqueD>;> rel="last"

The possible rel values are:

current - link to the current page of results. next - link to the next page of results. prev - link to the previous page of results. first - link to the first page of results. last - link to the last page of results. These will only be included if they are relevant. For example, the first page of results will not contain a rel="prev" link. rel="last" may also be excluded if the total count is too expensive to compute on each request.

开始的产品

$curlly=""
$url_main="https://[instance].instructure.com/api/v1/accounts/1/courses?per_page=1"
$security_token="imhungry"
$header = @{"Authorization"="Bearer "+ $security_token; "rel"="last"}
$curlly=Invoke-WebRequest -Headers $header -Method Get -Uri $url_main
$curlly = ConvertFrom-Json $curlly.Content
foreach($course in $curlly)
{
$course.name
}
$curlly.Count

最终产品

 ##This is an example on how to use pagination in powershell
$url_main="https://[instance].instructure.com/api/v1/accounts/1/courses?per_page=100"
$security_token="boyimhungry"
$header = @{"Authorization"="Bearer "+ $security_token}
$purlly=Invoke-WebRequest -Headers $header -Method Get -Uri $url_main
$curlly = ConvertFrom-Json $purlly.Content
$url_main = $purlly.Headers.Link.Split(",")[1].Replace("<","").Replace(">","") ## you can get away with just doing one replace("<","") but it looks neater this way
while( !$url_main.Contains("prev"))
{
$purlly=Invoke-WebRequest -Headers $header -Method Get -Uri $url_main
$curlly += ConvertFrom-Json $purlly.Content
$url_main = $purlly.Headers.Link.Split(",")[1].Replace("<","").Replace(">","")
cls
$curlly.Count
$url_main
}
foreach($course in $curlly)
{
$course.name
}
$curlly.Count

最佳答案

我知道您已经接受了答案,但我想我会给出我的代码示例以防万一有人需要。此示例获取我们所有 Canvas 用户的列表。不是一个糟糕的过程——大部分工作仅通过 4 行 do..while 循环完成。

$token = "YOUR_ACCESS_TOKEN"
$headers = @{"Authorization"="Bearer "+$token}
$allCanvasUsers = New-Object System.Collections.ArrayList @()
$pageNumber = 1

Function RequestPageOfUsers($page) {
$script:resultsPage = Invoke-WebRequest -Method GET -Headers $headers -Uri "https://$domain/api/v1/accounts/self/users?per_page=100&search_term=P00&page=$page"
$usersPage = ConvertFrom-Json $resultsPage.Content

foreach ($user in $usersPage) {
$script:allCanvasUsers.Add($user)
}
}

do {
RequestPageOfUsers $pageNumber
$pageNumber++
} while ($resultsPage.Headers.Link.Contains('rel="next"'))

关于powershell - Invoke-WebRequest Canvas LMS API 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37732370/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com