forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doget.go
77 lines (66 loc) · 1.67 KB
/
doget.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2018 Datadog, Inc.
package util
import (
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"net/http"
)
// GetClient is a convenience function returning an http client
func GetClient(verify bool) *http.Client {
if verify {
return &http.Client{}
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
return &http.Client{Transport: tr}
}
// DoGet is a wrapper around performing HTTP GET requests
func DoGet(c *http.Client, url string) (body []byte, e error) {
req, e := http.NewRequest("GET", url, nil)
if e != nil {
return body, e
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+GetAuthToken())
r, e := c.Do(req)
if e != nil {
return body, e
}
body, e = ioutil.ReadAll(r.Body)
r.Body.Close()
if e != nil {
return body, e
}
if r.StatusCode >= 400 {
return body, fmt.Errorf("%s", body)
}
return body, nil
}
// DoPost is a wrapper around performing HTTP POST requests
func DoPost(c *http.Client, url string, contentType string, body io.Reader) (resp []byte, e error) {
req, e := http.NewRequest("POST", url, body)
if e != nil {
return resp, e
}
req.Header.Set("Content-Type", contentType)
req.Header.Set("Authorization", "Bearer "+GetAuthToken())
r, e := c.Do(req)
if e != nil {
return resp, e
}
resp, e = ioutil.ReadAll(r.Body)
r.Body.Close()
if e != nil {
return resp, e
}
if r.StatusCode >= 400 {
return resp, fmt.Errorf("%s", resp)
}
return resp, nil
}