-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
124 lines (102 loc) · 2.59 KB
/
client.go
File metadata and controls
124 lines (102 loc) · 2.59 KB
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
package harper
import (
"fmt"
"net/http"
"net/url"
"github.com/go-resty/resty/v2"
)
type Client struct {
endpoint string
HttpClient *resty.Client
}
func NewClientWithHTTPClient(httpClient *http.Client, endpoint, username, password string) *Client {
restyClient := resty.
NewWithClient(httpClient).
SetDisableWarn(true).
SetBasicAuth(username, password)
return &Client{
endpoint: endpoint,
HttpClient: restyClient,
}
}
func NewClient(endpoint, username, password string) *Client {
httpClient := resty.
New().
SetDisableWarn(true).
SetBasicAuth(username, password)
return &Client{
endpoint: endpoint,
HttpClient: httpClient,
}
}
// RawRequest allows raw requests to be made against the client.
// The recommended route for making calls is via the specific function endpoints.
func (c *Client) RawRequest(op Operation, result interface{}) error {
return c.opRequest(op, result)
}
func (c *Client) opRequest(op Operation, result interface{}) error {
e := ErrorResponse{}
req := c.HttpClient.
NewRequest().
SetBody(op.Prepare()).
SetError(&e)
if result != nil {
req.SetResult(result)
}
resp, err := req.Post(c.endpoint)
if err != nil {
return &OperationError{
StatusCode: resp.StatusCode(),
Message: err.Error()}
}
if resp.StatusCode() > 399 {
return &OperationError{
StatusCode: resp.StatusCode(),
Message: string(resp.Body())}
}
return nil
}
func (c *Client) SetConfigurationRequest(op interface{}, result interface{}) error {
e := ErrorResponse{}
req := c.HttpClient.NewRequest().SetBody(op).SetError(&e)
if result != nil {
req.SetResult(result)
}
resp, err := req.Post(c.endpoint)
if err != nil {
return &OperationError{
StatusCode: resp.StatusCode(),
Message: err.Error()}
}
if resp.StatusCode() > 399 {
return &OperationError{
StatusCode: resp.StatusCode(),
Message: string(resp.Body())}
}
return nil
}
// Healthcheck does a GET request against the /health endpoint of the
// configured Harper server and returns an error if it gets a non-200
// status, nil otherwise.
func (c *Client) Healthcheck() error {
e := ErrorResponse{}
healthCheckURL, err := url.JoinPath(c.endpoint, "health")
if err != nil {
return fmt.Errorf("invalid healthcheck URL: '%w'", err)
}
req := c.HttpClient.NewRequest().SetError(&e)
resp, err := req.Get(healthCheckURL)
if err != nil {
return &OperationError{
StatusCode: resp.StatusCode(),
Message: err.Error(),
}
}
if resp.StatusCode() > 399 {
return &OperationError{
StatusCode: resp.StatusCode(),
Message: string(resp.Body()),
}
}
return nil
}