use the Canvas API to get A+ tool ID and URL

Was previously hardcoded.
This commit is contained in:
Vineet K 2023-11-16 13:44:11 -05:00
parent b966951c47
commit 23226c46a1
5 changed files with 43 additions and 6 deletions

View File

@ -2,7 +2,7 @@
This is an cli utility initially created for students at Florida Polytechnic University to submit their aplus codes. 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 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 ## Usage

View File

@ -7,6 +7,7 @@ import (
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
"os"
"strings" "strings"
) )
@ -60,6 +61,12 @@ func submit_code(course_code int, attendance_code string) {
} }
func launch_aplus(course_code int) 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", aplus_link := fmt.Sprintf("%s/courses/%d/external_tools/sessionless_launch?id=%d&access_token=%s",
base_link, course_code, external_tools_code, token) base_link, course_code, external_tools_code, token)
aplus := get_aplus(token, aplus_link, client) aplus := get_aplus(token, aplus_link, client)

View File

@ -2,20 +2,21 @@ package main
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"net/http/cookiejar" "net/http/cookiejar"
"os" "os"
"strings"
"golang.org/x/net/publicsuffix" "golang.org/x/net/publicsuffix"
) )
func initialize() { func initialize() {
base_link = "https://floridapolytechnic.instructure.com/api/v1" instance_url := "https://floridapolytechnic.instructure.com"
base_aplus_link = "https://floridapoly.aplusattendance.com/canvas" base_link = instance_url + "/api/v1"
token = os.Getenv("CANVAS_API_KEY") token = os.Getenv("CANVAS_API_KEY")
external_tools_code = 913
jar, _ := cookiejar.New(&cookiejar.Options{ jar, _ := cookiejar.New(&cookiejar.Options{
PublicSuffixList: publicsuffix.List, PublicSuffixList: publicsuffix.List,
}) })
@ -49,6 +50,31 @@ func get_courses(token string, link string, client http.Client) []CanvasCourse {
return canvas_courses 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() { func list_all_courses() {
all_courses := fmt.Sprintf("%s/courses?access_token=%s&per_page=100", base_link, token) all_courses := fmt.Sprintf("%s/courses?access_token=%s&per_page=100", base_link, token)

View File

@ -9,9 +9,7 @@ import (
) )
var base_link string var base_link string
var base_aplus_link string
var token string var token string
var external_tools_code int
var client http.Client var client http.Client
func main() { func main() {

View File

@ -54,3 +54,9 @@ type Enrollment struct {
EnrollmentState string `json:"enrollment_state"` EnrollmentState string `json:"enrollment_state"`
LimitPrivilegesToCourseSection bool `json:"limit_privileges_to_course_section"` LimitPrivilegesToCourseSection bool `json:"limit_privileges_to_course_section"`
} }
type ExternalTool struct {
ID int `json:"id"`
URL string `json:"url"`
Status string `json:"status"`
}