| 1 | package googlechatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/ |
| 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 | "net/httpscip-go gomod github.com/golang/go/src . `net/http`/" |
| 10 | "net/urlscip-go gomod github.com/golang/go/src . `net/url`/" |
| 11 | "timescip-go gomod github.com/golang/go/src . time/" |
| 12 | |
| 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 implements a Google Chat notification service. |
| 18 | type Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/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/chat/googlechat`/Service#Standard. |
| 20 | |
| 21 | Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Service#Config. *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Config# |
| 22 | httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/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# |
| 23 | } |
| 24 | |
| 25 | const defaultHTTPTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/defaultHTTPTimeout. = 10 * timescip-go gomod github.com/golang/go/src . time/.Secondscip-go gomod github.com/golang/go/src . time/Second. |
| 26 | |
| 27 | // ErrUnexpectedStatus indicates an unexpected HTTP status code from the Google Chat API. |
| 28 | var ErrUnexpectedStatusscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/ErrUnexpectedStatus. = errorsscip-go gomod github.com/golang/go/src . errors/.Newscip-go gomod github.com/golang/go/src . errors/New().("google chat api returned unexpected http status code") |
| 29 | |
| 30 | // GetID returns the identifier for this service. |
| 31 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Service#) GetIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Service#GetID().() string { |
| 32 | return Schemescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Scheme. |
| 33 | } |
| 34 | |
| 35 | // Initialize configures the service with a URL and logger. |
| 36 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Service#) Initializescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/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 { |
| 37 | s.SetLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/Logger#SetLogger().(logger) |
| 38 | |
| 39 | s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Service#Config. = &Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Config#{} |
| 40 | |
| 41 | if s.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Service#httpClient. == nil { |
| 42 | s.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Service#httpClient. = &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.: defaultHTTPTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/defaultHTTPTimeout.} |
| 43 | } |
| 44 | |
| 45 | return s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Service#Config..SetURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Config#SetURL().(serviceURL) |
| 46 | } |
| 47 | |
| 48 | // Send delivers a notification message to Google Chat. |
| 49 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Service#) Sendscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Service#Send().(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#) error { |
| 50 | config := s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Service#Config. |
| 51 | |
| 52 | jsonBody, err := jsonscip-go gomod github.com/golang/go/src . `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src . `encoding/json`/Marshal().(JSONscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/JSON#{Textscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/JSON#Text.: message}) |
| 53 | if err != nil { |
| 54 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("marshaling message to JSON: %w", err) |
| 55 | } |
| 56 | |
| 57 | postURL := getAPIURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/getAPIURL().(config) |
| 58 | jsonBuffer := bytesscip-go gomod github.com/golang/go/src . bytes/.NewBufferscip-go gomod github.com/golang/go/src . bytes/NewBuffer().(jsonBody) |
| 59 | |
| 60 | req, err := httpscip-go gomod github.com/golang/go/src . `net/http`/.NewRequestWithContextscip-go gomod github.com/golang/go/src . `net/http`/NewRequestWithContext().( |
| 61 | contextscip-go gomod github.com/golang/go/src . context/.Backgroundscip-go gomod github.com/golang/go/src . context/Background().(), |
| 62 | httpscip-go gomod github.com/golang/go/src . `net/http`/.MethodPostscip-go gomod github.com/golang/go/src . `net/http`/MethodPost., |
| 63 | postURL.Stringscip-go gomod github.com/golang/go/src . `net/url`/URL#String().(), |
| 64 | jsonBuffer, |
| 65 | ) |
| 66 | if err != nil { |
| 67 | 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) |
| 68 | } |
| 69 | |
| 70 | 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") |
| 71 | |
| 72 | resp, err := s.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Service#httpClient..Doscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/HTTPClient#Do.(req) |
| 73 | if err != nil { |
| 74 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("sending notification to Google Chat: %w", err) |
| 75 | } |
| 76 | |
| 77 | defer func() { _ = resp.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Response#Body..Closescip-go gomod github.com/golang/go/src . io/Closer#Close().() }() |
| 78 | |
| 79 | if resp.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode. < 200 || resp.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode. >= 300 { |
| 80 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %d", ErrUnexpectedStatusscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/ErrUnexpectedStatus., resp.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode.) |
| 81 | } |
| 82 | |
| 83 | return nil |
| 84 | } |
| 85 | |
| 86 | // SetHTTPClient sets a custom HTTP client for the service. |
| 87 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Service#) SetHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/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#) { |
| 88 | s.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Service#httpClient. = client |
| 89 | } |
| 90 | |
| 91 | // getAPIURL constructs the service URL for Google Chat notifications. |
| 92 | func getAPIURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/getAPIURL().(config *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Config#) *urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL# { |
| 93 | query := urlscip-go gomod github.com/golang/go/src . `net/url`/.Valuesscip-go gomod github.com/golang/go/src . `net/url`/Values#{} |
| 94 | query.Setscip-go gomod github.com/golang/go/src . `net/url`/Values#Set().("key", config.Keyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Config#Key.) |
| 95 | query.Setscip-go gomod github.com/golang/go/src . `net/url`/Values#Set().("token", config.Tokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Config#Token.) |
| 96 | |
| 97 | return &urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL#{ |
| 98 | Pathscip-go gomod github.com/golang/go/src . `net/url`/URL#Path.: config.Pathscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Config#Path., |
| 99 | Hostscip-go gomod github.com/golang/go/src . `net/url`/URL#Host.: config.Hostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/googlechat`/Config#Host., |
| 100 | Schemescip-go gomod github.com/golang/go/src . `net/url`/URL#Scheme.: "https", |
| 101 | RawQueryscip-go gomod github.com/golang/go/src . `net/url`/URL#RawQuery.: query.Encodescip-go gomod github.com/golang/go/src . `net/url`/Values#Encode().(), |
| 102 | } |
| 103 | } |