2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-12-18 23:52:46 -05:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
package wallabag // import "miniflux.app/v2/internal/integration/wallabag"
|
2017-12-18 23:52:46 -05:00
|
|
|
|
|
|
|
import (
|
2023-08-14 00:58:45 -04:00
|
|
|
"bytes"
|
2017-12-18 23:52:46 -05:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-08-14 00:58:45 -04:00
|
|
|
"net/http"
|
2017-12-18 23:52:46 -05:00
|
|
|
"net/url"
|
2023-08-14 00:58:45 -04:00
|
|
|
"strings"
|
|
|
|
"time"
|
2017-12-18 23:52:46 -05:00
|
|
|
|
2023-08-13 22:09:01 -04:00
|
|
|
"miniflux.app/v2/internal/urllib"
|
2023-08-14 00:58:45 -04:00
|
|
|
"miniflux.app/v2/internal/version"
|
2017-12-18 23:52:46 -05:00
|
|
|
)
|
|
|
|
|
2023-08-14 00:58:45 -04:00
|
|
|
const defaultClientTimeout = 10 * time.Second
|
|
|
|
|
2017-12-18 23:52:46 -05:00
|
|
|
type Client struct {
|
|
|
|
baseURL string
|
|
|
|
clientID string
|
|
|
|
clientSecret string
|
|
|
|
username string
|
|
|
|
password string
|
2022-09-18 19:52:28 -04:00
|
|
|
onlyURL bool
|
2017-12-18 23:52:46 -05:00
|
|
|
}
|
|
|
|
|
2022-09-18 19:52:28 -04:00
|
|
|
func NewClient(baseURL, clientID, clientSecret, username, password string, onlyURL bool) *Client {
|
|
|
|
return &Client{baseURL, clientID, clientSecret, username, password, onlyURL}
|
2021-09-07 23:28:41 -04:00
|
|
|
}
|
|
|
|
|
2023-08-14 00:58:45 -04:00
|
|
|
func (c *Client) CreateEntry(entryURL, entryTitle, entryContent string) error {
|
2018-04-29 20:58:09 -04:00
|
|
|
if c.baseURL == "" || c.clientID == "" || c.clientSecret == "" || c.username == "" || c.password == "" {
|
2023-08-14 00:58:45 -04:00
|
|
|
return fmt.Errorf("wallabag: missing base URL, client ID, client secret, username or password")
|
2018-04-29 20:58:09 -04:00
|
|
|
}
|
|
|
|
|
2017-12-18 23:52:46 -05:00
|
|
|
accessToken, err := c.getAccessToken()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-14 00:58:45 -04:00
|
|
|
return c.createEntry(accessToken, entryURL, entryTitle, entryContent)
|
2017-12-18 23:52:46 -05:00
|
|
|
}
|
|
|
|
|
2023-08-14 00:58:45 -04:00
|
|
|
func (c *Client) createEntry(accessToken, entryURL, entryTitle, entryContent string) error {
|
|
|
|
apiEndpoint, err := urllib.JoinBaseURLAndPath(c.baseURL, "/api/entries.json")
|
2017-12-18 23:52:46 -05:00
|
|
|
if err != nil {
|
2023-08-12 22:01:22 -04:00
|
|
|
return fmt.Errorf("wallbag: unable to generate entries endpoint: %v", err)
|
2017-12-18 23:52:46 -05:00
|
|
|
}
|
|
|
|
|
2023-08-14 00:58:45 -04:00
|
|
|
if c.onlyURL {
|
|
|
|
entryContent = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
requestBody, err := json.Marshal(&createEntryRequest{
|
|
|
|
URL: entryURL,
|
|
|
|
Title: entryTitle,
|
|
|
|
Content: entryContent,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("wallbag: unable to encode request body: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
request, err := http.NewRequest(http.MethodPost, apiEndpoint, bytes.NewReader(requestBody))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("wallbag: unable to create request: %v", err)
|
2022-09-18 19:52:28 -04:00
|
|
|
}
|
|
|
|
|
2023-08-14 00:58:45 -04:00
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
|
request.Header.Set("Accept", "application/json")
|
|
|
|
request.Header.Set("User-Agent", "Miniflux/"+version.Version)
|
|
|
|
request.Header.Set("Authorization", "Bearer "+accessToken)
|
|
|
|
|
|
|
|
httpClient := &http.Client{Timeout: defaultClientTimeout}
|
|
|
|
response, err := httpClient.Do(request)
|
2017-12-18 23:52:46 -05:00
|
|
|
if err != nil {
|
2023-08-14 00:58:45 -04:00
|
|
|
return fmt.Errorf("wallabag: unable to send request: %v", err)
|
2017-12-18 23:52:46 -05:00
|
|
|
}
|
2023-08-14 00:58:45 -04:00
|
|
|
defer response.Body.Close()
|
2017-12-18 23:52:46 -05:00
|
|
|
|
2023-08-14 00:58:45 -04:00
|
|
|
if response.StatusCode >= 400 {
|
|
|
|
return fmt.Errorf("wallabag: unable to get access token: url=%s status=%d", apiEndpoint, response.StatusCode)
|
2017-12-18 23:52:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) getAccessToken() (string, error) {
|
|
|
|
values := url.Values{}
|
|
|
|
values.Add("grant_type", "password")
|
|
|
|
values.Add("client_id", c.clientID)
|
|
|
|
values.Add("client_secret", c.clientSecret)
|
|
|
|
values.Add("username", c.username)
|
|
|
|
values.Add("password", c.password)
|
|
|
|
|
2023-08-14 00:58:45 -04:00
|
|
|
apiEndpoint, err := urllib.JoinBaseURLAndPath(c.baseURL, "/oauth/v2/token")
|
2017-12-18 23:52:46 -05:00
|
|
|
if err != nil {
|
2023-08-12 22:01:22 -04:00
|
|
|
return "", fmt.Errorf("wallbag: unable to generate token endpoint: %v", err)
|
2017-12-18 23:52:46 -05:00
|
|
|
}
|
|
|
|
|
2023-08-14 00:58:45 -04:00
|
|
|
request, err := http.NewRequest(http.MethodPost, apiEndpoint, strings.NewReader(values.Encode()))
|
2017-12-18 23:52:46 -05:00
|
|
|
if err != nil {
|
2023-08-14 00:58:45 -04:00
|
|
|
return "", fmt.Errorf("wallbag: unable to create request: %v", err)
|
2017-12-18 23:52:46 -05:00
|
|
|
}
|
|
|
|
|
2023-08-14 00:58:45 -04:00
|
|
|
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
request.Header.Set("Accept", "application/json")
|
|
|
|
request.Header.Set("User-Agent", "Miniflux/"+version.Version)
|
2017-12-18 23:52:46 -05:00
|
|
|
|
2023-08-14 00:58:45 -04:00
|
|
|
httpClient := &http.Client{Timeout: defaultClientTimeout}
|
|
|
|
response, err := httpClient.Do(request)
|
2017-12-18 23:52:46 -05:00
|
|
|
if err != nil {
|
2023-08-14 00:58:45 -04:00
|
|
|
return "", fmt.Errorf("wallabag: unable to send request: %v", err)
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
if response.StatusCode >= 400 {
|
|
|
|
return "", fmt.Errorf("wallabag: unable to get access token: url=%s status=%d", apiEndpoint, response.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
var responseBody tokenResponse
|
|
|
|
if err := json.NewDecoder(response.Body).Decode(&responseBody); err != nil {
|
|
|
|
return "", fmt.Errorf("wallabag: unable to decode token response: %v", err)
|
2017-12-18 23:52:46 -05:00
|
|
|
}
|
|
|
|
|
2023-08-14 00:58:45 -04:00
|
|
|
return responseBody.AccessToken, nil
|
2017-12-18 23:52:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type tokenResponse struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
Expires int `json:"expires_in"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
Scope string `json:"scope"`
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
}
|
|
|
|
|
2023-08-14 00:58:45 -04:00
|
|
|
type createEntryRequest struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Content string `json:"content,omitempty"`
|
2017-12-18 23:52:46 -05:00
|
|
|
}
|