forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.go
156 lines (145 loc) · 3.39 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
package jenkins
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
)
type client struct {
baseURL string
httpClient *http.Client
username string
password string
sessionCookie *http.Cookie
semaphore chan struct{}
}
func newClient(httpClient *http.Client, url, username, password string, maxConnections int) *client {
return &client{
baseURL: url,
httpClient: httpClient,
username: username,
password: password,
semaphore: make(chan struct{}, maxConnections),
}
}
func (c *client) init() error {
// get session cookie
req, err := http.NewRequest("GET", c.baseURL, nil)
if err != nil {
return err
}
if c.username != "" || c.password != "" {
req.SetBasicAuth(c.username, c.password)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
for _, cc := range resp.Cookies() {
if strings.Contains(cc.Name, "JSESSIONID") {
c.sessionCookie = cc
break
}
}
// first api fetch
if err := c.doGet(context.Background(), jobPath, new(jobResponse)); err != nil {
return err
}
return nil
}
func (c *client) doGet(ctx context.Context, url string, v interface{}) error {
req, err := createGetRequest(c.baseURL+url, c.username, c.password, c.sessionCookie)
if err != nil {
return err
}
select {
case c.semaphore <- struct{}{}:
break
case <-ctx.Done():
return ctx.Err()
}
resp, err := c.httpClient.Do(req.WithContext(ctx))
if err != nil {
<-c.semaphore
return err
}
defer func() {
resp.Body.Close()
<-c.semaphore
}()
// Clear invalid token if unauthorized
if resp.StatusCode == http.StatusUnauthorized {
c.sessionCookie = nil
return APIError{
URL: url,
StatusCode: resp.StatusCode,
Title: resp.Status,
}
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return APIError{
URL: url,
StatusCode: resp.StatusCode,
Title: resp.Status,
}
}
if resp.StatusCode == http.StatusNoContent {
return APIError{
URL: url,
StatusCode: resp.StatusCode,
Title: resp.Status,
}
}
if err = json.NewDecoder(resp.Body).Decode(v); err != nil {
return err
}
return nil
}
type APIError struct {
URL string
StatusCode int
Title string
Description string
}
func (e APIError) Error() string {
if e.Description != "" {
return fmt.Sprintf("[%s] %s: %s", e.URL, e.Title, e.Description)
}
return fmt.Sprintf("[%s] %s", e.URL, e.Title)
}
func createGetRequest(url string, username, password string, sessionCookie *http.Cookie) (*http.Request, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
if username != "" || password != "" {
req.SetBasicAuth(username, password)
}
if sessionCookie != nil {
req.AddCookie(sessionCookie)
}
req.Header.Add("Accept", "application/json")
return req, nil
}
func (c *client) getJobs(ctx context.Context, jr *jobRequest) (js *jobResponse, err error) {
js = new(jobResponse)
url := jobPath
if jr != nil {
url = jr.URL()
}
err = c.doGet(ctx, url, js)
return js, err
}
func (c *client) getBuild(ctx context.Context, jr jobRequest, number int64) (b *buildResponse, err error) {
b = new(buildResponse)
url := jr.buildURL(number)
err = c.doGet(ctx, url, b)
return b, err
}
func (c *client) getAllNodes(ctx context.Context) (nodeResp *nodeResponse, err error) {
nodeResp = new(nodeResponse)
err = c.doGet(ctx, nodePath, nodeResp)
return nodeResp, err
}