| 1 | package lark |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/url" |
| 6 | "strings" |
| 7 | |
| 8 | "github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 9 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 10 | ) |
| 11 | |
| 12 | // Config represents the configuration for the Lark service. |
| 13 | type Config struct { |
| 14 | Host string `default:"open.larksuite.com" desc:"Custom bot URL Host" url:"Host"` |
| 15 | |
| 16 | Secret string `default:"" desc:"Custom bot secret" key:"secret"` |
| 17 | Path string ` desc:"Custom bot token" url:"Path"` |
| 18 | Title string `default:"" desc:"Message Title" key:"title"` |
| 19 | Link string `default:"" desc:"Optional link URL" key:"link"` |
| 20 | } |
| 21 | |
| 22 | // Scheme is the identifier for the Lark service protocol. |
| 23 | const Scheme = "lark" |
| 24 | |
| 25 | // Enums returns a map of enum formatters (none for this service). |
| 26 | func (c *Config) Enums() map[string]types.EnumFormatter { |
| 27 | return map[string]types.EnumFormatter{} |
| 28 | } |
| 29 | |
| 30 | // GetURL constructs a URL from the Config fields. |
| 31 | func (c *Config) GetURL() *url.URL { |
| 32 | resolver := format.NewPropKeyResolver(c) |
| 33 | |
| 34 | return c.getURL(&resolver) |
| 35 | } |
| 36 | |
| 37 | // SetURL updates the Config from a URL. |
| 38 | func (c *Config) SetURL(serviceURL *url.URL) error { |
| 39 | resolver := format.NewPropKeyResolver(c) |
| 40 | |
| 41 | return c.setURL(&resolver, serviceURL) |
| 42 | } |
| 43 | |
| 44 | // getURL constructs a URL using the provided resolver. |
| 45 | func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL { |
| 46 | return &url.URL{ |
| 47 | Host: c.Host, |
| 48 | Path: "/" + c.Path, |
| 49 | Scheme: Scheme, |
| 50 | ForceQuery: true, |
| 51 | RawQuery: format.BuildQuery(resolver), |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // setURL updates the Config from a URL using the provided resolver. |
| 56 | // It sets the host, path, and query parameters, validating host and path, and |
| 57 | // returns an error if parsing or validation fails. |
| 58 | func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error { |
| 59 | c.Host = serviceURL.Host |
| 60 | // Handle documentation generation or empty host |
| 61 | if c.Host == "" || (serviceURL.User != nil && serviceURL.User.Username() == "dummy") { |
| 62 | c.Host = "open.larksuite.com" |
| 63 | } else if c.Host != larkHost && c.Host != feishuHost { |
| 64 | return ErrInvalidHost |
| 65 | } |
| 66 | |
| 67 | c.Path = strings.Trim(serviceURL.Path, "/") |
| 68 | // Handle documentation generation with empty path |
| 69 | if c.Path == "" && (serviceURL.User != nil && serviceURL.User.Username() == "dummy") { |
| 70 | c.Path = "token" |
| 71 | } else if c.Path == "" { |
| 72 | return ErrNoPath |
| 73 | } |
| 74 | |
| 75 | for key, vals := range serviceURL.Query() { |
| 76 | if err := resolver.Set(key, vals[0]); err != nil { |
| 77 | return fmt.Errorf("setting query parameter %q: %w", key, err) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return nil |
| 82 | } |