instapaper.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 instapaper // import "miniflux.app/integration/instapaper"
  5. import (
  6. "fmt"
  7. "net/url"
  8. "miniflux.app/http/client"
  9. )
  10. // Client represents an Instapaper client.
  11. type Client struct {
  12. username string
  13. password string
  14. }
  15. // NewClient returns a new Instapaper client.
  16. func NewClient(username, password string) *Client {
  17. return &Client{username: username, password: password}
  18. }
  19. // AddURL sends a link to Instapaper.
  20. func (c *Client) AddURL(link, title string) error {
  21. if c.username == "" || c.password == "" {
  22. return fmt.Errorf("instapaper: missing credentials")
  23. }
  24. values := url.Values{}
  25. values.Add("url", link)
  26. values.Add("title", title)
  27. apiURL := "https://www.instapaper.com/api/add?" + values.Encode()
  28. clt := client.New(apiURL)
  29. clt.WithCredentials(c.username, c.password)
  30. response, err := clt.Get()
  31. if err != nil {
  32. return fmt.Errorf("instapaper: unable to send url: %v", err)
  33. }
  34. if response.HasServerFailure() {
  35. return fmt.Errorf("instapaper: unable to send url, status=%d", response.StatusCode)
  36. }
  37. return nil
  38. }