pocket.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package pocket // import "miniflux.app/v2/internal/integration/pocket"
  4. import (
  5. "fmt"
  6. "miniflux.app/v2/internal/http/client"
  7. )
  8. // Client represents a Pocket client.
  9. type Client struct {
  10. consumerKey string
  11. accessToken string
  12. }
  13. // NewClient returns a new Pocket client.
  14. func NewClient(consumerKey, accessToken string) *Client {
  15. return &Client{consumerKey, accessToken}
  16. }
  17. // AddURL sends a single link to Pocket.
  18. func (c *Client) AddURL(link, title string) error {
  19. if c.consumerKey == "" || c.accessToken == "" {
  20. return fmt.Errorf("pocket: missing credentials")
  21. }
  22. type body struct {
  23. AccessToken string `json:"access_token"`
  24. ConsumerKey string `json:"consumer_key"`
  25. Title string `json:"title,omitempty"`
  26. URL string `json:"url"`
  27. }
  28. data := &body{
  29. AccessToken: c.accessToken,
  30. ConsumerKey: c.consumerKey,
  31. Title: title,
  32. URL: link,
  33. }
  34. clt := client.New("https://getpocket.com/v3/add")
  35. response, err := clt.PostJSON(data)
  36. if err != nil {
  37. return fmt.Errorf("pocket: unable to send url: %v", err)
  38. }
  39. if response.HasServerFailure() {
  40. return fmt.Errorf("pocket: unable to send url, status=%d", response.StatusCode)
  41. }
  42. return nil
  43. }