2017-12-03 00:12:03 -05:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
package instapaper // import "miniflux.app/integration/instapaper"
|
2017-12-03 00:12:03 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
"miniflux.app/http/client"
|
2017-12-03 00:12:03 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client represents an Instapaper client.
|
|
|
|
type Client struct {
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
}
|
|
|
|
|
2021-09-07 23:28:41 -04:00
|
|
|
// NewClient returns a new Instapaper client.
|
|
|
|
func NewClient(username, password string) *Client {
|
|
|
|
return &Client{username: username, password: password}
|
|
|
|
}
|
|
|
|
|
2017-12-03 00:12:03 -05:00
|
|
|
// AddURL sends a link to Instapaper.
|
|
|
|
func (c *Client) AddURL(link, title string) error {
|
2018-04-29 20:58:09 -04:00
|
|
|
if c.username == "" || c.password == "" {
|
|
|
|
return fmt.Errorf("instapaper: missing credentials")
|
|
|
|
}
|
|
|
|
|
2017-12-03 00:12:03 -05:00
|
|
|
values := url.Values{}
|
|
|
|
values.Add("url", link)
|
|
|
|
values.Add("title", title)
|
|
|
|
|
|
|
|
apiURL := "https://www.instapaper.com/api/add?" + values.Encode()
|
2018-04-28 13:51:07 -04:00
|
|
|
clt := client.New(apiURL)
|
|
|
|
clt.WithCredentials(c.username, c.password)
|
|
|
|
response, err := clt.Get()
|
2018-05-21 15:24:48 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("instapaper: unable to send url: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-12-03 00:12:03 -05:00
|
|
|
if response.HasServerFailure() {
|
2017-12-18 23:52:46 -05:00
|
|
|
return fmt.Errorf("instapaper: unable to send url, status=%d", response.StatusCode)
|
2017-12-03 00:12:03 -05:00
|
|
|
}
|
|
|
|
|
2018-05-21 15:24:48 -04:00
|
|
|
return nil
|
2017-12-03 00:12:03 -05:00
|
|
|
}
|