Adding upstream version 0.8.9.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
3b2c48b5e4
commit
c0c4addb85
285 changed files with 25880 additions and 0 deletions
92
pkg/services/bark/bark.go
Normal file
92
pkg/services/bark/bark.go
Normal file
|
@ -0,0 +1,92 @@
|
|||
package bark
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/format"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/types"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrFailedAPIRequest = errors.New("failed to make API request")
|
||||
ErrUnexpectedStatus = errors.New("unexpected status code")
|
||||
ErrUpdateParamsFailed = errors.New("failed to update config from params")
|
||||
)
|
||||
|
||||
// Service sends notifications to Bark.
|
||||
type Service struct {
|
||||
standard.Standard
|
||||
Config *Config
|
||||
pkr format.PropKeyResolver
|
||||
}
|
||||
|
||||
// Send transmits a notification message to Bark.
|
||||
func (service *Service) Send(message string, params *types.Params) error {
|
||||
config := service.Config
|
||||
|
||||
if err := service.pkr.UpdateConfigFromParams(config, params); err != nil {
|
||||
return fmt.Errorf("%w: %w", ErrUpdateParamsFailed, err)
|
||||
}
|
||||
|
||||
if err := service.sendAPI(config, message); err != nil {
|
||||
return fmt.Errorf("failed to send bark notification: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Initialize sets up the Service with configuration from configURL and assigns a logger.
|
||||
func (service *Service) Initialize(configURL *url.URL, logger types.StdLogger) error {
|
||||
service.SetLogger(logger)
|
||||
service.Config = &Config{}
|
||||
service.pkr = format.NewPropKeyResolver(service.Config)
|
||||
|
||||
_ = service.pkr.SetDefaultProps(service.Config)
|
||||
|
||||
return service.Config.setURL(&service.pkr, configURL)
|
||||
}
|
||||
|
||||
// GetID returns the identifier for the Bark service.
|
||||
func (service *Service) GetID() string {
|
||||
return Scheme
|
||||
}
|
||||
|
||||
func (service *Service) sendAPI(config *Config, message string) error {
|
||||
response := APIResponse{}
|
||||
request := PushPayload{
|
||||
Body: message,
|
||||
DeviceKey: config.DeviceKey,
|
||||
Title: config.Title,
|
||||
Category: config.Category,
|
||||
Copy: config.Copy,
|
||||
Sound: config.Sound,
|
||||
Group: config.Group,
|
||||
Badge: &config.Badge,
|
||||
Icon: config.Icon,
|
||||
URL: config.URL,
|
||||
}
|
||||
jsonClient := jsonclient.NewClient()
|
||||
|
||||
if err := jsonClient.Post(config.GetAPIURL("push"), &request, &response); err != nil {
|
||||
if jsonClient.ErrorResponse(err, &response) {
|
||||
return &response
|
||||
}
|
||||
|
||||
return fmt.Errorf("%w: %w", ErrFailedAPIRequest, err)
|
||||
}
|
||||
|
||||
if response.Code != http.StatusOK {
|
||||
if response.Message != "" {
|
||||
return &response
|
||||
}
|
||||
|
||||
return fmt.Errorf("%w: %d", ErrUnexpectedStatus, response.Code)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
101
pkg/services/bark/bark_config.go
Normal file
101
pkg/services/bark/bark_config.go
Normal file
|
@ -0,0 +1,101 @@
|
|||
package bark
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/format"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/types"
|
||||
)
|
||||
|
||||
// Scheme is the identifying part of this service's configuration URL.
|
||||
const (
|
||||
Scheme = "bark"
|
||||
)
|
||||
|
||||
// ErrSetQueryFailed indicates a failure to set a configuration value from a query parameter.
|
||||
var ErrSetQueryFailed = errors.New("failed to set query parameter")
|
||||
|
||||
// Config holds configuration settings for the Bark service.
|
||||
type Config struct {
|
||||
standard.EnumlessConfig
|
||||
Title string `default:"" desc:"Notification title, optionally set by the sender" key:"title"`
|
||||
Host string ` desc:"Server hostname and port" url:"host"`
|
||||
Path string `default:"/" desc:"Server path" url:"path"`
|
||||
DeviceKey string ` desc:"The key for each device" url:"password"`
|
||||
Scheme string `default:"https" desc:"Server protocol, http or https" key:"scheme"`
|
||||
Sound string `default:"" desc:"Value from https://github.com/Finb/Bark/tree/master/Sounds" key:"sound"`
|
||||
Badge int64 `default:"0" desc:"The number displayed next to App icon" key:"badge"`
|
||||
Icon string `default:"" desc:"An url to the icon, available only on iOS 15 or later" key:"icon"`
|
||||
Group string `default:"" desc:"The group of the notification" key:"group"`
|
||||
URL string `default:"" desc:"Url that will jump when click notification" key:"url"`
|
||||
Category string `default:"" desc:"Reserved field, no use yet" key:"category"`
|
||||
Copy string `default:"" desc:"The value to be copied" key:"copy"`
|
||||
}
|
||||
|
||||
// GetURL returns a URL representation of the current configuration values.
|
||||
func (config *Config) GetURL() *url.URL {
|
||||
resolver := format.NewPropKeyResolver(config)
|
||||
|
||||
return config.getURL(&resolver)
|
||||
}
|
||||
|
||||
// SetURL updates the configuration from a URL representation.
|
||||
func (config *Config) SetURL(url *url.URL) error {
|
||||
resolver := format.NewPropKeyResolver(config)
|
||||
|
||||
return config.setURL(&resolver, url)
|
||||
}
|
||||
|
||||
// GetAPIURL constructs the API URL for the specified endpoint using the current configuration.
|
||||
func (config *Config) GetAPIURL(endpoint string) string {
|
||||
path := strings.Builder{}
|
||||
if !strings.HasPrefix(config.Path, "/") {
|
||||
path.WriteByte('/')
|
||||
}
|
||||
|
||||
path.WriteString(config.Path)
|
||||
|
||||
if !strings.HasSuffix(path.String(), "/") {
|
||||
path.WriteByte('/')
|
||||
}
|
||||
|
||||
path.WriteString(endpoint)
|
||||
|
||||
apiURL := url.URL{
|
||||
Scheme: config.Scheme,
|
||||
Host: config.Host,
|
||||
Path: path.String(),
|
||||
}
|
||||
|
||||
return apiURL.String()
|
||||
}
|
||||
|
||||
func (config *Config) getURL(resolver types.ConfigQueryResolver) *url.URL {
|
||||
return &url.URL{
|
||||
User: url.UserPassword("", config.DeviceKey),
|
||||
Host: config.Host,
|
||||
Scheme: Scheme,
|
||||
ForceQuery: true,
|
||||
Path: config.Path,
|
||||
RawQuery: format.BuildQuery(resolver),
|
||||
}
|
||||
}
|
||||
|
||||
func (config *Config) setURL(resolver types.ConfigQueryResolver, url *url.URL) error {
|
||||
password, _ := url.User.Password()
|
||||
config.DeviceKey = password
|
||||
config.Host = url.Host
|
||||
config.Path = url.Path
|
||||
|
||||
for key, vals := range url.Query() {
|
||||
if err := resolver.Set(key, vals[0]); err != nil {
|
||||
return fmt.Errorf("%w '%s': %w", ErrSetQueryFailed, key, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
29
pkg/services/bark/bark_json.go
Normal file
29
pkg/services/bark/bark_json.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package bark
|
||||
|
||||
// PushPayload represents the notification payload for the Bark notification service.
|
||||
type PushPayload struct {
|
||||
Body string `json:"body"`
|
||||
DeviceKey string `json:"device_key"`
|
||||
Title string `json:"title"`
|
||||
Sound string `json:"sound,omitempty"`
|
||||
Badge *int64 `json:"badge,omitempty"`
|
||||
Icon string `json:"icon,omitempty"`
|
||||
Group string `json:"group,omitempty"`
|
||||
URL string `json:"url,omitempty"`
|
||||
Category string `json:"category,omitempty"`
|
||||
Copy string `json:"copy,omitempty"`
|
||||
}
|
||||
|
||||
// APIResponse represents a response from the Bark API.
|
||||
//
|
||||
//nolint:errname
|
||||
type APIResponse struct {
|
||||
Code int64 `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
}
|
||||
|
||||
// Error returns the error message from the API response when applicable.
|
||||
func (e *APIResponse) Error() string {
|
||||
return "server response: " + e.Message
|
||||
}
|
181
pkg/services/bark/bark_test.go
Normal file
181
pkg/services/bark/bark_test.go
Normal file
|
@ -0,0 +1,181 @@
|
|||
package bark_test
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/jarcoal/httpmock"
|
||||
"github.com/onsi/ginkgo/v2"
|
||||
"github.com/onsi/gomega"
|
||||
"github.com/onsi/gomega/format"
|
||||
|
||||
"github.com/nicholas-fedor/shoutrrr/internal/testutils"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/services/bark"
|
||||
)
|
||||
|
||||
// TestBark runs the Ginkgo test suite for the bark package.
|
||||
func TestBark(t *testing.T) {
|
||||
format.CharactersAroundMismatchToInclude = 20 // Show more context in failure output
|
||||
|
||||
gomega.RegisterFailHandler(ginkgo.Fail)
|
||||
ginkgo.RunSpecs(t, "Shoutrrr Bark Suite")
|
||||
}
|
||||
|
||||
var (
|
||||
service *bark.Service = &bark.Service{} // Bark service instance for testing
|
||||
envBarkURL *url.URL // Environment-provided URL for integration tests
|
||||
logger *log.Logger = testutils.TestLogger() // Shared logger for tests
|
||||
_ = ginkgo.BeforeSuite(func() {
|
||||
// Load the integration test URL from environment, if available
|
||||
var err error
|
||||
envBarkURL, err = url.Parse(os.Getenv("SHOUTRRR_BARK_URL"))
|
||||
if err != nil {
|
||||
envBarkURL = &url.URL{} // Default to empty URL if parsing fails
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
var _ = ginkgo.Describe("the bark service", func() {
|
||||
ginkgo.When("running integration tests", func() {
|
||||
ginkgo.It("sends a message successfully with a valid ENV URL", func() {
|
||||
if envBarkURL.String() == "" {
|
||||
ginkgo.Skip("No integration test ENV URL was set")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
configURL := testutils.URLMust(envBarkURL.String())
|
||||
gomega.Expect(service.Initialize(configURL, logger)).To(gomega.Succeed())
|
||||
gomega.Expect(service.Send("This is an integration test message", nil)).
|
||||
To(gomega.Succeed())
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.Describe("the config", func() {
|
||||
ginkgo.When("getting an API URL", func() {
|
||||
ginkgo.It("constructs the expected URL for various path formats", func() {
|
||||
gomega.Expect(getAPIForPath("path")).To(gomega.Equal("https://host/path/endpoint"))
|
||||
gomega.Expect(getAPIForPath("/path")).To(gomega.Equal("https://host/path/endpoint"))
|
||||
gomega.Expect(getAPIForPath("/path/")).
|
||||
To(gomega.Equal("https://host/path/endpoint"))
|
||||
gomega.Expect(getAPIForPath("path/")).To(gomega.Equal("https://host/path/endpoint"))
|
||||
gomega.Expect(getAPIForPath("/")).To(gomega.Equal("https://host/endpoint"))
|
||||
gomega.Expect(getAPIForPath("")).To(gomega.Equal("https://host/endpoint"))
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.When("only required fields are set", func() {
|
||||
ginkgo.It("applies default values to optional fields", func() {
|
||||
serviceURL := testutils.URLMust("bark://:devicekey@hostname")
|
||||
gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed())
|
||||
gomega.Expect(*service.Config).To(gomega.Equal(bark.Config{
|
||||
Host: "hostname",
|
||||
DeviceKey: "devicekey",
|
||||
Scheme: "https",
|
||||
}))
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.When("parsing the configuration URL", func() {
|
||||
ginkgo.It("preserves all fields after de-/serialization", func() {
|
||||
testURL := "bark://:device-key@example.com:2225/?badge=5&category=CAT&group=GROUP&scheme=http&title=TITLE&url=URL"
|
||||
config := &bark.Config{}
|
||||
gomega.Expect(config.SetURL(testutils.URLMust(testURL))).
|
||||
To(gomega.Succeed(), "verifying")
|
||||
gomega.Expect(config.GetURL().String()).To(gomega.Equal(testURL))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.When("sending the push payload", func() {
|
||||
ginkgo.BeforeEach(func() {
|
||||
httpmock.Activate()
|
||||
})
|
||||
ginkgo.AfterEach(func() {
|
||||
httpmock.DeactivateAndReset()
|
||||
})
|
||||
|
||||
ginkgo.It("sends successfully when the server accepts the payload", func() {
|
||||
serviceURL := testutils.URLMust("bark://:devicekey@hostname")
|
||||
gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed())
|
||||
httpmock.RegisterResponder("POST", service.Config.GetAPIURL("push"),
|
||||
testutils.JSONRespondMust(200, bark.APIResponse{
|
||||
Code: http.StatusOK,
|
||||
Message: "OK",
|
||||
}))
|
||||
gomega.Expect(service.Send("Message", nil)).To(gomega.Succeed())
|
||||
})
|
||||
|
||||
ginkgo.It("reports an error for a server error response", func() {
|
||||
serviceURL := testutils.URLMust("bark://:devicekey@hostname")
|
||||
gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed())
|
||||
httpmock.RegisterResponder("POST", service.Config.GetAPIURL("push"),
|
||||
testutils.JSONRespondMust(500, bark.APIResponse{
|
||||
Code: 500,
|
||||
Message: "someone turned off the internet",
|
||||
}))
|
||||
gomega.Expect(service.Send("Message", nil)).To(gomega.HaveOccurred())
|
||||
})
|
||||
|
||||
ginkgo.It("handles an unexpected server response gracefully", func() {
|
||||
serviceURL := testutils.URLMust("bark://:devicekey@hostname")
|
||||
gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed())
|
||||
httpmock.RegisterResponder("POST", service.Config.GetAPIURL("push"),
|
||||
testutils.JSONRespondMust(200, bark.APIResponse{
|
||||
Code: 500,
|
||||
Message: "For some reason, the response code and HTTP code is different?",
|
||||
}))
|
||||
gomega.Expect(service.Send("Message", nil)).To(gomega.HaveOccurred())
|
||||
})
|
||||
|
||||
ginkgo.It("handles communication errors without panicking", func() {
|
||||
httpmock.DeactivateAndReset() // Ensure no mocks interfere
|
||||
serviceURL := testutils.URLMust("bark://:devicekey@nonresolvablehostname")
|
||||
gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed())
|
||||
gomega.Expect(service.Send("Message", nil)).To(gomega.HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.Describe("the basic service API", func() {
|
||||
ginkgo.Describe("the service config", func() {
|
||||
ginkgo.It("implements basic service config API methods correctly", func() {
|
||||
testutils.TestConfigGetInvalidQueryValue(&bark.Config{})
|
||||
testutils.TestConfigSetInvalidQueryValue(
|
||||
&bark.Config{},
|
||||
"bark://:mock-device@host/?foo=bar",
|
||||
)
|
||||
testutils.TestConfigSetDefaultValues(&bark.Config{})
|
||||
testutils.TestConfigGetEnumsCount(&bark.Config{}, 0)
|
||||
testutils.TestConfigGetFieldsCount(&bark.Config{}, 9)
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.Describe("the service instance", func() {
|
||||
ginkgo.BeforeEach(func() {
|
||||
httpmock.Activate()
|
||||
})
|
||||
ginkgo.AfterEach(func() {
|
||||
httpmock.DeactivateAndReset()
|
||||
})
|
||||
ginkgo.It("implements basic service API methods correctly", func() {
|
||||
serviceURL := testutils.URLMust("bark://:devicekey@hostname")
|
||||
gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed())
|
||||
testutils.TestServiceSetInvalidParamValue(service, "foo", "bar")
|
||||
})
|
||||
ginkgo.It("returns the correct service identifier", func() {
|
||||
// No initialization needed since GetID is static
|
||||
gomega.Expect(service.GetID()).To(gomega.Equal("bark"))
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// getAPIForPath is a helper to construct an API URL for testing.
|
||||
func getAPIForPath(path string) string {
|
||||
c := bark.Config{Host: "host", Path: path, Scheme: "https"}
|
||||
|
||||
return c.GetAPIURL("endpoint")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue