mirror of
https://github.com/woodchen-ink/certimate.git
synced 2025-07-18 17:31:55 +08:00
fix(notify): optimize pushplus code and close unreleased resources.
This commit is contained in:
parent
b585782007
commit
9ab029a296
@ -19,18 +19,6 @@ type NotifierConfig struct {
|
|||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Message PushPlus 消息体
|
|
||||||
type Message struct {
|
|
||||||
Token string `json:"token"`
|
|
||||||
Title string `json:"title"`
|
|
||||||
Content string `json:"content"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ErrorResponse struct {
|
|
||||||
Code int `json:"code"`
|
|
||||||
Msg string `json:"msg"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type NotifierProvider struct {
|
type NotifierProvider struct {
|
||||||
config *NotifierConfig
|
config *NotifierConfig
|
||||||
logger *slog.Logger
|
logger *slog.Logger
|
||||||
@ -64,7 +52,11 @@ func (n *NotifierProvider) WithLogger(logger *slog.Logger) notifier.Notifier {
|
|||||||
// 参考文档:https://pushplus.plus/doc/guide/api.html
|
// 参考文档:https://pushplus.plus/doc/guide/api.html
|
||||||
func (n *NotifierProvider) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
|
func (n *NotifierProvider) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
|
||||||
// 请求体
|
// 请求体
|
||||||
reqBody := &Message{
|
reqBody := &struct {
|
||||||
|
Token string `json:"token"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
}{
|
||||||
Token: n.config.Token,
|
Token: n.config.Token,
|
||||||
Title: subject,
|
Title: subject,
|
||||||
Content: message,
|
Content: message,
|
||||||
@ -93,6 +85,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "send request to pushplus server")
|
return nil, errors.Wrapf(err, "send request to pushplus server")
|
||||||
}
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
result, err := io.ReadAll(resp.Body)
|
result, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -104,7 +97,10 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 解析响应
|
// 解析响应
|
||||||
var errorResponse ErrorResponse
|
var errorResponse struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Msg string `json:"msg"`
|
||||||
|
}
|
||||||
if err := json.Unmarshal(result, &errorResponse); err != nil {
|
if err := json.Unmarshal(result, &errorResponse); err != nil {
|
||||||
return nil, errors.Wrap(err, "decode response")
|
return nil, errors.Wrap(err, "decode response")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user