2017-12-03 06:12:03 +01: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 06:51:50 +02:00
|
|
|
package instapaper // import "miniflux.app/integration/instapaper"
|
2017-12-03 06:12:03 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/http/client"
|
2017-12-03 06:12:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client represents an Instapaper client.
|
|
|
|
type Client struct {
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
}
|
|
|
|
|
2021-09-08 05:28:41 +02:00
|
|
|
// NewClient returns a new Instapaper client.
|
|
|
|
func NewClient(username, password string) *Client {
|
|
|
|
return &Client{username: username, password: password}
|
|
|
|
}
|
|
|
|
|
2017-12-03 06:12:03 +01:00
|
|
|
// AddURL sends a link to Instapaper.
|
|
|
|
func (c *Client) AddURL(link, title string) error {
|
2018-04-30 02:58:09 +02:00
|
|
|
if c.username == "" || c.password == "" {
|
|
|
|
return fmt.Errorf("instapaper: missing credentials")
|
|
|
|
}
|
|
|
|
|
2017-12-03 06:12:03 +01:00
|
|
|
values := url.Values{}
|
|
|
|
values.Add("url", link)
|
|
|
|
values.Add("title", title)
|
|
|
|
|
|
|
|
apiURL := "https://www.instapaper.com/api/add?" + values.Encode()
|
2018-04-28 19:51:07 +02:00
|
|
|
clt := client.New(apiURL)
|
|
|
|
clt.WithCredentials(c.username, c.password)
|
|
|
|
response, err := clt.Get()
|
2018-05-21 21:24:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("instapaper: unable to send url: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-12-03 06:12:03 +01:00
|
|
|
if response.HasServerFailure() {
|
2017-12-19 05:52:46 +01:00
|
|
|
return fmt.Errorf("instapaper: unable to send url, status=%d", response.StatusCode)
|
2017-12-03 06:12:03 +01:00
|
|
|
}
|
|
|
|
|
2018-05-21 21:24:48 +02:00
|
|
|
return nil
|
2017-12-03 06:12:03 +01:00
|
|
|
}
|