This repository has been archived by the owner on May 4, 2022. It is now read-only.
/
client.go
157 lines (128 loc) · 3.23 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
package api
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/sirupsen/logrus"
)
var (
timeout = time.Second * 10
url = "https://updates.jenkins.io/update-center.actual.json"
)
// ClientOption represents an argument to NewClient.
type ClientOption = func(http.RoundTripper) http.RoundTripper
// NewClient initializes a Client.
func NewClient(opts ...ClientOption) *Client {
tr := http.DefaultTransport
for _, opt := range opts {
tr = opt(tr)
}
h := &http.Client{
Transport: tr,
Timeout: timeout,
}
client := &Client{http: h}
return client
}
func enableTracing() ClientOption {
return func(rt http.RoundTripper) http.RoundTripper {
return &Tracer{RoundTripper: rt}
}
}
// ReplaceTripper substitutes the underlying RoundTripper with a custom one.
func ReplaceTripper(tr http.RoundTripper) ClientOption {
return func(http.RoundTripper) http.RoundTripper {
return tr
}
}
// Client facilitates making HTTP requests to the GitHub API.
type Client struct {
http *http.Client
}
// GET performs a REST request and parses the response.
func (c Client) GET(version string, data interface{}) error {
requestURL := url
if version != "" {
requestURL += "?version=" + version
}
logrus.Debugf("checking url: %s", requestURL)
req, err := http.NewRequest(http.MethodGet, requestURL, nil)
if err != nil {
return err
}
resp, err := c.http.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
success := resp.StatusCode >= 200 && resp.StatusCode < 300
if !success {
logrus.Debugf("failed with resp code %d", resp.StatusCode)
return handleHTTPError(resp)
}
if resp.StatusCode == http.StatusNoContent {
return nil
}
b, err := ioutil.ReadAll(io.LimitReader(resp.Body, 10000000))
if err != nil {
return err
}
err = json.Unmarshal(b, &data)
if err != nil {
return err
}
return nil
}
// REST performs a REST request and parses the response.
func (c Client) REST(url string, body io.Reader, data interface{}) error {
logrus.Debugf("calling url: %s", url)
req, err := http.NewRequest(http.MethodGet, url, body)
if err != nil {
return err
}
resp, err := c.http.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
success := resp.StatusCode >= 200 && resp.StatusCode < 300
if !success {
logrus.Debugf("failed with resp code %d", resp.StatusCode)
return handleHTTPError(resp)
}
if resp.StatusCode == http.StatusNoContent {
return nil
}
b, err := ioutil.ReadAll(io.LimitReader(resp.Body, 10000000))
if err != nil {
return err
}
err = json.Unmarshal(b, &data)
if err != nil {
return err
}
return nil
}
func handleHTTPError(resp *http.Response) error {
var message string
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 10000000))
if err != nil {
return err
}
message = string(body)
return fmt.Errorf("http error, '%s' failed (%d): '%s'", resp.Request.URL, resp.StatusCode, message)
}
// BasicClient returns a basic implemenation of a http client.
func BasicClient() *Client {
var opts []ClientOption
// for testing purposes one can enable tracing of API calls.
httpTrace := os.Getenv("HTTP_TRACE")
if httpTrace == "1" || httpTrace == "on" || httpTrace == "true" {
opts = append(opts, enableTracing())
}
return NewClient(opts...)
}