| 1 |
package googlechat |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"net/url" |
| 6 |
|
| 7 |
"github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 8 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 9 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 10 |
) |
| 11 |
|
| 12 |
// Config holds the configuration for the Google Chat service. |
| 13 |
type Config struct { |
| 14 |
standard.EnumlessConfig |
| 15 |
|
| 16 |
Host string `default:"chat.googleapis.com"` |
| 17 |
Path string |
| 18 |
Token string |
| 19 |
Key string |
| 20 |
} |
| 21 |
|
| 22 |
const ( |
| 23 |
Scheme = "googlechat" |
| 24 |
) |
| 25 |
|
| 26 |
// Static error definitions. |
| 27 |
var ( |
| 28 |
ErrMissingKey = errors.New("missing field 'key'") |
| 29 |
ErrMissingToken = errors.New("missing field 'token'") |
| 30 |
) |
| 31 |
|
| 32 |
// GetURL returns the URL representation of the configuration. |
| 33 |
func (c *Config) GetURL() *url.URL { |
| 34 |
resolver := format.NewPropKeyResolver(c) |
| 35 |
|
| 36 |
return c.getURL(&resolver) |
| 37 |
} |
| 38 |
|
| 39 |
// SetURL updates the configuration from a URL. |
| 40 |
func (c *Config) SetURL(serviceURL *url.URL) error { |
| 41 |
resolver := format.NewPropKeyResolver(c) |
| 42 |
|
| 43 |
return c.setURL(&resolver, serviceURL) |
| 44 |
} |
| 45 |
|
| 46 |
// getURL constructs the URL from the configuration using the provided resolver. |
| 47 |
func (c *Config) getURL(_ types.ConfigQueryResolver) *url.URL { |
| 48 |
query := url.Values{} |
| 49 |
query.Set("key", c.Key) |
| 50 |
query.Set("token", c.Token) |
| 51 |
|
| 52 |
return &url.URL{ |
| 53 |
Host: c.Host, |
| 54 |
Path: c.Path, |
| 55 |
RawQuery: query.Encode(), |
| 56 |
Scheme: Scheme, |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
// setURL updates the configuration from a URL using the provided resolver. |
| 61 |
func (c *Config) setURL(_ types.ConfigQueryResolver, serviceURL *url.URL) error { |
| 62 |
c.Host = serviceURL.Host |
| 63 |
c.Path = serviceURL.Path |
| 64 |
|
| 65 |
query := serviceURL.Query() |
| 66 |
c.Key = query.Get("key") |
| 67 |
c.Token = query.Get("token") |
| 68 |
|
| 69 |
// Only enforce if explicitly provided but empty |
| 70 |
if query.Has("key") && c.Key == "" { |
| 71 |
return ErrMissingKey |
| 72 |
} |
| 73 |
|
| 74 |
if query.Has("token") && c.Token == "" { |
| 75 |
return ErrMissingToken |
| 76 |
} |
| 77 |
|
| 78 |
return nil |
| 79 |
} |