linkwarden.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package linkwarden // import "miniflux.app/v2/internal/integration/linkwarden"
  4. import (
  5. "bytes"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "net/http"
  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. collectionID *int64
  22. }
  23. type linkwardenCollection struct {
  24. ID *int64 `json:"id"`
  25. }
  26. type linkwardenRequest struct {
  27. URL string `json:"url"`
  28. Name string `json:"name"`
  29. Collection *linkwardenCollection `json:"collection,omitempty"`
  30. }
  31. func NewClient(baseURL, apiKey string, collectionID *int64) *Client {
  32. return &Client{baseURL: baseURL, apiKey: apiKey, collectionID: collectionID}
  33. }
  34. func (c *Client) CreateBookmark(entryURL, entryTitle string) error {
  35. if c.baseURL == "" || c.apiKey == "" {
  36. return errors.New("linkwarden: missing base URL or API key")
  37. }
  38. apiEndpoint, err := urllib.JoinBaseURLAndPath(c.baseURL, "/api/v1/links")
  39. if err != nil {
  40. return fmt.Errorf(`linkwarden: invalid API endpoint: %v`, err)
  41. }
  42. payload := linkwardenRequest{
  43. URL: entryURL,
  44. Name: entryTitle,
  45. }
  46. if c.collectionID != nil {
  47. payload.Collection = &linkwardenCollection{ID: c.collectionID}
  48. }
  49. requestBody, err := json.Marshal(payload)
  50. if err != nil {
  51. return fmt.Errorf("linkwarden: unable to encode request body: %v", err)
  52. }
  53. request, err := http.NewRequest(http.MethodPost, apiEndpoint, bytes.NewReader(requestBody))
  54. if err != nil {
  55. return fmt.Errorf("linkwarden: unable to create request: %v", err)
  56. }
  57. request.Header.Set("Content-Type", "application/json")
  58. request.Header.Set("User-Agent", "Miniflux/"+version.Version)
  59. request.Header.Set("Authorization", "Bearer "+c.apiKey)
  60. httpClient := client.NewClientWithOptions(client.Options{Timeout: defaultClientTimeout, BlockPrivateNetworks: !config.Opts.IntegrationAllowPrivateNetworks()})
  61. response, err := httpClient.Do(request)
  62. if err != nil {
  63. return fmt.Errorf("linkwarden: unable to send request: %v", err)
  64. }
  65. defer response.Body.Close()
  66. responseBody, err := io.ReadAll(response.Body)
  67. if err != nil {
  68. return fmt.Errorf("linkwarden: unable to read response body: %v", err)
  69. }
  70. if response.StatusCode >= 400 {
  71. return fmt.Errorf("linkwarden: unable to create link: status=%d body=%s", response.StatusCode, string(responseBody))
  72. }
  73. return nil
  74. }