forked from ovh/cds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
119 lines (101 loc) · 2.57 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
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
package main
import (
"bytes"
"io/ioutil"
"net/http"
"time"
"github.com/ovh/cds/sdk"
"github.com/facebookgo/httpcontrol"
)
//HTTPClient represent a http client
type HTTPClient struct {
api string
user string
password string
token string
http *http.Client
}
//NewHTTPClient creates a new http client
func NewHTTPClient(api, user, password, token string) *HTTPClient {
c := &HTTPClient{
api: api,
user: user,
password: password,
token: token,
}
client := &http.Client{
Transport: &httpcontrol.Transport{
RequestTimeout: 10 * time.Second,
MaxTries: 5,
},
}
c.http = client
return c
}
// CDSRequest executes an authentificated HTTP request on $path given $method and $args
func (c *HTTPClient) CDSRequest(method string, path string, args []byte) ([]byte, int, error) {
mods := []sdk.RequestModifier{
func(req *http.Request) {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "CDS/"+sdk.VERSION)
req.Header.Set("Connection", "close")
req.Header.Add(sdk.RequestedWithHeader, sdk.RequestedWithValue)
},
func(req *http.Request) {
if c.user != "" && c.password != "" {
req.SetBasicAuth(c.user, c.password)
}
},
func(req *http.Request) {
if c.user != "" && c.token != "" {
req.Header.Add(sdk.SessionTokenHeader, c.token)
req.SetBasicAuth(c.user, c.token)
}
},
}
return c.Request(method, c.api+path, args, mods...)
}
// Request executes an authentificated HTTP request on $path given $method and $args
func (c *HTTPClient) Request(method string, url string, args []byte, mods ...sdk.RequestModifier) ([]byte, int, error) {
req, err := http.NewRequest(method, url, bytes.NewReader(args))
for i := range mods {
mods[i](req)
}
if err != nil {
return nil, 0, err
}
resp, err := c.http.Do(req)
if err != nil {
return nil, 0, err
}
var nbRetry = 0
for resp.StatusCode > 500 && nbRetry < 10 {
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
resp, err = c.http.Do(req)
if err != nil {
return nil, 0, err
}
// Avoid the infinite loop
nbRetry = nbRetry + 1
}
var body []byte
body, err = ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, 0, err
}
if resp.StatusCode == 500 {
cdserr := sdk.DecodeError(body)
if cdserr != nil {
return nil, resp.StatusCode, cdserr
}
}
// if everything is fine, return body
return body, resp.StatusCode, nil
}
//Do is a wrapper to the hatchery http client
func (c *HTTPClient) Do(req *http.Request) (resp *http.Response, err error) {
return c.http.Do(req)
}