pinboard.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package pinboard // import "miniflux.app/v2/internal/integration/pinboard"
  4. import (
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "time"
  9. "miniflux.app/v2/internal/version"
  10. )
  11. const defaultClientTimeout = 10 * time.Second
  12. type Client struct {
  13. authToken string
  14. }
  15. func NewClient(authToken string) *Client {
  16. return &Client{authToken: authToken}
  17. }
  18. func (c *Client) CreateBookmark(entryURL, entryTitle, pinboardTags string, markAsUnread bool) error {
  19. if c.authToken == "" {
  20. return fmt.Errorf("pinboard: missing auth token")
  21. }
  22. toRead := "no"
  23. if markAsUnread {
  24. toRead = "yes"
  25. }
  26. values := url.Values{}
  27. values.Add("auth_token", c.authToken)
  28. values.Add("url", entryURL)
  29. values.Add("description", entryTitle)
  30. values.Add("tags", pinboardTags)
  31. values.Add("toread", toRead)
  32. apiEndpoint := "https://api.pinboard.in/v1/posts/add?" + values.Encode()
  33. request, err := http.NewRequest(http.MethodGet, apiEndpoint, nil)
  34. if err != nil {
  35. return fmt.Errorf("pinboard: unable to create request: %v", err)
  36. }
  37. request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  38. request.Header.Set("User-Agent", "Miniflux/"+version.Version)
  39. httpClient := &http.Client{Timeout: defaultClientTimeout}
  40. response, err := httpClient.Do(request)
  41. if err != nil {
  42. return fmt.Errorf("pinboard: unable to send request: %v", err)
  43. }
  44. defer response.Body.Close()
  45. if response.StatusCode >= 400 {
  46. return fmt.Errorf("pinboard: unable to create a bookmark: url=%s status=%d", apiEndpoint, response.StatusCode)
  47. }
  48. return nil
  49. }