forked from michaelklishin/rabbit-hole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
170 lines (139 loc) · 3.84 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
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
package rabbithole
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"time"
)
// Client for interaction with RabbitMQ HTTP API.
type Client struct {
// URI of a RabbitMQ node to use, not including the path, e.g. http://127.0.0.1:15672.
Endpoint string
// Username to use. This RabbitMQ user must have the "management" tag.
Username string
// Password to use.
Password string
host string
transport http.RoundTripper
timeout time.Duration
}
// NewClient instantiates a client.
func NewClient(uri string, username string, password string) (me *Client, err error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
}
me = &Client{
Endpoint: uri,
host: u.Host,
Username: username,
Password: password,
}
return me, nil
}
// NewTLSClient instantiates a client with a transport; it is up to the developer to make that layer secure.
func NewTLSClient(uri string, username string, password string, transport http.RoundTripper) (me *Client, err error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
}
me = &Client{
Endpoint: uri,
host: u.Host,
Username: username,
Password: password,
transport: transport,
}
return me, nil
}
// SetTransport changes the Transport Layer that the Client will use.
func (c *Client) SetTransport(transport http.RoundTripper) {
c.transport = transport
}
// SetTimeout changes the HTTP timeout that the Client will use.
// By default there is no timeout.
func (c *Client) SetTimeout(timeout time.Duration) {
c.timeout = timeout
}
func newGETRequest(client *Client, path string) (*http.Request, error) {
s := client.Endpoint + "/api/" + path
req, err := http.NewRequest("GET", s, nil)
if err != nil {
return nil, err
}
req.Close = true
req.SetBasicAuth(client.Username, client.Password)
return req, err
}
func newGETRequestWithParameters(client *Client, path string, qs url.Values) (*http.Request, error) {
s := client.Endpoint + "/api/" + path + "?" + qs.Encode()
req, err := http.NewRequest("GET", s, nil)
if err != nil {
return nil, err
}
req.Close = true
req.SetBasicAuth(client.Username, client.Password)
return req, err
}
func newRequestWithBody(client *Client, method string, path string, body []byte) (*http.Request, error) {
s := client.Endpoint + "/api/" + path
req, err := http.NewRequest(method, s, bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Close = true
req.SetBasicAuth(client.Username, client.Password)
req.Header.Add("Content-Type", "application/json")
return req, err
}
func executeRequest(client *Client, req *http.Request) (resp *http.Response, err error) {
httpc := &http.Client{
Timeout: client.timeout,
}
if client.transport != nil {
httpc.Transport = client.transport
}
resp, err = httpc.Do(req)
if err != nil {
return nil, err
}
if err = parseResponseErrors(resp); err != nil {
if resp.Body != nil {
resp.Body.Close()
}
return nil, err
}
return resp, err
}
func executeAndParseRequest(client *Client, req *http.Request, rec interface{}) (err error) {
res, err := executeRequest(client, req)
if err != nil {
return err
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&rec); err != nil {
return err
}
return nil
}
func parseResponseErrors(res *http.Response) (err error) {
if res.StatusCode == http.StatusUnauthorized {
return errors.New("Error: API responded with a 401 Unauthorized")
}
// handle a "404 Not Found" response for a DELETE request as success.
if res.Request.Method == http.MethodDelete && res.StatusCode == http.StatusNotFound {
return nil
}
if res.StatusCode >= http.StatusBadRequest {
rme := ErrorResponse{}
if err = json.NewDecoder(res.Body).Decode(&rme); err != nil {
rme.Message = fmt.Sprintf("Error %d from RabbitMQ: %s", res.StatusCode, err)
}
rme.StatusCode = res.StatusCode
return rme
}
return nil
}