mirror of
https://github.com/cooperspencer/gickup
synced 2025-04-05 09:59:05 +02:00
* 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
66 lines
1.0 KiB
Go
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
|
|
}
|