-
Notifications
You must be signed in to change notification settings - Fork 3
/
request.go
192 lines (170 loc) · 4.22 KB
/
request.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package reco
import (
"context"
"github.com/mattn/go-ieproxy"
"io"
"net/http"
"net/url"
"path"
"strings"
"time"
)
// Endpoint is reco client api endpoint.
type Endpoint string
// Append appends strings to endpoint path.
func (p Endpoint) Append(s ...string) string {
return path.Join(string(p), path.Join(s...))
}
// String returns underlying string.
func (p Endpoint) String() string { return string(p) }
// Item returns item endpoint.
func (p Endpoint) Item() string {
return p.Append("{id}")
}
// Input returns input endpoint.
func (p Endpoint) Input() string {
return p.Append("{id}", "input")
}
// Log returns log endpoint.
func (p Endpoint) Log() string {
return p.Append("{id}", "logs")
}
// Events returns events endpoint.
func (p Endpoint) Events() string {
return p.Append("{id}", "events")
}
// Graph returns graph endpoint.
func (p Endpoint) Graph() string {
return p.Append("{id}", "graph")
}
// Report returns report endpoint.
func (p Endpoint) Report() string {
return p.Append("{id}", "reports")
}
var (
endpoints = struct {
builds, deployments, projects, simulations, graphs, users Endpoint
}{
builds: "/builds",
simulations: "/simulations",
projects: "/projects",
deployments: "/deployments",
graphs: "/graphs",
users: "/user",
}
httpClient = &http.Client{
Timeout: 0,
}
)
func init() {
ieproxy.OverrideEnvWithStaticProxy()
http.DefaultTransport.(*http.Transport).Proxy = http.ProxyFromEnvironment
}
// endpoint string, params urlParams, body io.Reader
type clientRequest struct {
endpoint string
params map[string]string
username, password string
jsonBody bool
queryParams url.Values
ctx context.Context
}
func (p clientRequest) authenticated() bool {
return p.username != "" && p.password != ""
}
func (p *clientRequest) param(key, value string) *clientRequest {
if p.params == nil {
p.params = make(map[string]string)
}
p.params[key] = value
return p
}
func (p *clientRequest) queryParam(key, value string) *clientRequest {
if p.queryParams == nil {
p.queryParams = make(url.Values)
}
p.queryParams[key] = []string{value}
return p
}
func (p *clientRequest) withContext(ctx context.Context) {
p.ctx = ctx
}
// Do makes an http request with method and body. If body is not nil and not
// a io.Reader, body is encoded to JSON.
func (p *clientRequest) Do(method string, body interface{}) (*http.Response, error) {
var reader io.Reader
if !p.authenticated() {
return nil, errAuthRequired
}
if _, ok := body.(io.Reader); !ok && body != nil {
if r, err := jsonToReader(body); err == nil {
reader = r
}
} else if ok {
reader = body.(io.Reader)
}
for k, v := range p.params {
p.endpoint = strings.Replace(p.endpoint, "{"+k+"}", v, -1)
}
endpoint := p.endpoint
if len(p.queryParams) > 0 {
endpoint = p.endpoint + "?" + p.queryParams.Encode()
}
req, err := http.NewRequest(method, endpoint, reader)
if err != nil {
return nil, err
}
if p.ctx != nil {
req = req.WithContext(p.ctx)
}
if body != nil {
if p.jsonBody {
req.Header.Set("Content-Type", "application/json")
} else {
req.Header.Set("Content-Type", "application/octect-stream")
}
}
req.SetBasicAuth(p.username, p.password)
resp, err := httpClient.Do(req)
if resp != nil {
switch resp.StatusCode {
case 401, 403:
err = errAuthFailed
case 404:
err = ErrNotFound
}
} else if err != nil {
err = errNetworkError
}
return resp, err
}
type apiResponse struct {
ID string `json:"id"`
Job struct {
Events []event `json:"events"`
} `json:"job,omitempty"`
Project struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"project"`
Build struct {
ID string `json:"id"`
// workaround for deployments
// TODO: fix on platform
Project struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"project"`
} `json:"build,omitempty"`
// workaround for deployments
// TODO: fix on platform
Message string `json:"message"`
Events []event `json:"events,omitempty"`
Command string `json:"command,omitempty"`
IPAddress string `json:"ip_address,omitempty"`
}
type event struct {
Timestamp time.Time `json:"timestamp"`
Status string `json:"status"`
Code int `json:"code"`
}