raindrop.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package raindrop // import "miniflux.app/v2/internal/integration/raindrop"
  4. import (
  5. "errors"
  6. "fmt"
  7. "net/http"
  8. "strings"
  9. "miniflux.app/v2/internal/http/client"
  10. )
  11. type Client struct {
  12. token string
  13. collectionID string
  14. tags []string
  15. }
  16. func NewClient(token, collectionID, tags string) *Client {
  17. var tagList []string
  18. for tag := range strings.SplitSeq(tags, ",") {
  19. if trimmedTag := strings.TrimSpace(tag); trimmedTag != "" {
  20. tagList = append(tagList, trimmedTag)
  21. }
  22. }
  23. return &Client{token: token, collectionID: collectionID, tags: tagList}
  24. }
  25. // https://developer.raindrop.io/v1/raindrops/single#create-raindrop
  26. func (c *Client) CreateRaindrop(entryURL, entryTitle string) error {
  27. if c.token == "" {
  28. return errors.New("raindrop: missing token")
  29. }
  30. response, err := client.NewRequestBuilder("https://api.raindrop.io/rest/v1/raindrop").
  31. WithMethod(http.MethodPost).
  32. WithJSON(&raindrop{
  33. Link: entryURL,
  34. Title: entryTitle,
  35. Collection: collection{Id: c.collectionID},
  36. Tags: c.tags,
  37. }).
  38. WithHeader("Authorization", "Bearer "+c.token).
  39. Do()
  40. if err != nil {
  41. return fmt.Errorf("raindrop: %w", err)
  42. }
  43. defer response.Body.Close()
  44. if response.StatusCode >= 400 {
  45. return fmt.Errorf("raindrop: unable to create bookmark: status=%d", response.StatusCode)
  46. }
  47. return nil
  48. }
  49. type raindrop struct {
  50. Link string `json:"link"`
  51. Title string `json:"title"`
  52. Collection collection `json:"collection"`
  53. Tags []string `json:"tags,omitempty"`
  54. }
  55. type collection struct {
  56. Id string `json:"$id"`
  57. }