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