espial.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package espial // import "miniflux.app/v2/internal/integration/espial"
  4. import (
  5. "fmt"
  6. "miniflux.app/v2/internal/http/client"
  7. "miniflux.app/v2/internal/urllib"
  8. )
  9. // Document structure of an Espial document
  10. type Document struct {
  11. Title string `json:"title,omitempty"`
  12. Url string `json:"url,omitempty"`
  13. ToRead bool `json:"toread,omitempty"`
  14. Tags string `json:"tags,omitempty"`
  15. }
  16. // Client represents an Espial client.
  17. type Client struct {
  18. baseURL string
  19. apiKey string
  20. }
  21. // NewClient returns a new Espial client.
  22. func NewClient(baseURL, apiKey string) *Client {
  23. return &Client{baseURL: baseURL, apiKey: apiKey}
  24. }
  25. // AddEntry sends an entry to Espial.
  26. func (c *Client) AddEntry(link, title, content, tags string) error {
  27. if c.baseURL == "" || c.apiKey == "" {
  28. return fmt.Errorf("espial: missing base URL or API key")
  29. }
  30. doc := &Document{
  31. Title: title,
  32. Url: link,
  33. ToRead: true,
  34. Tags: tags,
  35. }
  36. apiEndpoint, err := urllib.JoinBaseURLAndPath(c.baseURL, "/api/add")
  37. if err != nil {
  38. return fmt.Errorf(`espial: invalid API endpoint: %v`, err)
  39. }
  40. clt := client.New(apiEndpoint)
  41. clt.WithAuthorization("ApiKey " + c.apiKey)
  42. response, err := clt.PostJSON(doc)
  43. if err != nil {
  44. return fmt.Errorf("espial: unable to send entry: %v", err)
  45. }
  46. if response.HasServerFailure() {
  47. return fmt.Errorf("espial: unable to send entry, status=%d", response.StatusCode)
  48. }
  49. return nil
  50. }