aplus/canvas.go

73 lines
1.6 KiB
Go

package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"os"
"golang.org/x/net/publicsuffix"
)
func initialize() {
base_link = "https://floridapolytechnic.instructure.com/api/v1"
base_aplus_link = "https://floridapoly.aplusattendance.com/canvas"
token = os.Getenv("CANVAS_API_KEY")
external_tools_code = 913
jar, _ := cookiejar.New(&cookiejar.Options{
PublicSuffixList: publicsuffix.List,
})
client = http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
fmt.Println("Redirected to:", req.URL)
fmt.Println(req.Cookies())
return nil
},
Jar: jar,
}
}
func get_courses(token string, link string, client http.Client) []CanvasCourse {
resp, err := client.Get(link)
if err != nil {
fmt.Println("Error performing GET request to get courses.")
return nil
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var canvas_courses []CanvasCourse
err2 := json.Unmarshal(body, &canvas_courses)
if err2 != nil {
fmt.Println(err2)
return nil
}
return canvas_courses
}
func list_all_courses() {
all_courses := fmt.Sprintf("%s/courses?access_token=%s&per_page=100", base_link, token)
canvas_courses := get_courses(token, all_courses, client)
for _, course := range canvas_courses {
fmt.Printf("%d, %s\n", course.ID%10000, course.Name)
}
}
func list_favorite_courses() {
// course selection by user happens below
favorites := fmt.Sprintf("%s/users/self/favorites/courses?access_token=%s&per_page=100", base_link, token)
canvas_courses := get_courses(token, favorites, client)
for _, course := range canvas_courses {
fmt.Printf("%d, %s\n", course.ID%10000, course.Name)
}
}