From 31eb8ac40e9f8dc2382163b3737bc6428fca93b7 Mon Sep 17 00:00:00 2001 From: Hakan Bayindir Date: Thu, 4 Jul 2024 01:19:15 +0300 Subject: [PATCH] proj: General dusting off and polishing. - refactor: Fix pushoverValidationResponse to PushoverValidationResponse. Types start with a capital letter. - refactor: getDeviceList returns PushoverValidationResponse instead of raw JSON. - refactor: Go through gofmt. - proj: Leave a marker about what to do next in nudge.go. --- CHANGELOG.md | 6 ++++++ nudge.go | 27 ++++++++++++++------------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5da69de..1698db2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ **Note:** Please add newest entries on top. Use ISO date format YYYY-MM-DD and markdown formatting. +## 2024-07-04 +- refactor: Fix `pushoverValidationResponse` to `PushoverValidationResponse`. Types start with a capital letter. +- refactor: `getDeviceList` returns `PushoverValidationResponse` instead of raw JSON. +- refactor: Go through `gofmt`. +- proj: Leave a marker about what to do next in `nudge.go`. + ## 2024-06-02 - feat: Continue implementing `verify_recipients` function. - feat: Implement JSON unmarshalling to a data structure for `verify_recipients` function. diff --git a/nudge.go b/nudge.go index 42579dd..5c75a3d 100644 --- a/nudge.go +++ b/nudge.go @@ -112,7 +112,7 @@ type ValidConfigurationOptions struct { // Note: All the fields in the following struct are exported in order to make struct tags function. // // struct tags cannot function on non-exported names, since other libraries can't peek into the struct. -type pushoverValidationResponse struct { +type PushoverValidationResponse struct { Status int `json:"status"` // Tells us how the request went. 1 is OK, 0 is Not OK. Group int `json:"group"` // Denotes whether the submitted user key belongs to a group. Groups are comprised of many users and used by commercial customers. Devices []string `json:"devices"` // Contains the list of devices (or recipients) under that user key. @@ -606,7 +606,7 @@ func printState(runtimeConfiguration *RuntimeConfiguration, notification *Notifi * Error handling is left to the caller function. See https://pushover.net/api#validate * for understanding what this function returns, and how to interpret it. */ -func getDeviceList(runtimeConfiguration *RuntimeConfiguration, logger *zap.SugaredLogger) (body []byte) { +func getDeviceList(runtimeConfiguration *RuntimeConfiguration, logger *zap.SugaredLogger) (pushoverResponse PushoverValidationResponse) { logger.Debugf("Getting list of users from Pushover.net") valuesToSend := url.Values{} // We will build our URL object and send it to the API. @@ -622,37 +622,36 @@ func getDeviceList(runtimeConfiguration *RuntimeConfiguration, logger *zap.Sugar } defer response.Body.Close() // Automatically close response.Body when the function exits. - body, err = io.ReadAll(response.Body) + body, err := io.ReadAll(response.Body) logger.Debugf("Got the following response: %s", body) // Need to convert this JSON to an object to be able to interact with it nicely. - var responseStruct pushoverValidationResponse - err = json.Unmarshal(body, &responseStruct) + var responseStruct PushoverValidationResponse + err = json.Unmarshal(body, &responseStruct) if err != nil { logger.Panicf("Cannot unmarshal JSON to a struct (error is %s).", err) } - logger.Infof("Pushover Response for User List") logger.Infof("-------------------------------") logger.Infof("Status: %d", responseStruct.Status) if responseStruct.Status == 1 { - logger.Infof("Group: %d", responseStruct.Group) - logger.Infof("Device(s): %s", responseStruct.Devices) - logger.Infof("License(s): %s", responseStruct.Licenses) + logger.Infof("Group: %d", responseStruct.Group) + logger.Infof("Device(s): %s", responseStruct.Devices) + logger.Infof("License(s): %s", responseStruct.Licenses) } else if responseStruct.Status == 0 { // This is not the best way to handle this, but good enough for now. // TODO: Revisit here and polish if necessary logger.Panicf("There's an error during validation (error is %s).", responseStruct.Error[0]) } - + logger.Infof("Request ID: %s", responseStruct.Request) logger.Infof("") // Always leave an empty line at the end. - return body + return responseStruct } /* This function verifies the supplied recipients from the valid endpoints list obtained @@ -662,9 +661,11 @@ func getDeviceList(runtimeConfiguration *RuntimeConfiguration, logger *zap.Sugar * For API limits, see https://pushover.net/api#friendly */ func verifyRecipients(notificationToSend *Notification, runtimeConfiguration *RuntimeConfiguration, logger *zap.SugaredLogger) { - responseFromPushover := getDeviceList(runtimeConfiguration, logger) + pushoverResponse := getDeviceList(runtimeConfiguration, logger) + + // TODO: Left here. Implement recipient verification next. getDeviceList is complete. - logger.Debugf("Device list query returned as follows: %s", responseFromPushover) + logger.Debugf("Device list query returned as follows: %s", pushoverResponse) } // This function sends the actual notification via PUSH request, via Pushover. -- 2.45.2