-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
72 lines (60 loc) · 1.51 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
package proxmox
import (
"crypto/tls"
"fmt"
"net/http"
"strings"
validator "gopkg.in/validator.v2"
)
type Client struct {
ticket Ticket
config *Config
client http.Client
Simulate bool
}
type Config struct {
ApiHost string
Username string
Password string
OTP string
TLS *tls.Config
}
func (c *Client) Ping() error {
var ticket Ticket
err := c.Request(http.MethodGet, "/access/ticket", nil, &ticket)
if err != nil {
return fmt.Errorf("client.ping.Request: %v", err)
}
return nil
}
func (c *Client) validate(req interface{}) error {
if err := validator.Validate(req); err != nil {
// values not valid, deal with errors here
return fmt.Errorf("client.validator.Error: %v", err)
}
return nil
}
func (c *Client) replaceBool(input string) string {
payload := strings.ReplaceAll(input, "true", "1")
payload = strings.ReplaceAll(payload, "false", "0")
return payload
}
func NewClient(cfg *Config) (*Client, error) {
client := http.Client{
Transport: &http.Transport{
TLSClientConfig: cfg.TLS,
DisableCompression: true,
Proxy: nil,
},
// CheckRedirect: nil,
// Jar: nil,
// Timeout: 0,
}
cfg.ApiHost = strings.TrimRight(cfg.ApiHost, "/") + "/api2/json"
cli := Client{client: client, config: cfg}
err := cli.authenticate()
if err != nil {
return nil, err
}
return &cli, nil
}