pinboard.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2017 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 pinboard // import "miniflux.app/integration/pinboard"
  5. import (
  6. "fmt"
  7. "net/url"
  8. "miniflux.app/http/client"
  9. )
  10. // Client represents a Pinboard client.
  11. type Client struct {
  12. authToken string
  13. }
  14. // AddBookmark sends a link to Pinboard.
  15. func (c *Client) AddBookmark(link, title, tags string, markAsUnread bool) error {
  16. if c.authToken == "" {
  17. return fmt.Errorf("pinboard: missing credentials")
  18. }
  19. toRead := "no"
  20. if markAsUnread {
  21. toRead = "yes"
  22. }
  23. values := url.Values{}
  24. values.Add("auth_token", c.authToken)
  25. values.Add("url", link)
  26. values.Add("description", title)
  27. values.Add("tags", tags)
  28. values.Add("toread", toRead)
  29. clt := client.New("https://api.pinboard.in/v1/posts/add?" + values.Encode())
  30. response, err := clt.Get()
  31. if err != nil {
  32. return fmt.Errorf("pinboard: unable to send bookmark: %v", err)
  33. }
  34. if response.HasServerFailure() {
  35. return fmt.Errorf("pinboard: unable to send bookmark, status=%d", response.StatusCode)
  36. }
  37. return nil
  38. }
  39. // NewClient returns a new Pinboard client.
  40. func NewClient(authToken string) *Client {
  41. return &Client{authToken: authToken}
  42. }