mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 04:42:33 +02:00
73 lines
1.4 KiB
Go
73 lines
1.4 KiB
Go
package notif
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/gotify/server/v2/model"
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
type (
|
|
GotifyClient struct {
|
|
ProviderBase
|
|
}
|
|
GotifyMessage model.MessageExternal
|
|
)
|
|
|
|
const gotifyMsgEndpoint = "/message"
|
|
|
|
func (client *GotifyClient) GetURL() string {
|
|
return client.URL + gotifyMsgEndpoint
|
|
}
|
|
|
|
// MarshalMessage implements Provider.
|
|
func (client *GotifyClient) MarshalMessage(logMsg *LogMessage) ([]byte, error) {
|
|
var priority int
|
|
|
|
switch logMsg.Level {
|
|
case zerolog.WarnLevel:
|
|
priority = 2
|
|
case zerolog.ErrorLevel:
|
|
priority = 5
|
|
case zerolog.FatalLevel, zerolog.PanicLevel:
|
|
priority = 8
|
|
}
|
|
|
|
body, err := logMsg.Body.Format(client.Format)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
msg := &GotifyMessage{
|
|
Title: logMsg.Title,
|
|
Message: string(body),
|
|
Priority: &priority,
|
|
}
|
|
|
|
if client.Format == LogFormatMarkdown {
|
|
msg.Extras = map[string]interface{}{
|
|
"client::display": map[string]string{
|
|
"contentType": "text/markdown",
|
|
},
|
|
}
|
|
}
|
|
|
|
data, err := json.Marshal(msg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return data, nil
|
|
}
|
|
|
|
// fmtError implements Provider.
|
|
func (client *GotifyClient) fmtError(respBody io.Reader) error {
|
|
var errm model.Error
|
|
err := json.NewDecoder(respBody).Decode(&errm)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to decode err response: %w", err)
|
|
}
|
|
return fmt.Errorf("%s: %s", errm.Error, errm.ErrorDescription)
|
|
}
|