| 1 | package genericscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src . bytes/" |
| 5 | "contextscip-go gomod github.com/golang/go/src . context/" |
| 6 | "encoding/jsonscip-go gomod github.com/golang/go/src . `encoding/json`/" |
| 7 | "errorsscip-go gomod github.com/golang/go/src . errors/" |
| 8 | "fmtscip-go gomod github.com/golang/go/src . fmt/" |
| 9 | "ioscip-go gomod github.com/golang/go/src . io/" |
| 10 | "mapsscip-go gomod github.com/golang/go/src . maps/" |
| 11 | "net/httpscip-go gomod github.com/golang/go/src . `net/http`/" |
| 12 | "net/urlscip-go gomod github.com/golang/go/src . `net/url`/" |
| 13 | "stringsscip-go gomod github.com/golang/go/src . strings/" |
| 14 | |
| 15 | "github.com/nicholas-fedor/shoutrrr/pkg/formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/" |
| 16 | "github.com/nicholas-fedor/shoutrrr/pkg/services/standardscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/" |
| 17 | "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/" |
| 18 | ) |
| 19 | |
| 20 | // Service implements a generic notification service for custom webhooks. |
| 21 | type Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service# struct { |
| 22 | standardscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/.Standardscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Standard. |
| 23 | |
| 24 | Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Config. *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config# |
| 25 | pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#pkr. 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# |
| 26 | httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#httpClient. typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/HTTPClient# |
| 27 | } |
| 28 | |
| 29 | // JSONTemplate identifies the JSON format for webhook payloads. |
| 30 | const ( |
| 31 | JSONTemplatescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/JSONTemplate. = "JSON" |
| 32 | ) |
| 33 | |
| 34 | // ErrSendFailed indicates a failure to send a notification to the generic webhook. |
| 35 | var ( |
| 36 | ErrSendFailedscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/ErrSendFailed. = errorsscip-go gomod github.com/golang/go/src . errors/.Newscip-go gomod github.com/golang/go/src . errors/New().("failed to send notification to generic webhook") |
| 37 | ErrUnexpectedStatusscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/ErrUnexpectedStatus. = errorsscip-go gomod github.com/golang/go/src . errors/.Newscip-go gomod github.com/golang/go/src . errors/New().("server returned unexpected response status code") |
| 38 | ErrTemplateNotLoadedscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/ErrTemplateNotLoaded. = errorsscip-go gomod github.com/golang/go/src . errors/.Newscip-go gomod github.com/golang/go/src . errors/New().("template has not been loaded") |
| 39 | ) |
| 40 | |
| 41 | // GetID returns the identifier for this service. |
| 42 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#) GetIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#GetID().() string { |
| 43 | return Schemescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Scheme. |
| 44 | } |
| 45 | |
| 46 | // GetPayload prepares the request payload based on the configured template. |
| 47 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#) GetPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#GetPayload().(config *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#, 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#) (ioscip-go gomod github.com/golang/go/src . io/.Readerscip-go gomod github.com/golang/go/src . io/Reader#, error) { |
| 48 | switch config.Templatescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#Template. { |
| 49 | case "": |
| 50 | // No template, send message directly |
| 51 | return bytesscip-go gomod github.com/golang/go/src . bytes/.NewBufferStringscip-go gomod github.com/golang/go/src . bytes/NewBufferString().(params[config.MessageKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#MessageKey.]), nil |
| 52 | case "json", JSONTemplatescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/JSONTemplate.: |
| 53 | // JSON template, marshal params to JSON |
| 54 | // Add extra data to params |
| 55 | mapsscip-go gomod github.com/golang/go/src . maps/.Copyscip-go gomod github.com/golang/go/src . maps/Copy().(params, config.extraDatascip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#extraData.) |
| 56 | |
| 57 | // Marshal to JSON |
| 58 | jsonBytes, err := jsonscip-go gomod github.com/golang/go/src . `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src . `encoding/json`/Marshal().(params) |
| 59 | if err != nil { |
| 60 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("marshaling params to JSON: %w", err) |
| 61 | } |
| 62 | |
| 63 | return bytesscip-go gomod github.com/golang/go/src . bytes/.NewBufferscip-go gomod github.com/golang/go/src . bytes/NewBuffer().(jsonBytes), nil |
| 64 | } |
| 65 | |
| 66 | // Get the template |
| 67 | tpl, found := s.GetTemplatescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/Templater#GetTemplate().(config.Templatescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#Template.) |
| 68 | if !found { |
| 69 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %q", ErrTemplateNotLoadedscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/ErrTemplateNotLoaded., config.Templatescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#Template.) |
| 70 | } |
| 71 | |
| 72 | // Buffer for template execution |
| 73 | bb := &bytesscip-go gomod github.com/golang/go/src . bytes/.Bufferscip-go gomod github.com/golang/go/src . bytes/Buffer#{} |
| 74 | if err := tpl.Executescip-go gomod github.com/golang/go/src . `text/template`/Template#Execute().(bb, params); err != nil { |
| 75 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("executing template %q: %w", config.Templatescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#Template., err) |
| 76 | } |
| 77 | |
| 78 | return bb, nil |
| 79 | } |
| 80 | |
| 81 | // GetServiceURLFromCustom converts a custom webhook URL into a standard service URL. |
| 82 | func (*Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#) GetServiceURLFromCustomscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#GetServiceURLFromCustom().(customURL *urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL#) (*urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL#, error) { |
| 83 | // Copy the URL to modify |
| 84 | webhookURL := *customURL |
| 85 | if stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(webhookURL.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.) { |
| 86 | // Remove the scheme prefix if present |
| 87 | webhookURL.Schemescip-go gomod github.com/golang/go/src . `net/url`/URL#Scheme. = webhookURL.Schemescip-go gomod github.com/golang/go/src . `net/url`/URL#Scheme.[len(Schemescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Scheme.)+1:] |
| 88 | } |
| 89 | |
| 90 | // Parse config from webhook URL |
| 91 | config, pkr, err := ConfigFromWebhookURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/ConfigFromWebhookURL().(&webhookURL) |
| 92 | if err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | |
| 96 | // Generate and return the service URL |
| 97 | return config.getURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#getURL().(&pkr), nil |
| 98 | } |
| 99 | |
| 100 | // Initialize configures the service with a URL and logger. |
| 101 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#) Initializescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Initialize().(serviceURL *urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL#, logger typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.StdLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/StdLogger#) error { |
| 102 | // Set the logger for the service |
| 103 | s.SetLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/Logger#SetLogger().(logger) |
| 104 | |
| 105 | // Get default config and property key resolver |
| 106 | config, pkr := DefaultConfigscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/DefaultConfig().() |
| 107 | // Assign config to service |
| 108 | s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Config. = config |
| 109 | // Assign resolver |
| 110 | s.pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#pkr. = pkr |
| 111 | |
| 112 | // Set URL and return any error |
| 113 | return s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Config..setURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#setURL().(&s.pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#pkr., serviceURL) |
| 114 | } |
| 115 | |
| 116 | // Send delivers a notification message to a generic webhook endpoint. |
| 117 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#) Sendscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Send().(message string, paramsPtr *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#) error { |
| 118 | // Create a copy of the config to avoid modifying the original |
| 119 | config := *s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#Config. |
| 120 | |
| 121 | var 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# |
| 122 | if paramsPtr == nil { |
| 123 | // Handle nil params by creating empty map |
| 124 | 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#{} |
| 125 | } else { |
| 126 | params = *paramsPtr |
| 127 | } |
| 128 | |
| 129 | if err := s.pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#pkr..UpdateConfigFromParamsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#UpdateConfigFromParams().(&config, ¶ms); err != nil { |
| 130 | // Update config with runtime parameters |
| 131 | s.Logfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/Logger#Logf().("Failed to update params: %v", err) |
| 132 | } |
| 133 | |
| 134 | // Prepare parameters for sending |
| 135 | sendParams := createSendParamsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/createSendParams().(&config, params, message) |
| 136 | if err := s.doSendscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#doSend().(&config, sendParams); err != nil { |
| 137 | // Execute the HTTP request to send the notification |
| 138 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %s", ErrSendFailedscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/ErrSendFailed., err.Error()) |
| 139 | } |
| 140 | |
| 141 | return nil |
| 142 | } |
| 143 | |
| 144 | // SetHTTPClient sets a custom HTTP client for the service. |
| 145 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#) SetHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#SetHTTPClient().(client typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/HTTPClient#) { |
| 146 | if client == nil { |
| 147 | s.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#httpClient. = nil |
| 148 | |
| 149 | return |
| 150 | } |
| 151 | |
| 152 | if c, ok := client.(*httpscip-go gomod github.com/golang/go/src . `net/http`/.Clientscip-go gomod github.com/golang/go/src . `net/http`/Client#); ok && c == nil { |
| 153 | s.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#httpClient. = nil |
| 154 | |
| 155 | return |
| 156 | } |
| 157 | |
| 158 | s.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#httpClient. = client |
| 159 | } |
| 160 | |
| 161 | // doSend executes the HTTP request to send a notification to the webhook. |
| 162 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#) doSendscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#doSend().(config *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#, 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#) error { |
| 163 | // Get the webhook URL as string |
| 164 | postURL := config.WebhookURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#WebhookURL().().Stringscip-go gomod github.com/golang/go/src . `net/url`/URL#String().() |
| 165 | |
| 166 | // Prepare the request payload |
| 167 | payload, err := s.GetPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#GetPayload().(config, params) |
| 168 | if err != nil { |
| 169 | return err |
| 170 | } |
| 171 | |
| 172 | // Create background context for the request |
| 173 | ctx := contextscip-go gomod github.com/golang/go/src . context/.Backgroundscip-go gomod github.com/golang/go/src . context/Background().() |
| 174 | |
| 175 | // Create HTTP request with context |
| 176 | req, err := httpscip-go gomod github.com/golang/go/src . `net/http`/.NewRequestWithContextscip-go gomod github.com/golang/go/src . `net/http`/NewRequestWithContext().(ctx, config.RequestMethodscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#RequestMethod., postURL, payload) |
| 177 | if err != nil { |
| 178 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("creating HTTP request: %w", err) |
| 179 | } |
| 180 | |
| 181 | // Determine content type based on payload format |
| 182 | contentType := config.ContentTypescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#ContentType. |
| 183 | if config.Templatescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#Template. == "" { |
| 184 | // When no template is specified, payload is plain text |
| 185 | contentType = "text/plain" |
| 186 | } |
| 187 | |
| 188 | // Set content type header |
| 189 | req.Headerscip-go gomod github.com/golang/go/src . `net/http`/Request#Header..Setscip-go gomod github.com/golang/go/src . `net/http`/Header#Set().("Content-Type", contentType) |
| 190 | // Set accept header |
| 191 | req.Headerscip-go gomod github.com/golang/go/src . `net/http`/Request#Header..Setscip-go gomod github.com/golang/go/src . `net/http`/Header#Set().("Accept", contentType) |
| 192 | |
| 193 | for key, value := range config.headersscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#headers. { |
| 194 | // Add custom headers |
| 195 | req.Headerscip-go gomod github.com/golang/go/src . `net/http`/Request#Header..Setscip-go gomod github.com/golang/go/src . `net/http`/Header#Set().(key, value) |
| 196 | } |
| 197 | |
| 198 | client := s.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Service#httpClient. |
| 199 | if client == nil { |
| 200 | client = &httpscip-go gomod github.com/golang/go/src . `net/http`/.Clientscip-go gomod github.com/golang/go/src . `net/http`/Client#{} |
| 201 | } |
| 202 | |
| 203 | // Send the HTTP request |
| 204 | res, err := client.Doscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/HTTPClient#Do.(req) |
| 205 | if err != nil { |
| 206 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("sending HTTP request: %w", err) |
| 207 | } |
| 208 | |
| 209 | if res != nil && res.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Response#Body. != nil { |
| 210 | // Read and log response body if available |
| 211 | defer func() { |
| 212 | _ = res.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Response#Body..Closescip-go gomod github.com/golang/go/src . io/Closer#Close().() |
| 213 | }() |
| 214 | |
| 215 | if body, err := ioscip-go gomod github.com/golang/go/src . io/.ReadAllscip-go gomod github.com/golang/go/src . io/ReadAll().(res.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Response#Body.); err == nil { |
| 216 | s.Logscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/Logger#Log().("Server response: ", string(body)) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if res.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode. >= httpscip-go gomod github.com/golang/go/src . `net/http`/.StatusBadRequestscip-go gomod github.com/golang/go/src . `net/http`/StatusBadRequest. { |
| 221 | // Check for error status codes (4xx and 5xx) |
| 222 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %s", ErrUnexpectedStatusscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/ErrUnexpectedStatus., res.Statusscip-go gomod github.com/golang/go/src . `net/http`/Response#Status.) |
| 223 | } |
| 224 | |
| 225 | return nil |
| 226 | } |
| 227 | |
| 228 | // createSendParams constructs parameters for sending a notification. |
| 229 | func createSendParamsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/createSendParams().(config *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#, 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#, message string) 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# { |
| 230 | // Initialize new params map |
| 231 | sendParams := 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#{} |
| 232 | |
| 233 | for key, val := range params { |
| 234 | // Copy params, mapping title key if necessary |
| 235 | if key == typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.TitleKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/TitleKey. { |
| 236 | key = config.TitleKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#TitleKey. |
| 237 | } |
| 238 | |
| 239 | sendParams[key] = val |
| 240 | } |
| 241 | |
| 242 | // Add the message |
| 243 | sendParams[config.MessageKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/specialized/generic`/Config#MessageKey.] = message |
| 244 | |
| 245 | return sendParams |
| 246 | } |