This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookstack.go
175 lines (131 loc) · 3 KB
/
bookstack.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package bookstack
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
"go.uber.org/ratelimit"
)
const (
appJSON = "application/json"
)
type Bookstack struct {
url string
tokenID string
tokenSecret string
limit ratelimit.Limiter
log *log.Logger
insecure bool
}
type Option func(*Bookstack)
func SetLogger(l *log.Logger) Option {
return func(b *Bookstack) {
b.log = l
}
}
func SetToken(id, secret string) Option {
return func(b *Bookstack) {
b.tokenID = id
b.tokenSecret = secret
}
}
// SetURL sets the url of the site to control.
func SetURL(url string) Option {
return func(b *Bookstack) {
b.url = url
}
}
func SetRateLimit(limit int) Option {
return func(b *Bookstack) {
b.limit = ratelimit.New(limit)
}
}
func New(opts ...Option) *Bookstack {
b := &Bookstack{
limit: ratelimit.New(180),
log: log.New(ioutil.Discard, "", 0),
}
for _, opt := range opts {
opt(b)
}
return b
}
func (b *Bookstack) authorization() string {
return fmt.Sprintf("Token %s:%s", b.tokenID, b.tokenSecret)
}
type Form interface {
Form() (string, io.Reader, error)
}
func (b *Bookstack) request(ctx context.Context, method, query string, data Form) ([]byte, error) {
b.limit.Take()
url := fmt.Sprintf("%s/api/%s", strings.TrimRight(b.url, "/"), strings.TrimLeft(query, "/"))
client := http.DefaultClient
if b.insecure {
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
}
contentType, reader, err := data.Form()
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, method, url, reader)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", b.authorization())
if contentType != "" {
req.Header.Add("Content-Type", contentType)
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
raw, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode >= http.StatusOK && resp.StatusCode <= http.StatusIMUsed {
return raw, nil
}
msg := Response{}
if err := json.Unmarshal(raw, &msg); err != nil {
return nil, err
}
return nil, msg.Error()
}
type Single interface {
User | Book | BookDetailed | Chapter | ChapterDetailed | Page | PageDetailed | Shelf | ShelfDetailed | RecycledBook | RecycledPage | RecycledChapter | Attachment | AttachmentDetailed
}
type Group interface {
[]User | []Book | []Chapter | []Page | []Shelf | []RecycleBinItem | []Attachment | []Search
}
func ParseSingle[s Single](data []byte) (s, error) {
var result s
if err := json.Unmarshal(data, &result); err != nil {
return result, nil
}
return result, nil
}
func ParseMultiple[g Group](data []byte) (g, error) {
r := Response{}
if err := json.Unmarshal(data, &r); err != nil {
return nil, err
}
if r.Error() != nil {
return nil, r.Error()
}
var result g
if err := json.Unmarshal(r.Data, &result); err != nil {
return nil, err
}
return result, nil
}