-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
60 lines (44 loc) · 896 Bytes
/
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
package metrafin
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"time"
)
type request struct {
URL string
Method string
Data *[]byte
Headers *map[string]string
}
func doRequest(request request, client *http.Client, output interface{}) (error error) {
innerReq, err := http.NewRequest(request.Method, request.URL, bytes.NewReader(*request.Data))
if err != nil {
return err
}
if request.Headers != nil {
headers := *request.Headers
for key, value := range headers {
innerReq.Header.Set(key, value)
}
}
if client == nil {
client = &http.Client{}
}
client.Timeout = time.Millisecond * 6000
resp, err := client.Do(innerReq)
if err != nil {
return err
}
defer resp.Body.Close()
all, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(all, &output)
if err != nil {
return err
}
return nil
}