-
Notifications
You must be signed in to change notification settings - Fork 1
/
lokal.go
70 lines (57 loc) · 1.52 KB
/
lokal.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
package lokal
import (
"errors"
"fmt"
"github.com/Masterminds/semver/v3"
"github.com/go-resty/resty/v2"
)
type Lokal struct {
baseURL string
basicAuth struct {
Username string
Password string
}
token string
rest *resty.Client
}
func NewDefault() (*Lokal, error) {
rest := resty.New()
rest.SetBaseURL("http://127.0.0.1:6174")
rest.SetHeader("User-Agent", "Lokal Go - github.com/lokal-so/lokal-go")
rest.OnAfterResponse(func(c *resty.Client, r *resty.Response) error {
// Now you have access to Client and Response instance
// manipulate it as per your need
version, err := semver.NewVersion(r.Header().Get("Lokal-Server-Version"))
if err != nil {
return errors.New("your local client might be outdated, please update")
}
minVersion, _ := semver.NewVersion(ServerMinVersion)
if version.LessThan(minVersion) {
return fmt.Errorf("your local client is outdated, please update to minimum version %v", ServerMinVersion)
}
return nil // if its success otherwise return error
})
instance := Lokal{
baseURL: "http://127.0.0.1:6174",
rest: rest,
}
return &instance, nil
}
func (l *Lokal) SetBaseURL(url string) *Lokal {
l.baseURL = url
l.rest.SetBaseURL(url)
return l
}
func (l *Lokal) SetBasicAuth(username, password string) *Lokal {
l.basicAuth = struct {
Username string
Password string
}{username, password}
l.rest.SetBasicAuth(username, password)
return l
}
func (l *Lokal) SetAPIToken(token string) *Lokal {
l.token = token
l.rest.SetHeader("X-Auth-Token", token)
return l
}