This Golang project provides access to the Canvas API
The Go canvasapi library provides a struct for every Canvas API endpoint found here:
https://canvas.instructure.com/doc/api/[https://canvas.instructure.com/doc/api/]
It is autogenerated using the json version of the documentation found here:
https://canvas.instructure.com/doc/api/api-docs.json[https://canvas.instructure.com/doc/api/api-docs.json]
and which leads to json that describes individual endpoints. For example:
https://canvas.instructure.com/doc/api/assignments.json[https://canvas.instructure.com/doc/api/assignments.json]
Examples
The Canvas struct
Construct a new Canvas struct. This instance will be passed to all requests to the Canvas API and contains the
url of the Canvas instance you will interact with as well as an API token. Note that this library does not manage the token and will return an error if the token is not valid or has expired. The calling code is responsible for handling
that error and aquiring a valid token
token := os.Getenv("CANVAS_API_TOKEN") canvasURL := "atomicjolt.instructure.com" canvas := canvasapi.New(token, canvasURL)
GET a list of assignments for a given course
`
// This example shows listing assignments for the list assignments endpoint documented here:
// https://canvas.instructure.com/doc/api/assignments.html#method.assignments_api.index[https://canvas.instructure.com/doc/api/assignments.html#method.assignments_api.index]
// Construct a new ListAssignmentsAssignments struct
listAssignments := requests.ListAssignmentsAssignments{}
// 'Path' parameters are used to build the full path to make the request and are typically required.
listAssignments.Path.CourseID = courseID
// Query parameters can be added to the 'Query' substruct. Most query parameters are optional.
listAssignments.Query.Include = []string{"submission", "can_edit"}
// Call 'Do' on the struct to execute the query. All 'Do' methods require an instance of a Canvas struct.
assignments, pager, err := listAssignments.Do(&canvas)
if err != nil {
t.Errorf("ListAssignmentsAssignments failed: %v", err)
}
// Be sure to check for additional pages of results and make additional requests as necessary.
if pager.Next != nil {
moreAssignments, pager, err := listAssignments.Do(&canvas, pager.Next.URL)
}
`
Run all Tests:
NOTE!!!!!! This will run against the Canvas Instance you use to generate the token. Create a test account and use that
account id in the .env file.
create a .env file and add a valid Canvas API Token
CANVAS_API_TOKEN=a valid token
CANVAS_ACCOUNT_ID=578
gotest ./... -v
Update packages
go get -u
Build lib:
go mod tidy // Clean up the mod file
go build ./...