From 09f973c46a3557df3578275f2f08a59552786a51 Mon Sep 17 00:00:00 2001 From: Xevion Date: Sun, 24 Dec 2023 20:01:56 -0600 Subject: [PATCH] Continue improving comments & use of structured logs --- api.go | 22 ++++++++++------------ helpers.go | 7 ++++--- main.go | 4 ++-- session.go | 12 ++++++------ 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/api.go b/api.go index 47fdf9f..6108d50 100644 --- a/api.go +++ b/api.go @@ -3,7 +3,6 @@ package main import ( "bytes" "encoding/json" - "io" "strconv" "strings" "time" @@ -38,11 +37,10 @@ func GetTerms(search string, offset int, max int) ([]Term, error) { } // print the response body - body, _ := io.ReadAll(res.Body) - log.Printf("Response Body: %s", body) + // _body, _ := io.ReadAll(res.Body) // Assert that the response is JSON - if !ContainsContentType(res.Header.Get("Content-Type"), "application/json") { + if !ContainsContentType(res, "application/json") { log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type")) } @@ -69,7 +67,7 @@ func GetPartOfTerms(search string, term int, offset int, max int) ([]TermParts, } // Assert that the response is JSON - if !ContainsContentType(res.Header.Get("Content-Type"), "application/json") { + if !ContainsContentType(res, "application/json") { log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type")) } @@ -97,7 +95,7 @@ func GetInstructor(search string, term int, offset int, max int) []Instructor { } // Assert that the response is JSON - if !ContainsContentType(res.Header.Get("Content-Type"), "application/json") { + if !ContainsContentType(res, "application/json") { log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type")) } @@ -121,7 +119,7 @@ func GetClassDetails(term int, crn int) *ClassDetails { } // Assert that the response is JSON - if !ContainsContentType(res.Header.Get("Content-Type"), "application/json") { + if !ContainsContentType(res, "application/json") { log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type")) } @@ -151,7 +149,7 @@ func Search(subject string, keyword string, term string, startDate time.Time, en } // Assert that the response is JSON - if !ContainsContentType(res.Header.Get("Content-Type"), "application/json") { + if !ContainsContentType(res, "application/json") { log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type")) } @@ -179,7 +177,7 @@ func GetSubjects(search string, term int, offset int, max int) []Subject { } // Assert that the response is JSON - if !ContainsContentType(res.Header.Get("Content-Type"), "application/json") { + if !ContainsContentType(res, "application/json") { log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type")) } @@ -207,7 +205,7 @@ func GetCampuses(search string, term int, offset int, max int) []Campus { } // Assert that the response is JSON - if !ContainsContentType(res.Header.Get("Content-Type"), "application/json") { + if !ContainsContentType(res, "application/json") { log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type")) } @@ -235,7 +233,7 @@ func GetInstructionalMethods(search string, term int, offset int, max int) ([]In } // Assert that the response is JSON - if !ContainsContentType(res.Header.Get("Content-Type"), "application/json") { + if !ContainsContentType(res, "application/json") { log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type")) } @@ -258,7 +256,7 @@ func GetCourseMeetingTime(term int, crn int) (*MeetingTimeResponse, error) { } // Assert that the response is JSON - if !ContainsContentType(res.Header.Get("Content-Type"), "application/json") { + if !ContainsContentType(res, "application/json") { log.Fatal().Msgf("Response was not JSON: %s", res.Header.Get("Content-Type")) } diff --git a/helpers.go b/helpers.go index 13fe053..68672b1 100644 --- a/helpers.go +++ b/helpers.go @@ -42,9 +42,9 @@ func AddUserAgent(req *http.Request) { req.Header.Add("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36") } -func ContainsContentType(header string, search string) bool { +func ContainsContentType(response *http.Response, search string) bool { // Split on commas, check if any of the types match - for _, content_type := range strings.Split(header, ";") { + for _, content_type := range strings.Split(response.Header.Get("Content-Type"), ";") { if content_type == search { return true } @@ -67,8 +67,9 @@ func Nonce() string { return strconv.Itoa(int(time.Now().UnixMilli())) } +// doRequest performs & logs the request, logging and returning the response func doRequest(req *http.Request) (*http.Response, error) { - log.Debug().Str("method", req.Method).Msg("Request") + log.Debug().Str("method", strings.TrimRight(req.Method, " ")).Str("url", req.URL.String()).Str("query", req.URL.RawQuery).Str("content-type", req.Header.Get("Content-Type")).Msg("Request") res, err := client.Do(req) if err != nil { log.Err(err).Str("method", req.Method).Msg("Request Failed") diff --git a/main.go b/main.go index d368243..9dbae69 100644 --- a/main.go +++ b/main.go @@ -63,11 +63,11 @@ func init() { envVarSplit := strings.Split(envVar, "=") environmentDict.Str(envVarSplit[0], envVarSplit[1]) }) - log.Info().Dict("env", environmentDict).Msg("env") if isDevelopment { log.Logger = zerolog.New(logOut{}).With().Timestamp().Logger() } + log.Info().Dict("env", environmentDict).Msg("env") discordgo.Logger = func(msgL, caller int, format string, a ...interface{}) { pc, file, line, _ := runtime.Caller(caller) @@ -146,7 +146,7 @@ func main() { lo.ForEach(commandDefinitions, func(cmd *discordgo.ApplicationCommand, _ int) { arr.Str(cmd.Name) }) - log.Info().Array("commands", zerolog.Arr()).Msg("Registering commands") + log.Info().Array("commands", arr).Msg("Registering commands") // Register commands registeredCommands := make([]*discordgo.ApplicationCommand, len(commandDefinitions)) diff --git a/session.go b/session.go index b7d739c..bfd7324 100644 --- a/session.go +++ b/session.go @@ -21,12 +21,12 @@ func setup() { } // Validate that cookies were set - baseURL_parsed, err := url.Parse(baseURL) + baseUrlParsed, err := url.Parse(baseURL) if err != nil { - log.Fatal().Msgf("Failed to parse baseURL: %s", baseURL) + log.Fatal().Str("baseURL", baseURL).Err(err).Msg("Failed to parse baseURL") } - current_cookies := client.Jar.Cookies(baseURL_parsed) + current_cookies := client.Jar.Cookies(baseUrlParsed) required_cookies := map[string]bool{ "JSESSIONID": false, "SSB_COOKIE": false, @@ -41,12 +41,12 @@ func setup() { } // Check if all required cookies were set - for cookie_name, cookie_set := range required_cookies { + for cookieName, cookie_set := range required_cookies { if !cookie_set { - log.Error().Msgf("Required cookie %s was not set", cookie_name) + log.Warn().Str("cookieName", cookieName).Msg("Required cookie not set") } } - log.Info().Msg("All cookies acquired. Session setup complete.") + log.Debug().Msg("All required cookies set, session setup complete") // TODO: Validate that the session allows access to termSelection }