instapaper.go 990 B

123456789101112131415161718192021222324252627282930313233343536373839
  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
  5. import (
  6. "fmt"
  7. "net/url"
  8. "github.com/miniflux/miniflux/http"
  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. values := url.Values{}
  18. values.Add("url", link)
  19. values.Add("title", title)
  20. apiURL := "https://www.instapaper.com/api/add?" + values.Encode()
  21. client := http.NewClientWithCredentials(apiURL, c.username, c.password)
  22. response, err := client.Get()
  23. if response.HasServerFailure() {
  24. return fmt.Errorf("instapaper: unable to send url, status=%d", response.StatusCode)
  25. }
  26. return err
  27. }
  28. // NewClient returns a new Instapaper client.
  29. func NewClient(username, password string) *Client {
  30. return &Client{username: username, password: password}
  31. }