espial.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. "bytes"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "net/http"
  10. "time"
  11. "miniflux.app/v2/internal/config"
  12. "miniflux.app/v2/internal/http/client"
  13. "miniflux.app/v2/internal/urllib"
  14. "miniflux.app/v2/internal/version"
  15. )
  16. const defaultClientTimeout = 10 * time.Second
  17. type Client struct {
  18. baseURL string
  19. apiKey string
  20. }
  21. func NewClient(baseURL, apiKey string) *Client {
  22. return &Client{baseURL: baseURL, apiKey: apiKey}
  23. }
  24. func (c *Client) CreateLink(entryURL, entryTitle, espialTags string) error {
  25. if c.baseURL == "" || c.apiKey == "" {
  26. return errors.New("espial: missing base URL or API key")
  27. }
  28. apiEndpoint, err := urllib.JoinBaseURLAndPath(c.baseURL, "/api/add")
  29. if err != nil {
  30. return fmt.Errorf("espial: invalid API endpoint: %v", err)
  31. }
  32. requestBody, err := json.Marshal(&espialDocument{
  33. Title: entryTitle,
  34. URL: entryURL,
  35. ToRead: true,
  36. Tags: espialTags,
  37. })
  38. if err != nil {
  39. return fmt.Errorf("espial: unable to encode request body: %v", err)
  40. }
  41. request, err := http.NewRequest(http.MethodPost, apiEndpoint, bytes.NewReader(requestBody))
  42. if err != nil {
  43. return fmt.Errorf("espial: unable to create request: %v", err)
  44. }
  45. request.Header.Set("Content-Type", "application/json")
  46. request.Header.Set("User-Agent", "Miniflux/"+version.Version)
  47. request.Header.Set("Authorization", "ApiKey "+c.apiKey)
  48. httpClient := client.NewClientWithOptions(client.Options{Timeout: defaultClientTimeout, BlockPrivateNetworks: !config.Opts.IntegrationAllowPrivateNetworks()})
  49. response, err := httpClient.Do(request)
  50. if err != nil {
  51. return fmt.Errorf("espial: unable to send request: %v", err)
  52. }
  53. defer response.Body.Close()
  54. if response.StatusCode != http.StatusCreated {
  55. responseBody := new(bytes.Buffer)
  56. responseBody.ReadFrom(response.Body)
  57. return fmt.Errorf("espial: unable to create link: url=%s status=%d body=%s", apiEndpoint, response.StatusCode, responseBody.String())
  58. }
  59. return nil
  60. }
  61. type espialDocument struct {
  62. Title string `json:"title,omitempty"`
  63. URL string `json:"url,omitempty"`
  64. ToRead bool `json:"toread,omitempty"`
  65. Tags string `json:"tags,omitempty"`
  66. }