aboutsummaryrefslogtreecommitdiffhomepage
path: root/integration/pinboard/pinboard.go
blob: d34e4fed22342894f9eb8a5fa5568ad6c36e8c1c (plain)
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
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package pinboard

import (
	"fmt"
	"net/url"

	"github.com/miniflux/miniflux/http/client"
)

// Client represents a Pinboard client.
type Client struct {
	authToken string
}

// AddBookmark sends a link to Pinboard.
func (c *Client) AddBookmark(link, title, tags string, markAsUnread bool) error {
	if c.authToken == "" {
		return fmt.Errorf("pinboard: missing credentials")
	}

	toRead := "no"
	if markAsUnread {
		toRead = "yes"
	}

	values := url.Values{}
	values.Add("auth_token", c.authToken)
	values.Add("url", link)
	values.Add("description", title)
	values.Add("tags", tags)
	values.Add("toread", toRead)

	clt := client.New("https://api.pinboard.in/v1/posts/add?" + values.Encode())
	response, err := clt.Get()
	if response.HasServerFailure() {
		return fmt.Errorf("pinboard: unable to send bookmark, status=%d", response.StatusCode)
	}

	return err
}

// NewClient returns a new Pinboard client.
func NewClient(authToken string) *Client {
	return &Client{authToken: authToken}
}