| 1 | package genericscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/ |
| 2 | |
| 3 | import ( |
| 4 | "fmtscip-go gomod github.com/golang/go/src . fmt/" |
| 5 | "net/urlscip-go gomod github.com/golang/go/src . `net/url`/" |
| 6 | |
| 7 | "github.com/nicholas-fedor/shoutrrr/pkg/formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/" |
| 8 | "github.com/nicholas-fedor/shoutrrr/pkg/services/standardscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/" |
| 9 | "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/" |
| 10 | ) |
| 11 | |
| 12 | // Config holds settings for the generic notification service. |
| 13 | type Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config# struct { |
| 14 | standardscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/.EnumlessConfigscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#EnumlessConfig. |
| 15 | |
| 16 | // The webhook URL to send notifications to |
| 17 | webhookURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#webhookURL. *urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL# |
| 18 | // Custom HTTP headers to include in requests |
| 19 | headersscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#headers. map[string]string |
| 20 | // Additional data to include in JSON payloads |
| 21 | extraDatascip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#extraData. map[string]string |
| 22 | ContentTypescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#ContentType. string `default:"application/json" desc:"The default value of the Content-Type header (dynamically set based on payload format)" key:"contenttype"` |
| 23 | DisableTLSscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#DisableTLS. bool `default:"No" key:"disabletls"` |
| 24 | Templatescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#Template. string ` desc:"The template used for creating the request payload" key:"template" optional:""` |
| 25 | Titlescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#Title. string `default:"" key:"title"` |
| 26 | TitleKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#TitleKey. string `default:"title" desc:"The key that will be used for the title value" key:"titlekey"` |
| 27 | MessageKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#MessageKey. string `default:"message" desc:"The key that will be used for the message value" key:"messagekey"` |
| 28 | RequestMethodscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#RequestMethod. string `default:"POST" key:"method"` |
| 29 | } |
| 30 | |
| 31 | // Scheme identifies this service in configuration URLs. |
| 32 | const ( |
| 33 | Schemescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Scheme. = "generic" |
| 34 | DefaultWebhookSchemescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/DefaultWebhookScheme. = "https" |
| 35 | ) |
| 36 | |
| 37 | // DefaultConfig creates a new Config with default values and its associated PropKeyResolver. |
| 38 | func DefaultConfigscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/DefaultConfig().() (*Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#, formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/.PropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#) { |
| 39 | // Initialize empty config |
| 40 | config := &Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#{} |
| 41 | // Create property key resolver |
| 42 | pkr := formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/.NewPropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/NewPropKeyResolver().(config) |
| 43 | // Set default properties from struct tags |
| 44 | if err := pkr.SetDefaultPropsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#SetDefaultProps().(config); err != nil { |
| 45 | panic(fmtscip-go gomod github.com/golang/go/src . fmt/.Sprintfscip-go gomod github.com/golang/go/src . fmt/Sprintf().("setting default properties: %v", err)) |
| 46 | } |
| 47 | |
| 48 | return config, pkr |
| 49 | } |
| 50 | |
| 51 | // ConfigFromWebhookURL constructs a Config from a parsed webhook URL. |
| 52 | func ConfigFromWebhookURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/ConfigFromWebhookURL().(webhookURL *urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL#) (*Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#, formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/.PropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#, error) { |
| 53 | // Get default config and resolver |
| 54 | config, pkr := DefaultConfigscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/DefaultConfig().() |
| 55 | |
| 56 | // Extract query parameters |
| 57 | webhookQuery := webhookURL.Queryscip-go gomod github.com/golang/go/src . `net/url`/URL#Query().() |
| 58 | // Extract custom headers and extra data |
| 59 | headers, extraData := stripCustomQueryValuesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/stripCustomQueryValues().(webhookQuery) |
| 60 | |
| 61 | // Set config properties from remaining query params |
| 62 | _, err := formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/.SetConfigPropsFromQueryscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/SetConfigPropsFromQuery().(&pkr, webhookQuery) |
| 63 | if err != nil { |
| 64 | return nil, pkr, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("setting config properties from query: %w", err) |
| 65 | } |
| 66 | |
| 67 | // Update URL with modified query |
| 68 | webhookURL.RawQueryscip-go gomod github.com/golang/go/src . `net/url`/URL#RawQuery. = webhookQuery.Encodescip-go gomod github.com/golang/go/src . `net/url`/Values#Encode().() |
| 69 | // Assign webhook URL |
| 70 | config.webhookURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#webhookURL. = webhookURL |
| 71 | // Assign extracted headers |
| 72 | config.headersscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#headers. = headers |
| 73 | // Assign extracted extra data |
| 74 | config.extraDatascip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#extraData. = extraData |
| 75 | // Set TLS based on scheme |
| 76 | config.DisableTLSscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#DisableTLS. = webhookURL.Schemescip-go gomod github.com/golang/go/src . `net/url`/URL#Scheme. == "http" |
| 77 | |
| 78 | return config, pkr, nil |
| 79 | } |
| 80 | |
| 81 | // GetURL generates a URL from the current configuration values. |
| 82 | func (c *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#) GetURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#GetURL().() *urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL# { |
| 83 | // Create resolver for this config |
| 84 | resolver := formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/.NewPropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/NewPropKeyResolver().(c) |
| 85 | |
| 86 | // Generate URL using resolver |
| 87 | return c.getURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#getURL().(&resolver) |
| 88 | } |
| 89 | |
| 90 | // SetURL updates the configuration from a service URL. |
| 91 | func (c *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#) SetURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#SetURL().(serviceURL *urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL#) error { |
| 92 | // Create resolver for this config |
| 93 | resolver := formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/.NewPropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/NewPropKeyResolver().(c) |
| 94 | |
| 95 | // Parse and set URL |
| 96 | return c.setURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#setURL().(&resolver, serviceURL) |
| 97 | } |
| 98 | |
| 99 | // WebhookURL returns the configured webhook URL, adjusted for TLS settings. |
| 100 | func (c *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#) WebhookURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#WebhookURL().() *urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL# { |
| 101 | // Copy the URL to modify |
| 102 | webhookURL := *c.webhookURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#webhookURL. |
| 103 | // Set default HTTPS scheme |
| 104 | webhookURL.Schemescip-go gomod github.com/golang/go/src . `net/url`/URL#Scheme. = DefaultWebhookSchemescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/DefaultWebhookScheme. |
| 105 | |
| 106 | if c.DisableTLSscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#DisableTLS. { |
| 107 | // Use HTTP if TLS is disabled |
| 108 | webhookURL.Schemescip-go gomod github.com/golang/go/src . `net/url`/URL#Scheme. = "http" |
| 109 | } |
| 110 | |
| 111 | return &webhookURL |
| 112 | } |
| 113 | |
| 114 | // getURL generates a service URL from the configuration using the provided resolver. |
| 115 | func (c *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#) getURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#getURL().(resolver typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.ConfigQueryResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/ConfigQueryResolver#) *urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL# { |
| 116 | // Copy webhook URL |
| 117 | serviceURL := *c.webhookURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#webhookURL. |
| 118 | // Get existing query params |
| 119 | webhookQuery := c.webhookURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#webhookURL..Queryscip-go gomod github.com/golang/go/src . `net/url`/URL#Query().() |
| 120 | // Build query with config fields |
| 121 | serviceQuery := formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/.BuildQueryWithCustomFieldsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/BuildQueryWithCustomFields().(resolver, webhookQuery) |
| 122 | // Add custom headers and extra data |
| 123 | appendCustomQueryValuesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/appendCustomQueryValues().(serviceQuery, c.headersscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#headers., c.extraDatascip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#extraData.) |
| 124 | // Encode the query |
| 125 | serviceURL.RawQueryscip-go gomod github.com/golang/go/src . `net/url`/URL#RawQuery. = serviceQuery.Encodescip-go gomod github.com/golang/go/src . `net/url`/Values#Encode().() |
| 126 | // Set service scheme |
| 127 | serviceURL.Schemescip-go gomod github.com/golang/go/src . `net/url`/URL#Scheme. = Schemescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Scheme. |
| 128 | |
| 129 | return &serviceURL |
| 130 | } |
| 131 | |
| 132 | // setURL updates the configuration from a service URL using the provided resolver. |
| 133 | func (c *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#) setURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#setURL().(resolver typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.ConfigQueryResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/ConfigQueryResolver#, serviceURL *urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL#) error { |
| 134 | // Copy service URL |
| 135 | webhookURL := *serviceURL |
| 136 | // Extract query parameters |
| 137 | serviceQuery := serviceURL.Queryscip-go gomod github.com/golang/go/src . `net/url`/URL#Query().() |
| 138 | // Extract custom headers and extra data |
| 139 | headers, extraData := stripCustomQueryValuesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/stripCustomQueryValues().(serviceQuery) |
| 140 | |
| 141 | // Set config properties from query |
| 142 | customQuery, err := formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/.SetConfigPropsFromQueryscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/SetConfigPropsFromQuery().(resolver, serviceQuery) |
| 143 | if err != nil { |
| 144 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("setting config properties from service URL query: %w", err) |
| 145 | } |
| 146 | |
| 147 | // Update URL with remaining query |
| 148 | webhookURL.RawQueryscip-go gomod github.com/golang/go/src . `net/url`/URL#RawQuery. = customQuery.Encodescip-go gomod github.com/golang/go/src . `net/url`/Values#Encode().() |
| 149 | // Assign webhook URL |
| 150 | c.webhookURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#webhookURL. = &webhookURL |
| 151 | // Assign extracted headers |
| 152 | c.headersscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#headers. = headers |
| 153 | // Assign extracted extra data |
| 154 | c.extraDatascip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#extraData. = extraData |
| 155 | |
| 156 | return nil |
| 157 | } |