diff options
| author | Juan Roig <jaroig@outlook.com> | 2023-10-30 01:00:50 -0400 |
|---|---|---|
| committer | Juan Roig <jaroig@outlook.com> | 2023-10-30 01:00:50 -0400 |
| commit | 19957a5adcd5436bd73ae1cf07098a952ffe8b01 (patch) | |
| tree | a987b5722215c5840b69ad1fbbd0158b24b77b14 | |
| parent | 41a98cfe6012afc3bb02a027bdf04c8a9fa422ad (diff) | |
switch to using http client instead of invoking static methods
| -rw-r--r-- | main.go | 28 |
1 files changed, 18 insertions, 10 deletions
| @@ -7,8 +7,8 @@ import ( | |||
| 7 | "net/http" | 7 | "net/http" |
| 8 | ) | 8 | ) |
| 9 | 9 | ||
| 10 | func get_courses(token string, link string) []CanvasCourse { | 10 | func get_courses(token string, link string, client http.Client) []CanvasCourse { |
| 11 | resp, err := http.Get(link) | 11 | resp, err := client.Get(link) |
| 12 | if err != nil { | 12 | if err != nil { |
| 13 | // handle error | 13 | // handle error |
| 14 | } | 14 | } |
| @@ -25,8 +25,8 @@ func get_courses(token string, link string) []CanvasCourse { | |||
| 25 | return canvas_courses | 25 | return canvas_courses |
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | func get_aplus(token string, link string) string { | 28 | func get_aplus(token string, link string, client http.Client) string { |
| 29 | resp, err := http.Get(link) | 29 | resp, err := client.Get(link) |
| 30 | 30 | ||
| 31 | if err != nil { | 31 | if err != nil { |
| 32 | // handle error | 32 | // handle error |
| @@ -49,25 +49,31 @@ func get_course_ids(canvas_courses []CanvasCourse) []uint64 { | |||
| 49 | } | 49 | } |
| 50 | 50 | ||
| 51 | func main() { | 51 | func main() { |
| 52 | client := http.Client{ | ||
| 53 | CheckRedirect: func(req *http.Request, via []*http.Request) error { | ||
| 54 | fmt.Println("Redirected to:", req.URL) | ||
| 55 | return nil | ||
| 56 | }, | ||
| 57 | } | ||
| 52 | 58 | ||
| 53 | // https://canvas.instructure.com/api/v1/courses?access_token={}&per_page=100", canvas_token | 59 | // https://canvas.instructure.com/api/v1/courses?access_token={}&per_page=100", canvas_token |
| 54 | base_link := "https://floridapolytechnic.instructure.com/api/v1" | 60 | base_link := "https://floridapolytechnic.instructure.com/api/v1" |
| 55 | token := "instructure api key" | 61 | token := "insert api key here" |
| 56 | //favorites := fmt.Sprintf("%s/users/self/favorites/courses?access_token=%s&per_page=100", base_link, token) | 62 | //favorites := fmt.Sprintf("%s/users/self/favorites/courses?access_token=%s&per_page=100", base_link, token) |
| 57 | //all_courses := fmt.Sprintf("%s/courses?access_token=%s&per_page=100", base_link,token) | 63 | //all_courses := fmt.Sprintf("%s/courses?access_token=%s&per_page=100", base_link,token) |
| 58 | 64 | ||
| 59 | //var canvas_courses []CanvasCourse | 65 | //var canvas_courses []CanvasCourse |
| 60 | //canvas_courses = get_courses(token, favorites) | 66 | //canvas_courses = get_courses(token, favorites, client) |
| 61 | //var course_ids []uint64 | 67 | //var course_ids []uint64 |
| 62 | //course_ids = get_course_ids(canvas_courses) | 68 | //course_ids = get_course_ids(canvas_courses) |
| 63 | 69 | ||
| 64 | //selected := 1 // course_ids[selected] | 70 | //selected := 1 // course_ids[selected] |
| 65 | aplus_link := fmt.Sprintf("%s/courses/%d/external_tools/sessionless_launch?id=913&access_token=%s", base_link, 7329, token) // 7329 always works | 71 | aplus_link := fmt.Sprintf("%s/courses/%d/external_tools/sessionless_launch?id=913&access_token=%s", base_link, 7329, token) // 7329 always works |
| 66 | var aplus string | 72 | var aplus string |
| 67 | aplus = get_aplus(token, aplus_link) | 73 | aplus = get_aplus(token, aplus_link, client) |
| 68 | fmt.Println(aplus) | 74 | fmt.Println(aplus) |
| 69 | 75 | ||
| 70 | resp, err := http.Get(aplus) | 76 | resp, err := client.Get(aplus) |
| 71 | 77 | ||
| 72 | if err != nil { | 78 | if err != nil { |
| 73 | // handle error | 79 | // handle error |
| @@ -76,7 +82,9 @@ func main() { | |||
| 76 | //fmt.Println(resp) | 82 | //fmt.Println(resp) |
| 77 | 83 | ||
| 78 | defer resp.Body.Close() | 84 | defer resp.Body.Close() |
| 79 | body, err := io.ReadAll(resp.Body) | 85 | //body, err := io.ReadAll(resp.Body) |
| 86 | |||
| 87 | //fmt.Println(string(body)) | ||
| 88 | //fmt.Println(resp) | ||
| 80 | 89 | ||
| 81 | fmt.Println(string(body)) | ||
| 82 | } | 90 | } |
