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"
|
2023-08-14 06:58:45 +02:00
|
|
|
"net/http"
|
2017-12-03 06:12:03 +01:00
|
|
|
"net/url"
|
2023-08-14 06:58:45 +02:00
|
|
|
"time"
|
2017-12-03 06:12:03 +01:00
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
"miniflux.app/v2/internal/version"
|
2017-12-03 06:12:03 +01:00
|
|
|
)
|
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
const defaultClientTimeout = 10 * time.Second
|
|
|
|
|
2017-12-03 06:12:03 +01:00
|
|
|
type Client struct {
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
}
|
|
|
|
|
2021-09-08 05:28:41 +02:00
|
|
|
func NewClient(username, password string) *Client {
|
|
|
|
return &Client{username: username, password: password}
|
|
|
|
}
|
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
func (c *Client) AddURL(entryURL, entryTitle string) error {
|
2018-04-30 02:58:09 +02:00
|
|
|
if c.username == "" || c.password == "" {
|
2023-08-14 06:58:45 +02:00
|
|
|
return fmt.Errorf("instapaper: missing username or password")
|
2018-04-30 02:58:09 +02:00
|
|
|
}
|
|
|
|
|
2017-12-03 06:12:03 +01:00
|
|
|
values := url.Values{}
|
2023-08-14 06:58:45 +02:00
|
|
|
values.Add("url", entryURL)
|
|
|
|
values.Add("title", entryTitle)
|
|
|
|
|
|
|
|
apiEndpoint := "https://www.instapaper.com/api/add?" + values.Encode()
|
|
|
|
request, err := http.NewRequest(http.MethodGet, apiEndpoint, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("instapaper: unable to create request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
request.SetBasicAuth(c.username, c.password)
|
|
|
|
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
request.Header.Set("User-Agent", "Miniflux/"+version.Version)
|
2017-12-03 06:12:03 +01:00
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
httpClient := &http.Client{Timeout: defaultClientTimeout}
|
|
|
|
response, err := httpClient.Do(request)
|
2018-05-21 21:24:48 +02:00
|
|
|
if err != nil {
|
2023-08-14 06:58:45 +02:00
|
|
|
return fmt.Errorf("instapaper: unable to send request: %v", err)
|
2018-05-21 21:24:48 +02:00
|
|
|
}
|
2023-08-14 06:58:45 +02:00
|
|
|
defer response.Body.Close()
|
2018-05-21 21:24:48 +02:00
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
if response.StatusCode != http.StatusCreated {
|
|
|
|
return fmt.Errorf("instapaper: unable to add URL: url=%s status=%d", apiEndpoint, 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
|
|
|
}
|