| 1 | package signalgrid |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/tls" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "net/http" |
| 9 | "net/url" |
| 10 | "strings" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/nicholas-fedor/shoutrrr/internal/meta" |
| 14 | "github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 15 | "github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 16 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 17 | ) |
| 18 | |
| 19 | // Service sends notifications to Signalgrid. |
| 20 | type Service struct { |
| 21 | // Standard provides base service functionality including logging. |
| 22 | standard.Standard |
| 23 | |
| 24 | // Config holds Signalgrid credentials and notification options. |
| 25 | Config *Config |
| 26 | // pkr resolves property keys for configuration updates from URL parameters. |
| 27 | pkr format.PropKeyResolver |
| 28 | // httpClient is the HTTP client used for Push API requests. |
| 29 | httpClient types.HTTPClient |
| 30 | } |
| 31 | |
| 32 | const ( |
| 33 | // apiURL is the Signalgrid Push API endpoint. |
| 34 | apiURL = "https://api.signalgrid.co/v1/push" |
| 35 | // contentType is the form encoding required by the Push API. |
| 36 | contentType = "application/x-www-form-urlencoded" |
| 37 | // defaultHTTPTimeout is the timeout applied to Push API requests. |
| 38 | defaultHTTPTimeout = 10 * time.Second |
| 39 | ) |
| 40 | |
| 41 | // GetID returns the service identifier. |
| 42 | // |
| 43 | // Returns: |
| 44 | // - The scheme name "signalgrid". |
| 45 | func (s *Service) GetID() string { |
| 46 | return Scheme |
| 47 | } |
| 48 | |
| 49 | // Initialize configures the service with a URL and logger. |
| 50 | // |
| 51 | // Parameters: |
| 52 | // - serviceURL: The Signalgrid service URL. |
| 53 | // - logger: Logger used for service output. |
| 54 | // |
| 55 | // Returns: |
| 56 | // - An error if default properties cannot be applied or the URL is invalid. |
| 57 | func (s *Service) Initialize(serviceURL *url.URL, logger types.StdLogger) error { |
| 58 | s.SetLogger(logger) |
| 59 | s.Config = &Config{} |
| 60 | s.pkr = format.NewPropKeyResolver(s.Config) |
| 61 | |
| 62 | if err := s.pkr.SetDefaultProps(s.Config); err != nil { |
| 63 | return fmt.Errorf("setting default props: %w", err) |
| 64 | } |
| 65 | |
| 66 | if err := s.Config.setURL(&s.pkr, serviceURL); err != nil { |
| 67 | return err |
| 68 | } |
| 69 | |
| 70 | if s.httpClient == nil { |
| 71 | s.httpClient = &http.Client{ |
| 72 | Timeout: defaultHTTPTimeout, |
| 73 | Transport: &http.Transport{ |
| 74 | TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS12}, |
| 75 | }, |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return nil |
| 80 | } |
| 81 | |
| 82 | // Send delivers a notification message to Signalgrid. |
| 83 | // |
| 84 | // Parameters: |
| 85 | // - message: The notification body. |
| 86 | // - params: Optional runtime overrides for title, type, and critical. |
| 87 | // |
| 88 | // Returns: |
| 89 | // - An error if configuration updates or delivery fail. |
| 90 | func (s *Service) Send(message string, params *types.Params) error { |
| 91 | config := *s.Config |
| 92 | if err := s.pkr.UpdateConfigFromParams(&config, params); err != nil { |
| 93 | return fmt.Errorf("updating config from params: %w", err) |
| 94 | } |
| 95 | |
| 96 | if err := s.send(message, &config); err != nil { |
| 97 | return fmt.Errorf("%w: %w", ErrSendFailed, err) |
| 98 | } |
| 99 | |
| 100 | return nil |
| 101 | } |
| 102 | |
| 103 | // SetHTTPClient sets a custom HTTP client for the service. |
| 104 | // |
| 105 | // Parameters: |
| 106 | // - client: The HTTP client to use for API requests. |
| 107 | func (s *Service) SetHTTPClient(client types.HTTPClient) { |
| 108 | s.httpClient = client |
| 109 | } |
| 110 | |
| 111 | // send posts a notification to the Signalgrid Push API. |
| 112 | // |
| 113 | // Parameters: |
| 114 | // - message: The notification body sent as the form field body. |
| 115 | // - config: The resolved configuration used to populate remaining form fields. |
| 116 | // |
| 117 | // Returns: |
| 118 | // - An error if the request cannot be created, sent, or the API returns a non-success status. |
| 119 | func (s *Service) send(message string, config *Config) error { |
| 120 | data := url.Values{} |
| 121 | data.Set("client_key", config.ClientKey) |
| 122 | data.Set("channel", config.Channel) |
| 123 | data.Set("body", message) |
| 124 | data.Set("type", config.Type.String()) |
| 125 | |
| 126 | if config.Title != "" { |
| 127 | data.Set("title", config.Title) |
| 128 | } |
| 129 | |
| 130 | if config.Critical { |
| 131 | data.Set("critical", "true") |
| 132 | } |
| 133 | |
| 134 | ctx, cancel := context.WithTimeout(context.Background(), defaultHTTPTimeout) |
| 135 | defer cancel() |
| 136 | |
| 137 | req, err := http.NewRequestWithContext( |
| 138 | ctx, |
| 139 | http.MethodPost, |
| 140 | apiURL, |
| 141 | strings.NewReader(data.Encode()), |
| 142 | ) |
| 143 | if err != nil { |
| 144 | return fmt.Errorf("creating request: %w", err) |
| 145 | } |
| 146 | |
| 147 | req.Header.Set("Content-Type", contentType) |
| 148 | req.Header.Set("User-Agent", "shoutrrr/"+meta.Version) |
| 149 | |
| 150 | client := s.httpClient |
| 151 | if client == nil { |
| 152 | client = &http.Client{Timeout: defaultHTTPTimeout} |
| 153 | } |
| 154 | |
| 155 | res, err := client.Do(req) |
| 156 | if err != nil { |
| 157 | return fmt.Errorf("sending request: %w", err) |
| 158 | } |
| 159 | |
| 160 | defer func() { _ = res.Body.Close() }() |
| 161 | |
| 162 | responseBody, err := io.ReadAll(res.Body) |
| 163 | if err != nil { |
| 164 | return fmt.Errorf("reading response body: %w", err) |
| 165 | } |
| 166 | |
| 167 | if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusMultipleChoices { |
| 168 | return fmt.Errorf("%w: %s: %s", ErrUnexpectedStatus, res.Status, string(responseBody)) |
| 169 | } |
| 170 | |
| 171 | return nil |
| 172 | } |