| 1 | package gotifyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/ |
| 2 | |
| 3 | import ( |
| 4 | "encoding/jsonscip-go gomod github.com/golang/go/src . `encoding/json`/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src . fmt/" |
| 6 | |
| 7 | "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/" |
| 8 | ) |
| 9 | |
| 10 | // DefaultPayloadBuilder provides the default implementation of PayloadBuilder. |
| 11 | type DefaultPayloadBuilderscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/DefaultPayloadBuilder# struct{} |
| 12 | |
| 13 | // PayloadBuilder handles request payload construction and extras parsing. |
| 14 | type PayloadBuilderscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/PayloadBuilder# interface { |
| 15 | PrepareRequestscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/PayloadBuilder#PrepareRequest.( |
| 16 | message string, |
| 17 | config *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/Config#, |
| 18 | extras map[string]any, |
| 19 | date string, |
| 20 | ) *MessageRequestscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/MessageRequest# |
| 21 | ParseExtrasscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/PayloadBuilder#ParseExtras.(params *typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Paramsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Params#, config *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/Config#) (map[string]any, error) |
| 22 | } |
| 23 | |
| 24 | // ParseExtras handles extras parsing from params. |
| 25 | // This function processes the 'extras' parameter which contains additional JSON data |
| 26 | // to be sent with the notification. It attempts to parse JSON from parameters first, |
| 27 | // falling back to configuration extras if parsing fails or no parameter extras exist. |
| 28 | // Parameters: |
| 29 | // - params: Request parameters that may contain 'extras' JSON string |
| 30 | // - config: Configuration that may contain default extras |
| 31 | // |
| 32 | // Returns: map of extra data to include in the notification payload, or error if parsing fails. |
| 33 | func (b *DefaultPayloadBuilderscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/DefaultPayloadBuilder#) ParseExtrasscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/DefaultPayloadBuilder#ParseExtras().( |
| 34 | params *typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Paramsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Params#, |
| 35 | config *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/Config#, |
| 36 | ) (map[string]any, error) { |
| 37 | // Initialize variable to hold parsed extras |
| 38 | var requestExtras map[string]any |
| 39 | |
| 40 | // Check if parameters exist and contain extras |
| 41 | if params != nil { |
| 42 | // Look for 'extras' key in parameters |
| 43 | if extrasStr, exists := (*params)["extras"]; exists && extrasStr != "" { |
| 44 | // Attempt to parse the JSON string into a map |
| 45 | if err := jsonscip-go gomod github.com/golang/go/src . `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src . `encoding/json`/Unmarshal().([]byte(extrasStr), &requestExtras); err != nil { |
| 46 | // Return error for parsing failure |
| 47 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w", ErrExtrasUnmarshalFailedscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/ErrExtrasUnmarshalFailed.) |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Fall back to configuration extras if no valid parameter extras were found |
| 53 | if requestExtras == nil { |
| 54 | requestExtras = config.Extrasscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/Config#Extras. |
| 55 | } |
| 56 | |
| 57 | // Return the resolved extras (either from params or config) |
| 58 | return requestExtras, nil |
| 59 | } |
| 60 | |
| 61 | // PrepareRequest builds the request payload. |
| 62 | // This function constructs the JSON payload that will be sent to the Gotify API, |
| 63 | // combining the message content with configuration settings and any additional extras. |
| 64 | // Parameters: |
| 65 | // - message: The main notification message text |
| 66 | // - config: Configuration containing title, priority, and other settings |
| 67 | // - extras: Additional key-value pairs to include in the notification |
| 68 | // - date: Optional custom timestamp in ISO 8601 format |
| 69 | // |
| 70 | // Returns: *MessageRequest containing all data to be sent to the API. |
| 71 | func (b *DefaultPayloadBuilderscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/DefaultPayloadBuilder#) PrepareRequestscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/DefaultPayloadBuilder#PrepareRequest().( |
| 72 | message string, |
| 73 | config *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/Config#, |
| 74 | extras map[string]any, |
| 75 | date string, |
| 76 | ) *MessageRequestscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/MessageRequest# { |
| 77 | var datePtr *string |
| 78 | if date != "" { |
| 79 | datePtr = &date |
| 80 | } |
| 81 | |
| 82 | return &MessageRequestscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/MessageRequest#{ |
| 83 | Messagescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/MessageRequest#Message.: message, // The notification message content |
| 84 | Titlescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/MessageRequest#Title.: config.Titlescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/Config#Title., // Notification title from configuration |
| 85 | Priorityscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/MessageRequest#Priority.: config.Priorityscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/Config#Priority., // Priority level for the notification |
| 86 | Datescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/MessageRequest#Date.: datePtr, // Optional custom timestamp |
| 87 | Extrasscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/gotify`/MessageRequest#Extras.: extras, // Additional metadata or custom fields |
| 88 | } |
| 89 | } |