aboutsummaryrefslogtreecommitdiff
path: root/internal/integration/espial/espial.go
blob: b7a9cc70ffa92a8c834598924eb162cbd18dc70b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package espial // import "miniflux.app/v2/internal/integration/espial"

import (
	"fmt"

	"miniflux.app/v2/internal/http/client"
	"miniflux.app/v2/internal/urllib"
)

// Document structure of an Espial document
type Document struct {
	Title  string `json:"title,omitempty"`
	Url    string `json:"url,omitempty"`
	ToRead bool   `json:"toread,omitempty"`
	Tags   string `json:"tags,omitempty"`
}

// Client represents an Espial client.
type Client struct {
	baseURL string
	apiKey  string
}

// NewClient returns a new Espial client.
func NewClient(baseURL, apiKey string) *Client {
	return &Client{baseURL: baseURL, apiKey: apiKey}
}

// AddEntry sends an entry to Espial.
func (c *Client) AddEntry(link, title, content, tags string) error {
	if c.baseURL == "" || c.apiKey == "" {
		return fmt.Errorf("espial: missing base URL or API key")
	}

	doc := &Document{
		Title:  title,
		Url:    link,
		ToRead: true,
		Tags:   tags,
	}

	apiEndpoint, err := urllib.JoinBaseURLAndPath(c.baseURL, "/api/add")
	if err != nil {
		return fmt.Errorf(`espial: invalid API endpoint: %v`, err)
	}

	clt := client.New(apiEndpoint)
	clt.WithAuthorization("ApiKey " + c.apiKey)
	response, err := clt.PostJSON(doc)
	if err != nil {
		return fmt.Errorf("espial: unable to send entry: %v", err)
	}

	if response.HasServerFailure() {
		return fmt.Errorf("espial: unable to send entry, status=%d", response.StatusCode)
	}

	return nil
}