-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
85 lines (73 loc) · 2.06 KB
/
client.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package shiori
import (
"fmt"
"net/http"
"time"
)
// Client provides a request client to execute
// REST API requests with.
type Client struct {
token string
endpoint string
client *http.Client
}
// NewClient returns a new instance of client with
// the passed authorization token which connects
// to the passed endpoint URL of the siori gateway
// instance.
func NewClient(token string, endpoint string) (c *Client) {
c = &Client{
token: token,
endpoint: endpoint,
}
c.client = &http.Client{
Timeout: 60 * time.Second,
}
return
}
// Links returns a list of links limited by the
// passed limit and offset by the passed offset.
func (c *Client) Links(limit, offset int) (links []*Link, err error) {
links = make([]*Link, 0)
path := fmt.Sprintf("links?limt=%d&offset=%d", limit, offset)
err = c.get(path, &links)
return
}
// Link returns the info of the pased links ID.
func (c *Client) Link(id int) (link *Link, err error) {
link = new(Link)
path := fmt.Sprintf("links/%d", id)
err = c.get(path, link)
return
}
// Search executes a search request with the passed
// query string and returns the search result. The
// hits are capped by the passed limit and offset by
// the passed offset.
func (c *Client) Search(query string, limit, offset int) (sr *SearchResult, err error) {
sr = new(SearchResult)
path := fmt.Sprintf("links/search?query=%s&limt=%d&offset=%d", query, limit, offset)
err = c.get(path, sr)
return
}
// CreateLink creates a new link object and returns it.
func (c *Client) CreateLink(link *Link) (res *Link, err error) {
res = new(Link)
err = c.post("links", link, res)
return
}
// UpdateLink replaces the link object (by ID) passed.
//
// `nil` or empty fields are interpreted as to be reset.
func (c *Client) UpdateLink(link *Link) (res *Link, err error) {
res = new(Link)
path := fmt.Sprintf("links/%d", link.Id)
err = c.put(path, link, res)
return
}
// RemoveLink removes a link by passed ID of the link.
func (c *Client) RemoveLink(id int) (err error) {
path := fmt.Sprintf("links/%d", id)
err = c.delete(path, nil)
return
}