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. // AddURL sends a single link to Pocket.
  15. func (c *Client) AddURL(link, title string) error {
  16. if c.consumerKey == "" || c.accessToken == "" {
  17. return fmt.Errorf("pocket: missing credentials")
  18. }
  19. type body struct {
  20. AccessToken string `json:"access_token"`
  21. ConsumerKey string `json:"consumer_key"`
  22. Title string `json:"title,omitempty"`
  23. URL string `json:"url"`
  24. }
  25. data := &body{
  26. AccessToken: c.accessToken,
  27. ConsumerKey: c.consumerKey,
  28. Title: title,
  29. URL: link,
  30. }
  31. clt := client.New("https://getpocket.com/v3/add")
  32. response, err := clt.PostJSON(data)
  33. if err != nil {
  34. return fmt.Errorf("pocket: unable to send url: %v", err)
  35. }
  36. if response.HasServerFailure() {
  37. return fmt.Errorf("pocket: unable to send url, status=%d", response.StatusCode)
  38. }
  39. return nil
  40. }
  41. // NewClient returns a new Pocket client.
  42. func NewClient(consumerKey, accessToken string) *Client {
  43. return &Client{consumerKey, accessToken}
  44. }