apprise.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package apprise
  4. import (
  5. "fmt"
  6. "net"
  7. "strings"
  8. "time"
  9. "miniflux.app/v2/http/client"
  10. "miniflux.app/v2/model"
  11. )
  12. // Client represents a Apprise client.
  13. type Client struct {
  14. servicesURL string
  15. baseURL string
  16. }
  17. // NewClient returns a new Apprise client.
  18. func NewClient(serviceURL, baseURL string) *Client {
  19. return &Client{serviceURL, baseURL}
  20. }
  21. // PushEntry pushes entry to apprise
  22. func (c *Client) PushEntry(entry *model.Entry) error {
  23. if c.baseURL == "" || c.servicesURL == "" {
  24. return fmt.Errorf("apprise: missing credentials")
  25. }
  26. timeout := time.Duration(1 * time.Second)
  27. _, err := net.DialTimeout("tcp", c.baseURL, timeout)
  28. if err != nil {
  29. clt := client.New(c.baseURL + "/notify")
  30. message := "[" + entry.Title + "]" + "(" + entry.URL + ")" + "\n\n"
  31. data := &Data{
  32. Urls: c.servicesURL,
  33. Body: message,
  34. }
  35. response, error := clt.PostJSON(data)
  36. if error != nil {
  37. return fmt.Errorf("apprise: ending message failed: %v", error)
  38. }
  39. if response.HasServerFailure() {
  40. return fmt.Errorf("apprise: request failed, status=%d", response.StatusCode)
  41. }
  42. } else {
  43. return fmt.Errorf("%s %s %s", c.baseURL, "responding on port:", strings.Split(c.baseURL, ":")[1])
  44. }
  45. return nil
  46. }