2025-06-04 08:37:11 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package karakeep // import "miniflux.app/v2/internal/integration/karakeep"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2025-09-28 20:37:05 +02:00
|
|
|
"strings"
|
2025-06-04 08:37:11 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"miniflux.app/v2/internal/version"
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultClientTimeout = 10 * time.Second
|
|
|
|
|
2025-09-28 20:37:05 +02:00
|
|
|
type Client struct {
|
|
|
|
wrapped *http.Client
|
|
|
|
apiEndpoint string
|
|
|
|
apiToken string
|
|
|
|
tags string
|
|
|
|
}
|
|
|
|
|
|
|
|
type tagItem struct {
|
|
|
|
TagName string `json:"tagName"`
|
2025-06-04 08:37:11 -04:00
|
|
|
}
|
|
|
|
|
2025-06-08 17:42:23 -07:00
|
|
|
type saveURLPayload struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
URL string `json:"url"`
|
2025-06-04 08:37:11 -04:00
|
|
|
}
|
|
|
|
|
2025-09-28 20:37:05 +02:00
|
|
|
type saveURLResponse struct {
|
|
|
|
ID string `json:"id"`
|
2025-06-04 08:37:11 -04:00
|
|
|
}
|
|
|
|
|
2025-09-28 20:37:05 +02:00
|
|
|
type attachTagsPayload struct {
|
|
|
|
Tags []tagItem `json:"tags"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type errorResponse struct {
|
|
|
|
Code string `json:"code"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(apiToken string, apiEndpoint string, tags string) *Client {
|
|
|
|
return &Client{wrapped: &http.Client{Timeout: defaultClientTimeout}, apiEndpoint: apiEndpoint, apiToken: apiToken, tags: tags}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) attachTags(entryID string) error {
|
|
|
|
if c.tags == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tagItems := make([]tagItem, 0)
|
|
|
|
for tag := range strings.SplitSeq(c.tags, ",") {
|
|
|
|
if trimmedTag := strings.TrimSpace(tag); trimmedTag != "" {
|
|
|
|
tagItems = append(tagItems, tagItem{TagName: trimmedTag})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tagItems) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tagRequestBody, err := json.Marshal(&attachTagsPayload{
|
|
|
|
Tags: tagItems,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("karakeep: unable to encode tag request body: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tagRequest, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/%s/tags", c.apiEndpoint, entryID), bytes.NewReader(tagRequestBody))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("karakeep: unable to create tag request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tagRequest.Header.Set("Authorization", "Bearer "+c.apiToken)
|
|
|
|
tagRequest.Header.Set("Content-Type", "application/json")
|
|
|
|
tagRequest.Header.Set("User-Agent", "Miniflux/"+version.Version)
|
|
|
|
|
|
|
|
tagResponse, err := c.wrapped.Do(tagRequest)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("karakeep: unable to send tag request: %v", err)
|
|
|
|
}
|
|
|
|
defer tagResponse.Body.Close()
|
|
|
|
|
|
|
|
if tagResponse.StatusCode != http.StatusOK && tagResponse.StatusCode != http.StatusCreated {
|
|
|
|
tagResponseBody, err := io.ReadAll(tagResponse.Body)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("karakeep: failed to parse tag response: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var errResponse errorResponse
|
|
|
|
if err := json.Unmarshal(tagResponseBody, &errResponse); err != nil {
|
|
|
|
return fmt.Errorf("karakeep: unable to parse tag error response: status=%d body=%s", tagResponse.StatusCode, string(tagResponseBody))
|
|
|
|
}
|
|
|
|
return fmt.Errorf("karakeep: failed to attach tags: status=%d errorcode=%s %s", tagResponse.StatusCode, errResponse.Code, errResponse.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2025-06-04 08:37:11 -04:00
|
|
|
}
|
|
|
|
|
2025-06-08 17:42:23 -07:00
|
|
|
func (c *Client) SaveURL(entryURL string) error {
|
|
|
|
requestBody, err := json.Marshal(&saveURLPayload{
|
|
|
|
Type: "link",
|
|
|
|
URL: entryURL,
|
|
|
|
})
|
2025-06-04 08:37:11 -04:00
|
|
|
if err != nil {
|
2025-06-08 17:42:23 -07:00
|
|
|
return fmt.Errorf("karakeep: unable to encode request body: %v", err)
|
2025-06-04 08:37:11 -04:00
|
|
|
}
|
2025-06-08 17:42:23 -07:00
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPost, c.apiEndpoint, bytes.NewReader(requestBody))
|
2025-06-04 08:37:11 -04:00
|
|
|
if err != nil {
|
2025-06-08 17:42:23 -07:00
|
|
|
return fmt.Errorf("karakeep: unable to create request: %v", err)
|
2025-06-04 08:37:11 -04:00
|
|
|
}
|
|
|
|
|
2025-09-08 20:54:16 +02:00
|
|
|
req.Header.Set("Authorization", "Bearer "+c.apiToken)
|
2025-06-04 08:37:11 -04:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Header.Set("User-Agent", "Miniflux/"+version.Version)
|
|
|
|
|
|
|
|
resp, err := c.wrapped.Do(req)
|
|
|
|
if err != nil {
|
2025-06-08 17:42:23 -07:00
|
|
|
return fmt.Errorf("karakeep: unable to send request: %v", err)
|
2025-06-04 08:37:11 -04:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2025-06-08 17:42:23 -07:00
|
|
|
|
|
|
|
responseBody, err := io.ReadAll(resp.Body)
|
2025-06-04 08:37:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("karakeep: failed to parse response: %s", err)
|
|
|
|
}
|
|
|
|
|
2025-06-08 17:42:23 -07:00
|
|
|
if resp.Header.Get("Content-Type") != "application/json" {
|
|
|
|
return fmt.Errorf("karakeep: unexpected content type response: %s", resp.Header.Get("Content-Type"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusCreated {
|
2025-06-04 08:37:11 -04:00
|
|
|
var errResponse errorResponse
|
2025-06-08 17:42:23 -07:00
|
|
|
if err := json.Unmarshal(responseBody, &errResponse); err != nil {
|
|
|
|
return fmt.Errorf("karakeep: unable to parse error response: status=%d body=%s", resp.StatusCode, string(responseBody))
|
2025-06-04 08:37:11 -04:00
|
|
|
}
|
|
|
|
return fmt.Errorf("karakeep: failed to save URL: status=%d errorcode=%s %s", resp.StatusCode, errResponse.Code, errResponse.Error)
|
|
|
|
}
|
|
|
|
|
2025-09-28 20:37:05 +02:00
|
|
|
var response saveURLResponse
|
|
|
|
if err := json.Unmarshal(responseBody, &response); err != nil {
|
|
|
|
return fmt.Errorf("karakeep: unable to parse response: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.ID == "" {
|
|
|
|
return fmt.Errorf("karakeep: unable to get ID from response")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.attachTags(response.ID); err != nil {
|
|
|
|
return fmt.Errorf("karakeep: unable to attach tags: %v", err)
|
|
|
|
}
|
|
|
|
|
2025-06-04 08:37:11 -04:00
|
|
|
return nil
|
|
|
|
}
|