readwise.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. // Readwise Reader API documentation: https://readwise.io/reader_api
  4. package readwise // import "miniflux.app/v2/internal/integration/readwise"
  5. import (
  6. "errors"
  7. "fmt"
  8. "net/http"
  9. "miniflux.app/v2/internal/http/client"
  10. )
  11. const readwiseApiEndpoint = "https://readwise.io/api/v3/save/"
  12. type Client struct {
  13. apiKey string
  14. }
  15. func NewClient(apiKey string) *Client {
  16. return &Client{apiKey: apiKey}
  17. }
  18. func (c *Client) CreateDocument(entryURL string) error {
  19. if c.apiKey == "" {
  20. return errors.New("readwise: missing API key")
  21. }
  22. response, err := client.NewRequestBuilder(readwiseApiEndpoint).
  23. WithMethod(http.MethodPost).
  24. WithJSON(&readwiseDocument{
  25. URL: entryURL,
  26. }).
  27. WithHeader("Authorization", "Token "+c.apiKey).
  28. Do()
  29. if err != nil {
  30. return fmt.Errorf("readwise: %w", err)
  31. }
  32. defer response.Body.Close()
  33. if response.StatusCode >= 400 {
  34. return fmt.Errorf("readwise: unable to create document: url=%s status=%d", readwiseApiEndpoint, response.StatusCode)
  35. }
  36. return nil
  37. }
  38. type readwiseDocument struct {
  39. URL string `json:"url"`
  40. }