open-vault/vendor/github.com/michaelklishin/rabbit-hole/client.go

136 lines
3.2 KiB
Go
Raw Normal View History

2015-11-19 05:45:05 +00:00
package rabbithole
import (
"bytes"
"encoding/json"
2017-03-31 00:03:13 +00:00
"fmt"
2015-11-19 05:45:05 +00:00
"net/http"
"net/url"
2017-07-18 14:15:54 +00:00
"time"
2015-11-19 05:45:05 +00:00
)
// Client for interaction with RabbitMQ HTTP API.
2015-11-19 05:45:05 +00:00
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.
2016-06-08 14:33:08 +00:00
Password string
host string
transport http.RoundTripper
2017-07-18 14:15:54 +00:00
timeout time.Duration
2015-11-19 05:45:05 +00:00
}
// NewClient instantiates a client.
2015-11-19 05:45:05 +00:00
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) {
2015-11-19 05:45:05 +00:00
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) {
2015-11-19 05:45:05 +00:00
c.transport = transport
}
2017-07-18 14:15:54 +00:00
// 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
}
2015-11-19 05:45:05 +00:00
func newGETRequest(client *Client, path string) (*http.Request, error) {
s := client.Endpoint + "/api/" + path
req, err := http.NewRequest("GET", s, nil)
2016-06-08 14:33:08 +00:00
req.Close = true
2015-11-19 05:45:05 +00:00
req.SetBasicAuth(client.Username, client.Password)
2017-02-02 21:19:55 +00:00
2015-11-19 05:45:05 +00:00
return req, err
}
2017-01-04 21:47:38 +00:00
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)
req.Close = true
req.SetBasicAuth(client.Username, client.Password)
return req, err
}
2015-11-19 05:45:05 +00:00
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))
2016-06-08 14:33:08 +00:00
req.Close = true
2015-11-19 05:45:05 +00:00
req.SetBasicAuth(client.Username, client.Password)
req.Header.Add("Content-Type", "application/json")
return req, err
}
func executeRequest(client *Client, req *http.Request) (res *http.Response, err error) {
2017-07-18 14:15:54 +00:00
httpc := &http.Client{
Timeout: client.timeout,
2015-11-19 05:45:05 +00:00
}
2017-07-18 14:15:54 +00:00
if client.transport != nil {
httpc.Transport = client.transport
2015-11-19 05:45:05 +00:00
}
2017-07-18 14:15:54 +00:00
return httpc.Do(req)
2015-11-19 05:45:05 +00:00
}
func executeAndParseRequest(client *Client, req *http.Request, rec interface{}) (err error) {
2017-07-18 14:15:54 +00:00
res, err := executeRequest(client, req)
2015-11-19 05:45:05 +00:00
if err != nil {
return err
}
2016-06-08 14:33:08 +00:00
defer res.Body.Close() // always close body
2015-11-19 05:45:05 +00:00
2017-03-31 00:03:13 +00:00
if res.StatusCode >= http.StatusBadRequest {
rme := ErrorResponse{}
if err = json.NewDecoder(res.Body).Decode(&rme); err != nil {
2017-03-31 00:03:13 +00:00
return fmt.Errorf("Error %d from RabbitMQ: %s", res.StatusCode, err)
}
rme.StatusCode = res.StatusCode
return rme
2015-11-19 05:45:05 +00:00
}
if err = json.NewDecoder(res.Body).Decode(&rec); err != nil {
2015-11-19 05:45:05 +00:00
return err
}
return nil
}