From 23226c46a16bd1037016c1578b608eb6d23f7546 Mon Sep 17 00:00:00 2001 From: vin Date: Thu, 16 Nov 2023 13:44:11 -0500 Subject: [PATCH] use the Canvas API to get A+ tool ID and URL Was previously hardcoded. --- README.md | 2 +- aplus.go | 7 +++++++ canvas.go | 32 +++++++++++++++++++++++++++++--- main.go | 2 -- structs.go | 6 ++++++ 5 files changed, 43 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 99780c3..93917e4 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ This is an cli utility initially created for students at Florida Polytechnic University to submit their aplus codes. In time, we intend to generalize this utility to all universities. -In the meantime however, any students from other universities wishing to use the utility may change the base_link and external_tools_code (you see this in the URL when you navigate to Aplus attendance within your course) variables. +In the meantime however, any students from other universities wishing to use the utility may change the instance_url (the main URL you see before the / when you use Canvas) variables. ## Usage diff --git a/aplus.go b/aplus.go index a8d4daf..51a5a10 100644 --- a/aplus.go +++ b/aplus.go @@ -7,6 +7,7 @@ import ( "io" "net/http" "net/url" + "os" "strings" ) @@ -60,6 +61,12 @@ func submit_code(course_code int, attendance_code string) { } func launch_aplus(course_code int) string { + err := init_aplus_toolid(course_code) + if err != nil { + fmt.Println("Failed to get A+ base URL and tool ID") + os.Exit(1) + } + aplus_link := fmt.Sprintf("%s/courses/%d/external_tools/sessionless_launch?id=%d&access_token=%s", base_link, course_code, external_tools_code, token) aplus := get_aplus(token, aplus_link, client) diff --git a/canvas.go b/canvas.go index df5f94c..5a43999 100644 --- a/canvas.go +++ b/canvas.go @@ -2,20 +2,21 @@ package main import ( "encoding/json" + "errors" "fmt" "io" "net/http" "net/http/cookiejar" "os" + "strings" "golang.org/x/net/publicsuffix" ) func initialize() { - base_link = "https://floridapolytechnic.instructure.com/api/v1" - base_aplus_link = "https://floridapoly.aplusattendance.com/canvas" + instance_url := "https://floridapolytechnic.instructure.com" + base_link = instance_url + "/api/v1" token = os.Getenv("CANVAS_API_KEY") - external_tools_code = 913 jar, _ := cookiejar.New(&cookiejar.Options{ PublicSuffixList: publicsuffix.List, }) @@ -49,6 +50,31 @@ func get_courses(token string, link string, client http.Client) []CanvasCourse { return canvas_courses } +func init_aplus_toolid(course_id int) error { + link := fmt.Sprintf("%s/courses/%d/external_tools?access_token=%s&search_term=aplus&include_parents=true", base_link, course_id, token) + resp, err := client.Get(link) + if err != nil { + return err + } + defer resp.Body.Close() + body, _ := io.ReadAll(resp.Body) + + var external_tools []ExternalTool + err = json.Unmarshal(body, &external_tools) + if err != nil && !strings.Contains(string(body), "\"unauthorized\"") { + return err + } + + for _, tool := range external_tools { + if strings.Contains(tool.URL, ".aplusattendance.com/") && base_aplus_link == "" { + base_aplus_link = tool.URL + external_tools_code = tool.ID + } + } + + return errors.New("Could not find A+ link and tool ID") +} + func list_all_courses() { all_courses := fmt.Sprintf("%s/courses?access_token=%s&per_page=100", base_link, token) diff --git a/main.go b/main.go index b6b1e1a..9386b46 100644 --- a/main.go +++ b/main.go @@ -9,9 +9,7 @@ import ( ) var base_link string -var base_aplus_link string var token string -var external_tools_code int var client http.Client func main() { diff --git a/structs.go b/structs.go index bf75787..2da2899 100644 --- a/structs.go +++ b/structs.go @@ -54,3 +54,9 @@ type Enrollment struct { EnrollmentState string `json:"enrollment_state"` LimitPrivilegesToCourseSection bool `json:"limit_privileges_to_course_section"` } + +type ExternalTool struct { + ID int `json:"id"` + URL string `json:"url"` + Status string `json:"status"` +}