|
|
@@ -7,6 +7,7 @@ import (
|
|
|
"bytes"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
+ "log/slog"
|
|
|
"net/http"
|
|
|
"time"
|
|
|
|
|
|
@@ -26,43 +27,53 @@ func NewClient(serviceURL, baseURL string) *Client {
|
|
|
return &Client{serviceURL, baseURL}
|
|
|
}
|
|
|
|
|
|
-func (c *Client) SendNotification(entry *model.Entry) error {
|
|
|
+func (c *Client) SendNotification(feed *model.Feed, entries model.Entries) error {
|
|
|
if c.baseURL == "" || c.servicesURL == "" {
|
|
|
return fmt.Errorf("apprise: missing base URL or services URL")
|
|
|
}
|
|
|
|
|
|
- message := "[" + entry.Title + "]" + "(" + entry.URL + ")" + "\n\n"
|
|
|
- apiEndpoint, err := urllib.JoinBaseURLAndPath(c.baseURL, "/notify")
|
|
|
- if err != nil {
|
|
|
- return fmt.Errorf(`apprise: invalid API endpoint: %v`, err)
|
|
|
- }
|
|
|
+ for _, entry := range entries {
|
|
|
+ message := "[" + entry.Title + "]" + "(" + entry.URL + ")" + "\n\n"
|
|
|
+ apiEndpoint, err := urllib.JoinBaseURLAndPath(c.baseURL, "/notify")
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf(`apprise: invalid API endpoint: %v`, err)
|
|
|
+ }
|
|
|
|
|
|
- requestBody, err := json.Marshal(map[string]any{
|
|
|
- "urls": c.servicesURL,
|
|
|
- "body": message,
|
|
|
- "title": entry.Feed.Title,
|
|
|
- })
|
|
|
- if err != nil {
|
|
|
- return fmt.Errorf("apprise: unable to encode request body: %v", err)
|
|
|
- }
|
|
|
+ requestBody, err := json.Marshal(map[string]any{
|
|
|
+ "urls": c.servicesURL,
|
|
|
+ "body": message,
|
|
|
+ "title": feed.Title,
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("apprise: unable to encode request body: %v", err)
|
|
|
+ }
|
|
|
|
|
|
- request, err := http.NewRequest(http.MethodPost, apiEndpoint, bytes.NewReader(requestBody))
|
|
|
- if err != nil {
|
|
|
- return fmt.Errorf("apprise: unable to create request: %v", err)
|
|
|
- }
|
|
|
+ request, err := http.NewRequest(http.MethodPost, apiEndpoint, bytes.NewReader(requestBody))
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("apprise: unable to create request: %v", err)
|
|
|
+ }
|
|
|
|
|
|
- request.Header.Set("Content-Type", "application/json")
|
|
|
- request.Header.Set("User-Agent", "Miniflux/"+version.Version)
|
|
|
+ request.Header.Set("Content-Type", "application/json")
|
|
|
+ request.Header.Set("User-Agent", "Miniflux/"+version.Version)
|
|
|
|
|
|
- httpClient := &http.Client{Timeout: defaultClientTimeout}
|
|
|
- response, err := httpClient.Do(request)
|
|
|
- if err != nil {
|
|
|
- return fmt.Errorf("apprise: unable to send request: %v", err)
|
|
|
- }
|
|
|
- defer response.Body.Close()
|
|
|
+ slog.Debug("Sending Apprise notification",
|
|
|
+ slog.String("apprise_url", c.baseURL),
|
|
|
+ slog.String("services_url", c.servicesURL),
|
|
|
+ slog.String("title", feed.Title),
|
|
|
+ slog.String("body", message),
|
|
|
+ slog.String("entry_url", entry.URL),
|
|
|
+ )
|
|
|
+
|
|
|
+ httpClient := &http.Client{Timeout: defaultClientTimeout}
|
|
|
+ response, err := httpClient.Do(request)
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("apprise: unable to send request: %v", err)
|
|
|
+ }
|
|
|
+ defer response.Body.Close()
|
|
|
|
|
|
- if response.StatusCode >= 400 {
|
|
|
- return fmt.Errorf("apprise: unable to send a notification: url=%s status=%d", apiEndpoint, response.StatusCode)
|
|
|
+ if response.StatusCode >= 400 {
|
|
|
+ return fmt.Errorf("apprise: unable to send a notification: url=%s status=%d", apiEndpoint, response.StatusCode)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return nil
|