| 1 | package gotify |
| 2 | |
| 3 | import "net/url" |
| 4 | |
| 5 | // URLBuilder handles URL construction for Gotify API endpoints. |
| 6 | type URLBuilder interface { |
| 7 | BuildURL(config *Config) (string, error) |
| 8 | } |
| 9 | |
| 10 | // DefaultURLBuilder provides the default implementation of URLBuilder. |
| 11 | type DefaultURLBuilder struct{} |
| 12 | |
| 13 | // BuildURL constructs the Gotify API URL with scheme, host, path, and token. |
| 14 | // This function builds the complete endpoint URL for the Gotify message API, handling |
| 15 | // different authentication methods (header vs query parameter) and TLS settings. |
| 16 | // The URL format depends on whether header authentication is enabled. |
| 17 | // Parameters: |
| 18 | // - config: Configuration containing host, path, token, and authentication settings |
| 19 | // |
| 20 | // Returns: complete API URL string. |
| 21 | func (b *DefaultURLBuilder) BuildURL(config *Config) (string, error) { |
| 22 | // Determine URL scheme based on TLS settings |
| 23 | scheme := "https" |
| 24 | if config.DisableTLS { |
| 25 | scheme = "http" // Use HTTP scheme when TLS verification is disabled |
| 26 | } |
| 27 | |
| 28 | // Construct URL using url.URL for proper encoding |
| 29 | apiURL := &url.URL{ |
| 30 | Scheme: scheme, |
| 31 | Host: config.Host, |
| 32 | Path: config.Path + "/message", |
| 33 | } |
| 34 | |
| 35 | if !config.UseHeader { |
| 36 | // Query parameter authentication: include token in URL query string |
| 37 | q := apiURL.Query() |
| 38 | q.Set("token", config.Token) |
| 39 | apiURL.RawQuery = q.Encode() |
| 40 | } |
| 41 | |
| 42 | return apiURL.String(), nil |
| 43 | } |