2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-12-03 06:12:03 +01:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package instapaper // import "miniflux.app/v2/internal/integration/instapaper"
|
2017-12-03 06:12:03 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/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
|
|
|
}
|