1
1
mirror of https://github.com/cooperspencer/gickup synced 2025-04-05 09:59:05 +02:00
gickup/metrics/apprise/apprise.go
Andreas Wachter 377378ce3a
add Apprise (#309)
* added apprise

* fixed yaml type, check for / at the end of url

* return error message

* adapted example config and gickup_spec

* re-format spec

* Revert "re-format spec"

This reverts commit b938a0587e52c6f2ebd2728ce97fed32fe5e5ff4.

* fix spec
2025-04-02 11:29:40 +02:00

66 lines
1.0 KiB
Go

package apprise
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strings"
"github.com/cooperspencer/gickup/types"
)
type Request struct {
Body string `json:"body"`
Tags []string `json:"tags,omitempty"`
Urls []string `json:"urls",omitempty`
}
type ErrorMsg struct {
Error string `json:"error"`
}
func Notify(msg string, config types.AppriseConfig) error {
payload := Request{
Body: msg,
Urls: config.Urls,
Tags: config.Tags,
}
jsonData, _ := json.Marshal(payload)
if !strings.HasSuffix(config.Url, "/") {
config.Url += "/"
}
url := config.Url + "notify/"
if config.Config != "" {
url += config.Config
}
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
defer resp.Body.Close()
if err != nil {
return err
}
errormsg := ErrorMsg{}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(body, &errormsg)
if err != nil {
return err
}
if errormsg.Error != "" {
return errors.New(errormsg.Error)
}
return nil
}