-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
57 lines (47 loc) · 1.09 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
// Package client provides an HTTP client for Metronome operations
package client
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
// Client represents the HTTP client for interacting with Metronome
type Client struct {
Client *http.Client
URL string
ProxyURL *url.URL
}
// DoRequest makes a request to Metronome REST API
func (cl Client) DoRequest(req *http.Request) ([]byte, error) {
// Init a client
client := cl.Client
if cl.ProxyURL != nil {
client = &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(cl.ProxyURL)}}
} else {
if cl.Client == nil {
client = &http.Client{}
}
}
// Do request
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Read data
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return data, errors.New("bad response: " + fmt.Sprintf("%d", resp.StatusCode))
}
return data, nil
}
// Returns the Metronome url
func (cl Client) MetronomeUrl() string {
return strings.TrimSuffix(cl.URL, "/")
}