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 04:32:14 +01:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package pinboard // import "miniflux.app/v2/internal/integration/pinboard"
|
2017-12-03 04:32:14 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-08-14 06:58:45 +02:00
|
|
|
"net/http"
|
2017-12-03 04:32:14 +01:00
|
|
|
"net/url"
|
2023-08-14 06:58:45 +02:00
|
|
|
"time"
|
2017-12-03 04:32:14 +01:00
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
"miniflux.app/v2/internal/version"
|
2017-12-03 04:32:14 +01:00
|
|
|
)
|
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
const defaultClientTimeout = 10 * time.Second
|
|
|
|
|
2017-12-03 04:32:14 +01:00
|
|
|
type Client struct {
|
|
|
|
authToken string
|
|
|
|
}
|
|
|
|
|
2021-09-08 05:28:41 +02:00
|
|
|
func NewClient(authToken string) *Client {
|
|
|
|
return &Client{authToken: authToken}
|
|
|
|
}
|
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
func (c *Client) CreateBookmark(entryURL, entryTitle, pinboardTags string, markAsUnread bool) error {
|
2018-04-30 02:58:09 +02:00
|
|
|
if c.authToken == "" {
|
2023-08-13 04:01:22 +02:00
|
|
|
return fmt.Errorf("pinboard: missing auth token")
|
2018-04-30 02:58:09 +02:00
|
|
|
}
|
|
|
|
|
2017-12-03 04:32:14 +01:00
|
|
|
toRead := "no"
|
|
|
|
if markAsUnread {
|
|
|
|
toRead = "yes"
|
|
|
|
}
|
|
|
|
|
|
|
|
values := url.Values{}
|
|
|
|
values.Add("auth_token", c.authToken)
|
2023-08-14 06:58:45 +02:00
|
|
|
values.Add("url", entryURL)
|
|
|
|
values.Add("description", entryTitle)
|
|
|
|
values.Add("tags", pinboardTags)
|
2017-12-03 04:32:14 +01:00
|
|
|
values.Add("toread", toRead)
|
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
apiEndpoint := "https://api.pinboard.in/v1/posts/add?" + values.Encode()
|
|
|
|
request, err := http.NewRequest(http.MethodGet, apiEndpoint, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("pinboard: unable to create request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
request.Header.Set("User-Agent", "Miniflux/"+version.Version)
|
|
|
|
|
|
|
|
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("pinboard: 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 >= 400 {
|
|
|
|
return fmt.Errorf("pinboard: unable to create a bookmark: url=%s status=%d", apiEndpoint, response.StatusCode)
|
2017-12-03 04:32:14 +01:00
|
|
|
}
|
|
|
|
|
2018-05-21 21:24:48 +02:00
|
|
|
return nil
|
2017-12-03 04:32:14 +01:00
|
|
|
}
|