-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfetchCourses.go
170 lines (155 loc) Β· 7.52 KB
/
fetchCourses.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"github.com/spf13/viper"
// "fmt"
"time"
"bytes"
)
type Course struct {
ID int `json:"id"`
Name string `json:"name"`
AccountID int `json:"account_id"`
UUID string `json:"uuid"`
StartAt time.Time `json:"start_at"`
GradingStandardID interface{} `json:"grading_standard_id"`
IsPublic bool `json:"is_public"`
CreatedAt time.Time `json:"created_at"`
SyllabusBody string `json:"syllabus_body"`
CourseCode string `json:"course_code"`
DefaultView string `json:"default_view"`
RootAccountID int `json:"root_account_id"`
EnrollmentTermID int `json:"enrollment_term_id"`
License string `json:"license"`
PublicDescription string `json:"public_description"`
GradePassbackSetting interface{} `json:"grade_passback_setting"`
EndAt time.Time `json:"end_at"`
PublicSyllabus bool `json:"public_syllabus"`
PublicSyllabusToAuth bool `json:"public_syllabus_to_auth"`
StorageQuotaMb int `json:"storage_quota_mb"`
IsPublicToAuthUsers bool `json:"is_public_to_auth_users"`
Term Term `json:"term"`
CourseProgress CourseProgress `json:"course_progress"`
ApplyAssignmentGroupWeights bool `json:"apply_assignment_group_weights"`
Sections []Sections `json:"sections"`
TotalStudents int `json:"total_students"`
IsFavorite bool `json:"is_favorite"`
Teachers []Teachers `json:"teachers"`
Tabs []Tabs `json:"tabs"`
Calendar Calendar `json:"calendar"`
TimeZone string `json:"time_zone"`
ImageDownloadURL string `json:"image_download_url"`
Concluded bool `json:"concluded"`
Blueprint bool `json:"blueprint"`
Enrollments []Enrollments `json:"enrollments"`
HideFinalGrades bool `json:"hide_final_grades"`
WorkflowState string `json:"workflow_state"`
RestrictEnrollmentsToCourseDates bool `json:"restrict_enrollments_to_course_dates"`
}
type Term struct {
ID int `json:"id"`
Name string `json:"name"`
StartAt time.Time `json:"start_at"`
EndAt time.Time `json:"end_at"`
CreatedAt time.Time `json:"created_at"`
WorkflowState string `json:"workflow_state"`
GradingPeriodGroupID interface{} `json:"grading_period_group_id"`
}
type CourseProgress struct {
RequirementCount int `json:"requirement_count"`
RequirementCompletedCount int `json:"requirement_completed_count"`
NextRequirementURL time.Time `json:"next_requirement_url"`
CompletedAt time.Time `json:"completed_at"`
}
type Sections struct {
ID int `json:"id"`
Name string `json:"name"`
StartAt interface{} `json:"start_at"`
EndAt interface{} `json:"end_at"`
EnrollmentRole string `json:"enrollment_role"`
}
type Teachers struct {
ID int `json:"id"`
DisplayName string `json:"display_name"`
AvatarImageURL string `json:"avatar_image_url"`
HTMLURL string `json:"html_url"`
Pronouns interface{} `json:"pronouns"`
}
type Tabs struct {
ID string `json:"id"`
HTMLURL string `json:"html_url"`
FullURL string `json:"full_url"`
Position int `json:"position"`
Visibility string `json:"visibility"`
Label string `json:"label"`
Type string `json:"type"`
}
type Calendar struct {
Ics string `json:"ics"`
}
type Enrollments struct {
Type string `json:"type"`
Role string `json:"role"`
RoleID int `json:"role_id"`
UserID int `json:"user_id"`
EnrollmentState string `json:"enrollment_state"`
LimitPrivilegesToCourseSection bool `json:"limit_privileges_to_course_section"`
ComputedCurrentGrade interface{} `json:"computed_current_grade"`
ComputedCurrentScore float64 `json:"computed_current_score"`
ComputedFinalGrade interface{} `json:"computed_final_grade"`
ComputedFinalScore float64 `json:"computed_final_score"`
}
func fetchCourses() *[]Course {
// Create URL string from config file
url := viper.Get("canvasdomain").(string) + "api/v1/courses?per_page=60&enrollment_state=active"+
"&enrollment_type=student" +
"&include[]=syllabus_body"+
"&include[]=total_scores"+
"&include[]=public_description"+
"&include[]=course_progress"+
"&include[]=sections"+
"&include[]=total_students"+
"&include[]=favorites"+
"&include[]=teachers"+
"&include[]=tabs"+
"&include[]=term"+
"&include[]=course_image"+
"&include[]=concluded"
// Create a Bearer string by appending string access token
var bearer = "Bearer " + viper.Get("canvastoken").(string)
type Params struct{
PerPage int `json:"per_page"`
// EnrollmentType string `json:"enrollment_type"`
}
m := Params{50}
rawbody, err := json.Marshal(m)
// fmt.Println(string(rawbody))
params := bytes.NewReader(rawbody)
// params := bytes.NewBuffer(rawbody)
// Create a new request using http
req, err := http.NewRequest("GET", url, params)
// add authorization header to the req
req.Header.Add("Authorization", bearer)
// Send req using http Client
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
// fmt.Println(string(body))
courses := make([]Course,0)
err = json.Unmarshal([]byte(body), &courses)
if err != nil {
panic(err)
}
// iterate through course structs
// for _, crs := range courses {
// fmt.Println(crs.Name)
// }
return &courses
}