mirror of
https://github.com/miniflux/v2.git
synced 2025-10-15 19:42:07 +00:00
feat(integration): add tags option for karakeep integration
fix missing comma Move tags attachment to own function
This commit is contained in:
parent
8adcaed29e
commit
6ed12151d2
28 changed files with 143 additions and 17 deletions
|
@ -9,6 +9,7 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"miniflux.app/v2/internal/version"
|
||||
|
@ -16,9 +17,15 @@ import (
|
|||
|
||||
const defaultClientTimeout = 10 * time.Second
|
||||
|
||||
type errorResponse struct {
|
||||
Code string `json:"code"`
|
||||
Error string `json:"error"`
|
||||
type Client struct {
|
||||
wrapped *http.Client
|
||||
apiEndpoint string
|
||||
apiToken string
|
||||
tags string
|
||||
}
|
||||
|
||||
type tagItem struct {
|
||||
TagName string `json:"tagName"`
|
||||
}
|
||||
|
||||
type saveURLPayload struct {
|
||||
|
@ -26,14 +33,75 @@ type saveURLPayload struct {
|
|||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
wrapped *http.Client
|
||||
apiEndpoint string
|
||||
apiToken string
|
||||
type saveURLResponse struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
func NewClient(apiToken string, apiEndpoint string) *Client {
|
||||
return &Client{wrapped: &http.Client{Timeout: defaultClientTimeout}, apiEndpoint: apiEndpoint, apiToken: apiToken}
|
||||
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
|
||||
}
|
||||
|
||||
func (c *Client) SaveURL(entryURL string) error {
|
||||
|
@ -77,5 +145,18 @@ func (c *Client) SaveURL(entryURL string) error {
|
|||
return fmt.Errorf("karakeep: failed to save URL: status=%d errorcode=%s %s", resp.StatusCode, errResponse.Code, errResponse.Error)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue