Debian Codegraph @

pkg/services/push/ifttt/ifttt.go

raw symbols SCIP
1 package ifttt
2
3 import (
4 "bytes"
5 "context"
6 "errors"
7 "fmt"
8 "net/http"
9 "net/url"
10 "time"
11
12 "github.com/nicholas-fedor/shoutrrr/pkg/format"
13 "github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
14 "github.com/nicholas-fedor/shoutrrr/pkg/types"
15 )
16
17 // Service sends notifications to an IFTTT webhook.
18 type Service struct {
19 standard.Standard
20
21 Config *Config
22 pkr format.PropKeyResolver
23 httpClient types.HTTPClient
24 }
25
26 // apiURLFormat defines the IFTTT webhook URL template.
27 const (
28 apiURLFormat = "https://maker.ifttt.com/trigger/%s/with/key/%s"
29 defaultTimeout = 10 * time.Second
30 )
31
32 // ErrSendFailed indicates a failure to send an IFTTT event notification.
33 var (
34 ErrSendFailed = errors.New("failed to send IFTTT event")
35 ErrUnexpectedStatus = errors.New("got unexpected response status code")
36 )
37
38 // GetID returns the identifier for this service.
39 func (s *Service) GetID() string {
40 return Scheme
41 }
42
43 // Initialize configures the service with a URL and logger.
44 func (s *Service) Initialize(serviceURL *url.URL, logger types.StdLogger) error {
45 s.SetLogger(logger)
46 s.Config = &Config{
47 UseMessageAsValue: DefaultMessageValue,
48 }
49 s.pkr = format.NewPropKeyResolver(s.Config)
50
51 if err := s.Config.setURL(&s.pkr, serviceURL); err != nil {
52 return err
53 }
54
55 return nil
56 }
57
58 // Send delivers a notification message to an IFTTT webhook.
59 func (s *Service) Send(message string, params *types.Params) error {
60 config := s.Config
61 if err := s.pkr.UpdateConfigFromParams(config, params); err != nil {
62 return fmt.Errorf("updating config from params: %w", err)
63 }
64
65 payload, err := createJSONToSend(config, message, params)
66 if err != nil {
67 return err
68 }
69
70 for _, event := range config.Events {
71 apiURL := s.createAPIURLForEvent(event)
72 if err := s.doSend(payload, apiURL); err != nil {
73 return fmt.Errorf("%w: event %q: %w", ErrSendFailed, event, err)
74 }
75 }
76
77 return nil
78 }
79
80 // SetHTTPClient sets a custom HTTP client for the service.
81 func (s *Service) SetHTTPClient(client types.HTTPClient) {
82 s.httpClient = client
83 }
84
85 // createAPIURLForEvent builds an IFTTT webhook URL for a specific event.
86 func (s *Service) createAPIURLForEvent(event string) string {
87 return fmt.Sprintf(apiURLFormat, event, s.Config.WebHookID)
88 }
89
90 // doSend executes an HTTP POST request to send the payload to the IFTTT webhook.
91 func (s *Service) doSend(payload []byte, postURL string) error {
92 ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
93 defer cancel()
94
95 req, err := http.NewRequestWithContext(
96 ctx,
97 http.MethodPost,
98 postURL,
99 bytes.NewBuffer(payload),
100 )
101 if err != nil {
102 return fmt.Errorf("creating HTTP request: %w", err)
103 }
104
105 req.Header.Set("Content-Type", "application/json")
106
107 client := s.httpClient
108 if client == nil {
109 client = &http.Client{Timeout: defaultTimeout}
110 }
111
112 res, err := client.Do(req)
113 if err != nil {
114 return fmt.Errorf("sending HTTP request to IFTTT webhook: %w", err)
115 }
116
117 defer func() { _ = res.Body.Close() }()
118
119 if res.StatusCode < 200 || res.StatusCode >= 300 {
120 return fmt.Errorf("%w: %s", ErrUnexpectedStatus, res.Status)
121 }
122
123 return nil
124 }