linkace.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package linkace // import "miniflux.app/v2/internal/integration/linkace"
  4. import (
  5. "bytes"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "net/http"
  10. "strings"
  11. "time"
  12. "miniflux.app/v2/internal/config"
  13. "miniflux.app/v2/internal/http/client"
  14. "miniflux.app/v2/internal/urllib"
  15. "miniflux.app/v2/internal/version"
  16. )
  17. const defaultClientTimeout = 10 * time.Second
  18. type Client struct {
  19. baseURL string
  20. apiKey string
  21. tags string
  22. private bool
  23. checkDisabled bool
  24. }
  25. func NewClient(baseURL, apiKey, tags string, private bool, checkDisabled bool) *Client {
  26. return &Client{baseURL: baseURL, apiKey: apiKey, tags: tags, private: private, checkDisabled: checkDisabled}
  27. }
  28. func (c *Client) AddURL(entryURL, entryTitle string) error {
  29. if c.baseURL == "" || c.apiKey == "" {
  30. return errors.New("linkace: missing base URL or API key")
  31. }
  32. tagsSplitFn := func(c rune) bool {
  33. return c == ',' || c == ' '
  34. }
  35. apiEndpoint, err := urllib.JoinBaseURLAndPath(c.baseURL, "/api/v2/links")
  36. if err != nil {
  37. return fmt.Errorf("linkace: invalid API endpoint: %v", err)
  38. }
  39. requestBody, err := json.Marshal(&createItemRequest{
  40. URL: entryURL,
  41. Title: entryTitle,
  42. Tags: strings.FieldsFunc(c.tags, tagsSplitFn),
  43. Private: c.private,
  44. CheckDisabled: c.checkDisabled,
  45. })
  46. if err != nil {
  47. return fmt.Errorf("linkace: unable to encode request body: %v", err)
  48. }
  49. request, err := http.NewRequest(http.MethodPost, apiEndpoint, bytes.NewReader(requestBody))
  50. if err != nil {
  51. return fmt.Errorf("linkace: unable to create request: %v", err)
  52. }
  53. request.Header.Set("Content-Type", "application/json")
  54. request.Header.Set("Accept", "application/json")
  55. request.Header.Set("User-Agent", "Miniflux/"+version.Version)
  56. request.Header.Set("Authorization", "Bearer "+c.apiKey)
  57. httpClient := client.NewClientWithOptions(client.Options{Timeout: defaultClientTimeout, BlockPrivateNetworks: !config.Opts.IntegrationAllowPrivateNetworks()})
  58. response, err := httpClient.Do(request)
  59. if err != nil {
  60. return fmt.Errorf("linkace: unable to send request: %v", err)
  61. }
  62. defer response.Body.Close()
  63. if response.StatusCode >= 400 {
  64. return fmt.Errorf("linkace: unable to create item: url=%s status=%d", apiEndpoint, response.StatusCode)
  65. }
  66. return nil
  67. }
  68. type createItemRequest struct {
  69. Title string `json:"title,omitempty"`
  70. URL string `json:"url"`
  71. Tags []string `json:"tags,omitempty"`
  72. Private bool `json:"is_private,omitempty"`
  73. CheckDisabled bool `json:"check_disabled,omitempty"`
  74. }