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