cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
i_oliveira
Community Participant

"rel" pagination header broken when listing courses?

Jump to solution

When requesting a list of courses from the API, the "Link" header comes incomplete for some reason. (URL of my instance changed to [my institution] in the examples)

Interesting that it only breaks when requesting a list of courses, but not when requesting a list of something else (Submissions in the example below).

Having the "last" link is important if you want to make parallel requests (What I do is to request the first page, get the number of pages and make parallel requests for pages 2 to N).

Request URL: https://canvas.[my_institution].com/api/v1/accounts/7/courses?per_page=10&page=1

Link header:

<https://canvas.[my_institution].com/api/v1/accounts/7/courses?page=1&per_page=10>; rel="current",
<https://canvas.[my_institution].com/api/v1/accounts/7/courses?page=2&per_page=10>; rel="next",
<https://canvas.[my_institution].com/api/v1/accounts/7/courses?page=1&per_page=10>; rel="first"

 

Request URL: https://canvas.[my_institution].com/api/v1/courses/6115/assignments/

Link header:

<https://canvas.[my_institution].com/api/v1/courses/6115/assignments?page=1&per_page=10>; rel="current",
<https://canvas.[my_institution].com/api/v1/courses/6115/assignments?page=2&per_page=10>; rel="next",
<https://canvas.[my_institution].com/api/v1/courses/6115/assignments?page=1&per_page=10>; rel="first",
<https://canvas.[my_institution].com/api/v1/courses/6115/assignments?page=2&per_page=10>; rel="last"

 

Has anyone ran into the same problem? This looks like a bug on the API...

Labels (3)
Tags (3)
0 Kudos
1 Solution

Accepted Solutions
matthew_buckett
Community Participant

Hiya,

I believe this is expected behaviour as outlined in: https://canvas.instructure.com/doc/api/file.pagination.html

When it's difficult/expensive to calculate the total number of items that exist Canvas doesn't give a last page. My guess is that this is done when they are filtering the items outside the DB and so would have to load every single item from the DB to work out the total.

An example request that doesn't give me the last page is asking for course that contain students:

https://inst.instructure.com/api/v1/accounts/1/courses?enrollment_type%5B0%5D=student

It includes next link, but doesn't have a last link.

If you are doing bulk operations over lots of courses we've had success with the account reports in Canvas, so we run a report, download the report and then base our processing of the report results. That way you get things like the total number of courses up front.

https://canvas.instructure.com/doc/api/account_reports.html

View solution in original post

0 Kudos
4 Replies
matthew_buckett
Community Participant

Hiya,

I believe this is expected behaviour as outlined in: https://canvas.instructure.com/doc/api/file.pagination.html

When it's difficult/expensive to calculate the total number of items that exist Canvas doesn't give a last page. My guess is that this is done when they are filtering the items outside the DB and so would have to load every single item from the DB to work out the total.

An example request that doesn't give me the last page is asking for course that contain students:

https://inst.instructure.com/api/v1/accounts/1/courses?enrollment_type%5B0%5D=student

It includes next link, but doesn't have a last link.

If you are doing bulk operations over lots of courses we've had success with the account reports in Canvas, so we run a report, download the report and then base our processing of the report results. That way you get things like the total number of courses up front.

https://canvas.instructure.com/doc/api/account_reports.html

View solution in original post

0 Kudos

Hi @matthew_buckett thanks a lot for the reply, that helps! I think I'll have to bite the bullet and rewrite that part of my app to go around that issue.

What bothers me is that I'm running the same script which worked fine one month ago but now I'm getting a different result. I'm not a fan of an API which is not predictable...

I wonder if the cost to calculate the pages is dependent only on the number of items to be listed or if the current server load (we are on the first week of the academic year) plays a big role in that.

Regarding the account report, I don't have the privileges on our environment, so that's out of the question. Nice solution though.

matthew_buckett
Community Participant

@i_oliveira Yeah, I'd expect that if you only have a few pages it may well tell you that the next page is the last, but I haven't tested this.

I completely agree that an API that changes like that isn't very nice. While it's documented, it's much nicer when APIs just behave how people expect them to.

I think the permissions you need to run reports:

  • Account-level settings - manage
  • Courses - view usage reports

Although if you are using the API I don't know if you need both these (some things aren't quite the same between the UI and API with permissions).

The best document about permissions is: https://s3.amazonaws.com/tr-learncanvas/docs/Canvas_Permissions_Account.pdf

 

Just as an update to this thread in case anyone ever bumps here.

I want to have a single function which retrieves any number of pages of information of Canvas' API.

For now I have two functions, one for requests which I know are paginated, one for requests I know are not paginated.

My next build for requests will do the following:

Request first page

if there are no links for pagination, return the output and stop

if there are links for pagination AND no link for last page AND number of items bigger than zero -> request next page (and repeat the same "if" statement again)

if there are links for pagination AND no link for last page AND number of items equal zero -> return the output and stop

if there are links for pagination and a link for last page -> queue all remaining requests as parallel requests.