diff options
| author | vin <git@vineetk.net> | 2023-11-16 13:44:11 -0500 |
|---|---|---|
| committer | vin <git@vineetk.net> | 2023-11-20 10:08:58 -0500 |
| commit | 23226c46a16bd1037016c1578b608eb6d23f7546 (patch) | |
| tree | 04cad20e31b2d76b5e76bebed151503c89c43f03 /canvas.go | |
| parent | b966951c47e2ba886d7a99541072c586b64bb17a (diff) | |
use the Canvas API to get A+ tool ID and URL
Was previously hardcoded.
Diffstat (limited to 'canvas.go')
| -rw-r--r-- | canvas.go | 32 |
1 files changed, 29 insertions, 3 deletions
| @@ -2,20 +2,21 @@ package main | |||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "encoding/json" | 4 | "encoding/json" |
| 5 | "errors" | ||
| 5 | "fmt" | 6 | "fmt" |
| 6 | "io" | 7 | "io" |
| 7 | "net/http" | 8 | "net/http" |
| 8 | "net/http/cookiejar" | 9 | "net/http/cookiejar" |
| 9 | "os" | 10 | "os" |
| 11 | "strings" | ||
| 10 | 12 | ||
| 11 | "golang.org/x/net/publicsuffix" | 13 | "golang.org/x/net/publicsuffix" |
| 12 | ) | 14 | ) |
| 13 | 15 | ||
| 14 | func initialize() { | 16 | func initialize() { |
| 15 | base_link = "https://floridapolytechnic.instructure.com/api/v1" | 17 | instance_url := "https://floridapolytechnic.instructure.com" |
| 16 | base_aplus_link = "https://floridapoly.aplusattendance.com/canvas" | 18 | base_link = instance_url + "/api/v1" |
| 17 | token = os.Getenv("CANVAS_API_KEY") | 19 | token = os.Getenv("CANVAS_API_KEY") |
| 18 | external_tools_code = 913 | ||
| 19 | jar, _ := cookiejar.New(&cookiejar.Options{ | 20 | jar, _ := cookiejar.New(&cookiejar.Options{ |
| 20 | PublicSuffixList: publicsuffix.List, | 21 | PublicSuffixList: publicsuffix.List, |
| 21 | }) | 22 | }) |
| @@ -49,6 +50,31 @@ func get_courses(token string, link string, client http.Client) []CanvasCourse { | |||
| 49 | return canvas_courses | 50 | return canvas_courses |
| 50 | } | 51 | } |
| 51 | 52 | ||
| 53 | func init_aplus_toolid(course_id int) error { | ||
| 54 | link := fmt.Sprintf("%s/courses/%d/external_tools?access_token=%s&search_term=aplus&include_parents=true", base_link, course_id, token) | ||
| 55 | resp, err := client.Get(link) | ||
| 56 | if err != nil { | ||
| 57 | return err | ||
| 58 | } | ||
| 59 | defer resp.Body.Close() | ||
| 60 | body, _ := io.ReadAll(resp.Body) | ||
| 61 | |||
| 62 | var external_tools []ExternalTool | ||
| 63 | err = json.Unmarshal(body, &external_tools) | ||
| 64 | if err != nil && !strings.Contains(string(body), "\"unauthorized\"") { | ||
| 65 | return err | ||
| 66 | } | ||
| 67 | |||
| 68 | for _, tool := range external_tools { | ||
| 69 | if strings.Contains(tool.URL, ".aplusattendance.com/") && base_aplus_link == "" { | ||
| 70 | base_aplus_link = tool.URL | ||
| 71 | external_tools_code = tool.ID | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | return errors.New("Could not find A+ link and tool ID") | ||
| 76 | } | ||
| 77 | |||
| 52 | func list_all_courses() { | 78 | func list_all_courses() { |
| 53 | all_courses := fmt.Sprintf("%s/courses?access_token=%s&per_page=100", base_link, token) | 79 | all_courses := fmt.Sprintf("%s/courses?access_token=%s&per_page=100", base_link, token) |
| 54 | 80 | ||
