-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
316 lines (267 loc) · 9.08 KB
/
main.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/gorilla/mux"
"github.com/meruff/go-trailhead-leaderboard-api/trailhead"
)
const (
trailheadApiUrl = "https://profile.api.trailhead.com/graphql"
trailblazerUrl = "https://www.salesforce.com/trailblazer/"
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/trailblazer/{id}", profileHandler)
r.HandleFunc("/trailblazer/{id}/profile", profileHandler)
r.HandleFunc("/trailblazer/{id}/rank", rankHandler)
r.HandleFunc("/trailblazer/{id}/skills", skillsHandler)
r.HandleFunc("/trailblazer/{id}/certifications", certificationsHandler)
r.HandleFunc("/trailblazer/{id}/badges", badgesHandler)
r.HandleFunc("/trailblazer/{id}/badges/{filter}", badgesHandler)
r.HandleFunc("/trailblazer/{id}/badges/{filter}/{count}", badgesHandler)
r.HandleFunc("/trailblazer/{id}/badges/{filter}/{count}/{after}", badgesHandler)
r.PathPrefix("/").HandlerFunc(catchAllHandler)
r.Use(loggingHandler)
http.Handle("/", r)
port := os.Getenv("PORT")
if port == "" {
http.ListenAndServe(":8000", nil)
} else {
http.ListenAndServe(":"+os.Getenv("PORT"), nil)
}
}
// profileHandler gets profile information of the Trailblazer i.e. Name, Company, Title etc. Uses a
// Trailblazer handle only, not an ID.
func profileHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
userAlias := vars["id"]
if strings.HasPrefix(userAlias, "005") {
writeErrorToBrowser(w, "/profile requires a trailblazer handle, not an ID as a parameter.", 503)
return
}
res, err := http.Get(trailblazerUrl + userAlias)
if res != nil {
defer res.Body.Close()
}
if err != nil {
log.Println(err)
writeErrorToBrowser(w, "Problem retrieving profile data.", 503)
return
}
body, err := io.ReadAll(res.Body)
if err != nil {
log.Println(err)
writeErrorToBrowser(w, "Problem reading profile body.", 503)
return
}
if !strings.Contains(string(body), "var profile = ") {
writeErrorToBrowser(
w,
fmt.Sprintf("Cannot find profile data for %s. Does this trailblazer exist?", vars["id"]),
503,
)
return
}
re := regexp.MustCompile(`var profile = (.*);`)
match := re.FindStringSubmatch(string(body))
if len(match) > 1 {
var trailheadProfileData trailhead.Profile
json.Unmarshal([]byte(match[1]), &trailheadProfileData)
profileDataForUi := trailhead.ProfileReturn{}
profileDataForUi.ProfilePhotoUrl = trailheadProfileData.PhotoURL
profileDataForUi.ProfileUser.TBID_Role = trailheadProfileData.Role
profileDataForUi.ProfileUser.CompanyName = trailheadProfileData.Company.Name
profileDataForUi.ProfileUser.TrailblazerId = vars["id"]
profileDataForUi.ProfileUser.Title = trailheadProfileData.Title
profileDataForUi.ProfileUser.FirstName = trailheadProfileData.FirstName
profileDataForUi.ProfileUser.LastName = trailheadProfileData.LastName
profileDataForUi.ProfileUser.Id = trailheadProfileData.ID
encodeAndWriteToBrowser(w, profileDataForUi)
} else {
writeErrorToBrowser(w, "No profile data found.", 503)
}
}
// rankHandler returns information about a Trailblazer's rank and overall points
func rankHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
responseBody, err := doTrailheadCallout(
trailhead.GetGraphqlPayload("GetTrailheadRank", vars["id"], "", trailhead.GetRankQuery()),
)
if err != nil {
writeErrorToBrowser(w, "No rank data returned from Trailhead.", 503)
}
var trailheadRankData trailhead.Rank
json.Unmarshal([]byte(responseBody), &trailheadRankData)
encodeAndWriteToBrowser(w, trailheadRankData.Data)
}
// skillsHandler returns information about a Trailblazer's skills
func skillsHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
responseBody, err := doTrailheadCallout(
trailhead.GetGraphqlPayload(
"GetEarnedSkills",
vars["id"],
"",
trailhead.GetSkillsQuery(),
),
)
if err != nil {
writeErrorToBrowser(w, "No skills data returned from Trailhead.", 503)
}
var trailheadSkillsData trailhead.Skills
json.Unmarshal([]byte(responseBody), &trailheadSkillsData)
encodeAndWriteToBrowser(w, trailheadSkillsData.Data)
}
// certificationsHandler gets Salesforce certifications the Trailblazer has earned.
func certificationsHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
responseBody, err := doTrailheadCallout(
trailhead.GetGraphqlPayload(
"GetUserCertifications",
vars["id"],
"",
trailhead.GetCertificationsQuery(),
),
)
if err != nil {
writeErrorToBrowser(w, "No certification data returned from Trailhead.", 503)
}
var trailheadCertificationsData trailhead.Certifications
json.Unmarshal([]byte(responseBody), &trailheadCertificationsData)
certificationReturnData := trailhead.CertificationsReturn{}
for _, certification := range trailheadCertificationsData.Data.Profile.Credential.Certifications {
cReturn := trailhead.Certification{}
cReturn.DateCompleted = certification.DateCompleted
cReturn.CertificationUrl = certification.InfoURL
cReturn.Description = certification.PublicDescription
cReturn.CertificationStatus = certification.Status.Title
cReturn.Title = certification.Title
cReturn.CertificationImageUrl = certification.LogoURL
if dateExpired, ok := certification.DateExpired.(string); ok {
cReturn.DateExpired = dateExpired
} else {
cReturn.DateExpired = ""
}
certificationReturnData.CertificationsList = append(
certificationReturnData.CertificationsList, cReturn,
)
}
encodeAndWriteToBrowser(w, certificationReturnData)
}
// badgeshandler gets badges the Trailblazer has earned. Returns first 8. Optionally can
// provide filter criteria, or additional return count. i.e. "event" type badges, count by 30.
func badgesHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
filter, after, count := vars["filter"], vars["after"], vars["count"]
badgeRequestStruct := trailhead.BadgeRequest{}
// Set filter
if contains(getValidBadgeFilters(), filter) {
var upperFilter = strings.ToUpper(filter)
badgeRequestStruct.Filter = upperFilter
} else if filter != "all" && filter != "" {
writeErrorToBrowser(
w,
fmt.Sprintf("Expected badge filter to be one of: %s.", strings.Join(getValidBadgeFilters(), ", ")),
501,
)
return
}
// Set count
if count != "" {
countConvert, err := strconv.Atoi(count)
if err != nil {
log.Println("Error parsing badge count from params.")
}
badgeRequestStruct.Count = countConvert
} else {
badgeRequestStruct.Count = 8
}
// Set after
if after != "" {
badgeRequestStruct.After = after
}
responseBody, err := doTrailheadCallout(
trailhead.GetGraphqlPayload(
"GetTrailheadBadges",
vars["id"],
trailhead.GetBadgesFilterPayload(vars["id"], badgeRequestStruct),
trailhead.GetBadgesQuery(),
),
)
if err != nil {
writeErrorToBrowser(w, "No badge data returned from Trailhead.", 503)
}
var trailheadBadgeData trailhead.Badges
json.Unmarshal([]byte(responseBody), &trailheadBadgeData)
encodeAndWriteToBrowser(w, trailheadBadgeData.Data)
}
// loggingHandler logs time spent to access each request/what page was requested.
func loggingHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t1 := time.Now()
next.ServeHTTP(w, r)
t2 := time.Now()
log.Printf("[%s] %q %v\n", r.Method, r.URL.String(), t2.Sub(t1))
})
}
// catchAllHandler is the default message if no Trailblazer Id or handle is provided,
// or if the u ser has navigated to an unsupported page.
func catchAllHandler(w http.ResponseWriter, r *http.Request) {
writeErrorToBrowser(
w,
"Please provide a valid handle or visit a valid URL. Example: /trailblazer/{id}",
501,
)
}
// doTrailheadCallout makes a callout to the given URL using the given
func doTrailheadCallout(payload string) (string, error) {
client := &http.Client{}
req, err := http.NewRequest("POST", trailheadApiUrl, strings.NewReader(payload))
if err != nil {
log.Println(err)
}
req.Header.Add("Accept", "*/*")
req.Header.Add("Accept-Language", "en-US,en;q=0.5")
req.Header.Add("Content-Type", "application/json")
res, err := client.Do(req)
if res != nil {
defer res.Body.Close()
}
if err != nil {
log.Println(err)
}
body, err := io.ReadAll(res.Body)
return string(body), err
}
// writeErrorToBrowser writes an HTTP error to the broswer in JSON.
func writeErrorToBrowser(w http.ResponseWriter, errorMsg string, code int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write([]byte(fmt.Sprintf(`{"error":"%s"`, errorMsg)))
}
// encodeAndWriteToBrowser encodes a given interface and writes it to the browser as JSON.
func encodeAndWriteToBrowser(w http.ResponseWriter, i interface{}) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(i)
}
// contains simply checks if a string exists inside a slice.
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
// getValidBadgeFilters returns a slice containing valid filters for Trailblazer badges.
func getValidBadgeFilters() []string {
return []string{"module", "project", "superbadge", "event", "standalone"}
}