pocket.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package pocket // import "miniflux.app/integration/pocket"
  5. import (
  6. "fmt"
  7. "miniflux.app/http/client"
  8. )
  9. // Client represents a Pocket client.
  10. type Client struct {
  11. consumerKey string
  12. accessToken string
  13. }
  14. // NewClient returns a new Pocket client.
  15. func NewClient(consumerKey, accessToken string) *Client {
  16. return &Client{consumerKey, accessToken}
  17. }
  18. // AddURL sends a single link to Pocket.
  19. func (c *Client) AddURL(link, title string) error {
  20. if c.consumerKey == "" || c.accessToken == "" {
  21. return fmt.Errorf("pocket: missing credentials")
  22. }
  23. type body struct {
  24. AccessToken string `json:"access_token"`
  25. ConsumerKey string `json:"consumer_key"`
  26. Title string `json:"title,omitempty"`
  27. URL string `json:"url"`
  28. }
  29. data := &body{
  30. AccessToken: c.accessToken,
  31. ConsumerKey: c.consumerKey,
  32. Title: title,
  33. URL: link,
  34. }
  35. clt := client.New("https://getpocket.com/v3/add")
  36. response, err := clt.PostJSON(data)
  37. if err != nil {
  38. return fmt.Errorf("pocket: unable to send url: %v", err)
  39. }
  40. if response.HasServerFailure() {
  41. return fmt.Errorf("pocket: unable to send url, status=%d", response.StatusCode)
  42. }
  43. return nil
  44. }