Constructs a request object for the Canvas LMS API to interact with quiz resources within a specific course, as documented here:
Value
An httr2
request object configured with the full API endpoint URL and
authorization header set for bearer token authentication. This object can then
be further modified or used with httr2
functions to execute the API request.
Details
https://canvas.instructure.com/doc/api/new_quizzes.html https://canvas.instructure.com/doc/api/new_quiz_items.html
The function dynamically builds the API endpoint URL based on the Canvas domain, course ID, and additional parameters for quiz identification or operations. It then initializes a request and sets the authorization header using a bearer token. This function requires that the environment variables CANVAS_DOMAIN, CANVAS_COURSE_ID, and CANVAS_API_TOKEN are set appropriately, e.g. using an .Renviron file in the user directory or RStudio project directory, specifying name-value pairs like so:
CANVAS_DOMAIN=https://nmbu.test.instructure.com CANVAS_API_TOKEN=10453~1kZ... CANVAS_COURSE_ID=1234
Examples
# Assuming appropriate environment variables are set:
if (FALSE) { # \dontrun{
resp <- new_quiz() |>
req_body_json(list(quiz = list(title = "Title of New Quiz"))) |>
req_perform()
nq <- resp_body_json(resp) # To inspect the returned data, including the new quiz id
} # }
# If anything goes wrong, inspect the body of the last response:
if (FALSE) { # \dontrun{
resp <- new_quiz(nq$id, "items") |>
req_body_json(list(item = list(entry_type = "Item"))) |>
req_perform()
resp_body_json(last_response())
} # }
# $errors
# $errors[[1]]
# $errors[[1]]$message
# [1] "entry is missing"