-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhttp.go
59 lines (48 loc) · 1.49 KB
/
http.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
package megos
import (
"errors"
"io/ioutil"
"net/http"
"net/url"
)
// GetHTTPResponseFromCluster will return a http.Response from one of the Mesos master nodes.
// In a cluster the master nodes can be online or offline.
// With GetHTTPResponseFromCluster you will receive a response from one of the nodes.
func (c *Client) GetHTTPResponseFromCluster(f func(url.URL) url.URL) (*http.Response, error) {
for _, instance := range c.Master {
u := f(*instance)
resp, err := c.GetHTTPResponse(&u)
// If there is no error, we hit an instance / master that is online
if err == nil {
return resp, nil
}
}
return nil, errors.New("No master online.")
}
// GetHTTPResponseFromLeader will return a http.Response from the determined leader
// of the master nodes.
func (c *Client) GetHTTPResponseFromLeader(f func(Pid) url.URL) (*http.Response, error) {
if c.Leader == nil {
return nil, errors.New("No leader set.")
}
u := f(*c.Leader)
return c.GetHTTPResponse(&u)
}
// GetHTTPResponse will return a http.Response from a URL
func (c *Client) GetHTTPResponse(u *url.URL) (*http.Response, error) {
resp, err := c.HTTP.Get(u.String())
if err != nil {
return nil, err
}
return resp, nil
}
// GetBodyOfHTTPResponse will return the request body of the requested url u.
func (c *Client) GetBodyOfHTTPResponse(u *url.URL) ([]byte, error) {
resp, err := c.GetHTTPResponse(u)
if err != nil {
return []byte{}, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
return body, err
}