forked from JaCoB1123/bookstack-import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookstack.go
41 lines (32 loc) · 919 Bytes
/
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
package main
import (
"fmt"
"github.com/go-resty/resty/v2"
)
type bookStackClient struct {
*resty.Client
}
type httpErrorResponse struct {
Error httpError `json:"error"`
}
type httpError struct {
Code int `json:"code"`
Message string `json:"message"`
}
func (err httpError) Error() string {
return "HTTP Error"
}
func NewBookStackClient(path, tokenID, tokenSecret string) (*bookStackClient, error) {
if path == "" || tokenID == "" || tokenSecret == "" {
return nil, fmt.Errorf("URL, Token-ID and Token-Secret have to be specified")
}
client := resty.New()
client.SetError(httpErrorResponse{})
client.SetBaseURL(path)
client.SetHeader("Authorization", "Token "+tokenID+":"+tokenSecret)
_, err := client.R().Get("/api/docs.json")
if err != nil {
return nil, fmt.Errorf("get list of endpoints: %w", err)
}
return &bookStackClient{client}, nil
}