2023-08-01 05:55:17 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package apprise
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/http/client"
|
|
|
|
"miniflux.app/v2/internal/model"
|
2023-08-14 04:09:01 +02:00
|
|
|
"miniflux.app/v2/internal/urllib"
|
2023-08-01 05:55:17 +02:00
|
|
|
)
|
|
|
|
|
2023-08-13 04:01:22 +02:00
|
|
|
const defaultClientTimeout = 1 * time.Second
|
|
|
|
|
2023-08-01 05:55:17 +02:00
|
|
|
// Client represents a Apprise client.
|
|
|
|
type Client struct {
|
|
|
|
servicesURL string
|
|
|
|
baseURL string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient returns a new Apprise client.
|
|
|
|
func NewClient(serviceURL, baseURL string) *Client {
|
|
|
|
return &Client{serviceURL, baseURL}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PushEntry pushes entry to apprise
|
|
|
|
func (c *Client) PushEntry(entry *model.Entry) error {
|
|
|
|
if c.baseURL == "" || c.servicesURL == "" {
|
2023-08-13 04:01:22 +02:00
|
|
|
return fmt.Errorf("apprise: missing base URL or service URL")
|
2023-08-01 05:55:17 +02:00
|
|
|
}
|
2023-08-13 04:01:22 +02:00
|
|
|
_, err := net.DialTimeout("tcp", c.baseURL, defaultClientTimeout)
|
2023-08-01 05:55:17 +02:00
|
|
|
if err != nil {
|
2023-08-14 04:09:01 +02:00
|
|
|
apiEndpoint, err := urllib.JoinBaseURLAndPath(c.baseURL, "/notify")
|
2023-08-13 04:01:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(`apprise: invalid API endpoint: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
clt := client.New(apiEndpoint)
|
2023-08-01 05:55:17 +02:00
|
|
|
message := "[" + entry.Title + "]" + "(" + entry.URL + ")" + "\n\n"
|
|
|
|
data := &Data{
|
|
|
|
Urls: c.servicesURL,
|
|
|
|
Body: message,
|
|
|
|
}
|
|
|
|
response, error := clt.PostJSON(data)
|
|
|
|
if error != nil {
|
|
|
|
return fmt.Errorf("apprise: ending message failed: %v", error)
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.HasServerFailure() {
|
|
|
|
return fmt.Errorf("apprise: request failed, status=%d", response.StatusCode)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("%s %s %s", c.baseURL, "responding on port:", strings.Split(c.baseURL, ":")[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|