instapaper.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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/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 response.HasServerFailure() {
  28. return fmt.Errorf("instapaper: unable to send url, status=%d", response.StatusCode)
  29. }
  30. return err
  31. }
  32. // NewClient returns a new Instapaper client.
  33. func NewClient(username, password string) *Client {
  34. return &Client{username: username, password: password}
  35. }