| 1 | package iftttscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src . bytes/" |
| 5 | "contextscip-go gomod github.com/golang/go/src . context/" |
| 6 | "errorsscip-go gomod github.com/golang/go/src . errors/" |
| 7 | "fmtscip-go gomod github.com/golang/go/src . fmt/" |
| 8 | "net/httpscip-go gomod github.com/golang/go/src . `net/http`/" |
| 9 | "net/urlscip-go gomod github.com/golang/go/src . `net/url`/" |
| 10 | "timescip-go gomod github.com/golang/go/src . time/" |
| 11 | |
| 12 | "github.com/nicholas-fedor/shoutrrr/pkg/formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/" |
| 13 | "github.com/nicholas-fedor/shoutrrr/pkg/services/standardscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/" |
| 14 | "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/" |
| 15 | ) |
| 16 | |
| 17 | // Service sends notifications to an IFTTT webhook. |
| 18 | type Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service# struct { |
| 19 | 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/push/ifttt`/Service#Standard. |
| 20 | |
| 21 | Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#Config. *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Config# |
| 22 | pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/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# |
| 23 | httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/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# |
| 24 | } |
| 25 | |
| 26 | // apiURLFormat defines the IFTTT webhook URL template. |
| 27 | const ( |
| 28 | apiURLFormatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/apiURLFormat. = "https://maker.ifttt.com/trigger/%s/with/key/%s" |
| 29 | defaultTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/defaultTimeout. = 10 * timescip-go gomod github.com/golang/go/src . time/.Secondscip-go gomod github.com/golang/go/src . time/Second. |
| 30 | ) |
| 31 | |
| 32 | // ErrSendFailed indicates a failure to send an IFTTT event notification. |
| 33 | var ( |
| 34 | ErrSendFailedscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/ErrSendFailed. = errorsscip-go gomod github.com/golang/go/src . errors/.Newscip-go gomod github.com/golang/go/src . errors/New().("failed to send IFTTT event") |
| 35 | ErrUnexpectedStatusscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/ErrUnexpectedStatus. = errorsscip-go gomod github.com/golang/go/src . errors/.Newscip-go gomod github.com/golang/go/src . errors/New().("got unexpected response status code") |
| 36 | ) |
| 37 | |
| 38 | // GetID returns the identifier for this service. |
| 39 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#) GetIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#GetID().() string { |
| 40 | return Schemescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Scheme. |
| 41 | } |
| 42 | |
| 43 | // Initialize configures the service with a URL and logger. |
| 44 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#) Initializescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/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 { |
| 45 | s.SetLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/Logger#SetLogger().(logger) |
| 46 | s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#Config. = &Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Config#{ |
| 47 | UseMessageAsValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Config#UseMessageAsValue.: DefaultMessageValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/DefaultMessageValue., |
| 48 | } |
| 49 | s.pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#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().(s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#Config.) |
| 50 | |
| 51 | if err := s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#Config..setURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Config#setURL().(&s.pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#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 *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#) Sendscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#Send().(message string, 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 { |
| 60 | config := s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#Config. |
| 61 | if err := s.pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#pkr..UpdateConfigFromParamsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#UpdateConfigFromParams().(config, params); err != nil { |
| 62 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("updating config from params: %w", err) |
| 63 | } |
| 64 | |
| 65 | payload, err := createJSONToSendscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/createJSONToSend().(config, message, params) |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | |
| 70 | for _, event := range config.Eventsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Config#Events. { |
| 71 | apiURL := s.createAPIURLForEventscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#createAPIURLForEvent().(event) |
| 72 | if err := s.doSendscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#doSend().(payload, apiURL); err != nil { |
| 73 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: event %q: %w", ErrSendFailedscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/ErrSendFailed., event, err) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return nil |
| 78 | } |
| 79 | |
| 80 | // SetHTTPClient sets a custom HTTP client for the service. |
| 81 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#) SetHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/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#) { |
| 82 | s.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#httpClient. = client |
| 83 | } |
| 84 | |
| 85 | // createAPIURLForEvent builds an IFTTT webhook URL for a specific event. |
| 86 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#) createAPIURLForEventscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#createAPIURLForEvent().(event string) string { |
| 87 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Sprintfscip-go gomod github.com/golang/go/src . fmt/Sprintf().(apiURLFormatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/apiURLFormat., event, s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#Config..WebHookIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Config#WebHookID.) |
| 88 | } |
| 89 | |
| 90 | // doSend executes an HTTP POST request to send the payload to the IFTTT webhook. |
| 91 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#) doSendscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#doSend().(payload []byte, postURL string) error { |
| 92 | ctx, cancel := contextscip-go gomod github.com/golang/go/src . context/.WithTimeoutscip-go gomod github.com/golang/go/src . context/WithTimeout().(contextscip-go gomod github.com/golang/go/src . context/.Backgroundscip-go gomod github.com/golang/go/src . context/Background().(), defaultTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/defaultTimeout.) |
| 93 | defer cancel() |
| 94 | |
| 95 | req, err := httpscip-go gomod github.com/golang/go/src . `net/http`/.NewRequestWithContextscip-go gomod github.com/golang/go/src . `net/http`/NewRequestWithContext().( |
| 96 | ctx, |
| 97 | httpscip-go gomod github.com/golang/go/src . `net/http`/.MethodPostscip-go gomod github.com/golang/go/src . `net/http`/MethodPost., |
| 98 | postURL, |
| 99 | bytesscip-go gomod github.com/golang/go/src . bytes/.NewBufferscip-go gomod github.com/golang/go/src . bytes/NewBuffer().(payload), |
| 100 | ) |
| 101 | if err != nil { |
| 102 | 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) |
| 103 | } |
| 104 | |
| 105 | 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", "application/json") |
| 106 | |
| 107 | client := s.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/Service#httpClient. |
| 108 | if client == nil { |
| 109 | client = &httpscip-go gomod github.com/golang/go/src . `net/http`/.Clientscip-go gomod github.com/golang/go/src . `net/http`/Client#{Timeoutscip-go gomod github.com/golang/go/src . `net/http`/Client#Timeout.: defaultTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/push/ifttt`/defaultTimeout.} |
| 110 | } |
| 111 | |
| 112 | res, err := client.Doscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/HTTPClient#Do.(req) |
| 113 | if err != nil { |
| 114 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("sending HTTP request to IFTTT webhook: %w", err) |
| 115 | } |
| 116 | |
| 117 | defer func() { _ = res.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Response#Body..Closescip-go gomod github.com/golang/go/src . io/Closer#Close().() }() |
| 118 | |
| 119 | if res.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode. < 200 || res.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode. >= 300 { |
| 120 | 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/push/ifttt`/ErrUnexpectedStatus., res.Statusscip-go gomod github.com/golang/go/src . `net/http`/Response#Status.) |
| 121 | } |
| 122 | |
| 123 | return nil |
| 124 | } |