Debian Codegraph @

pkg/services/push/bark/bark_config.go

raw
1 package bark
2
3 import (
4 "errors"
5 "fmt"
6 "net/url"
7 "strings"
8
9 "github.com/nicholas-fedor/shoutrrr/pkg/format"
10 "github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
11 "github.com/nicholas-fedor/shoutrrr/pkg/types"
12 )
13
14 // Config holds configuration settings for the Bark service.
15 type Config struct {
16 standard.EnumlessConfig
17
18 Title string `default:"" desc:"Notification title, optionally set by the sender" key:"title"`
19 Host string ` desc:"Server hostname and port" url:"host"`
20 Path string `default:"/" desc:"Server path" url:"path"`
21 DeviceKey string ` desc:"The key for each device" url:"password"`
22 Scheme string `default:"https" desc:"Server protocol, http or https" key:"scheme"`
23 Sound string `default:"" desc:"Value from https://github.com/Finb/Bark/tree/master/Sounds" key:"sound"`
24 Badge int64 `default:"0" desc:"The number displayed next to App icon" key:"badge"`
25 Icon string `default:"" desc:"An url to the icon, available only on iOS 15 or later" key:"icon"`
26 Group string `default:"" desc:"The group of the notification" key:"group"`
27 URL string `default:"" desc:"Url that will jump when click notification" key:"url"`
28 Category string `default:"" desc:"Reserved field, no use yet" key:"category"`
29 Copy string `default:"" desc:"The value to be copied" key:"copy"`
30 }
31
32 // Scheme is the URL scheme identifier for the Bark service.
33 const Scheme = "bark"
34
35 // ErrSetQueryFailed indicates a failure to set a configuration value from a query parameter.
36 var ErrSetQueryFailed = errors.New("failed to set query parameter")
37
38 // GetAPIURL constructs the full API URL for a given endpoint.
39 // The endpoint is appended to the configured host and path.
40 //
41 // Parameters:
42 // - endpoint: The API endpoint to access (e.g., "push", "register").
43 //
44 // Returns:
45 // - The complete URL string for the API request.
46 func (c *Config) GetAPIURL(endpoint string) string {
47 path := strings.Builder{}
48 if !strings.HasPrefix(c.Path, "/") {
49 path.WriteByte('/')
50 }
51
52 path.WriteString(c.Path)
53
54 if !strings.HasSuffix(path.String(), "/") {
55 path.WriteByte('/')
56 }
57
58 path.WriteString(endpoint)
59
60 apiURL := url.URL{
61 Scheme: c.Scheme,
62 Host: c.Host,
63 Path: path.String(),
64 }
65
66 return apiURL.String()
67 }
68
69 // GetURL returns a URL representation of the current configuration.
70 // This URL can be used to share or persist the service configuration.
71 //
72 // Returns:
73 // - A URL struct containing the configuration as query parameters.
74 func (c *Config) GetURL() *url.URL {
75 resolver := format.NewPropKeyResolver(c)
76
77 return c.getURL(&resolver)
78 }
79
80 // SetURL updates the configuration from a URL representation.
81 // The URL should contain the device key as the password and optional query parameters.
82 //
83 // Parameters:
84 // - serviceURL: URL containing the service configuration.
85 //
86 // Returns:
87 // - An error if the URL is invalid or missing required parameters.
88 func (c *Config) SetURL(serviceURL *url.URL) error {
89 resolver := format.NewPropKeyResolver(c)
90
91 return c.setURL(&resolver, serviceURL)
92 }
93
94 // getURL generates a URL representation using the provided resolver.
95 // This internal method handles the actual URL construction logic.
96 //
97 // Parameters:
98 // - resolver: Configuration query resolver for building URL parameters.
99 //
100 // Returns:
101 // - A URL struct with embedded credentials and query parameters.
102 func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL {
103 return &url.URL{
104 User: url.UserPassword("", c.DeviceKey),
105 Host: c.Host,
106 Scheme: Scheme,
107 ForceQuery: true,
108 Path: c.Path,
109 RawQuery: format.BuildQuery(resolver),
110 }
111 }
112
113 // setURL parses a service URL and updates the configuration accordingly.
114 // It extracts the device key from the password field and host from the URL.
115 //
116 // Parameters:
117 // - resolver: Configuration query resolver for setting query parameters.
118 // - serviceURL: URL containing the service configuration.
119 //
120 // Returns:
121 // - An error if required fields are missing or query parameters are invalid.
122 func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error {
123 password, _ := serviceURL.User.Password()
124 c.DeviceKey = password
125 c.Host = serviceURL.Host
126 c.Path = serviceURL.Path
127
128 // Skip validation for dummy URLs used in docs generation
129 if serviceURL.Hostname() == "dummy.com" {
130 return nil
131 }
132
133 // Validate required fields
134 if c.DeviceKey == "" {
135 return ErrMissingDeviceKey
136 }
137
138 if c.Host == "" {
139 return ErrMissingHost
140 }
141
142 for key, vals := range serviceURL.Query() {
143 if err := resolver.Set(key, vals[0]); err != nil {
144 return fmt.Errorf("%w '%s': %w", ErrSetQueryFailed, key, err)
145 }
146 }
147
148 return nil
149 }