/
useragent.go
63 lines (53 loc) · 1.7 KB
/
useragent.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
package utils
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"runtime"
"github.com/hashicorp/go-cleanhttp"
"github.com/turbot/steampipe/pkg/version"
)
func getUserAgent() string {
return fmt.Sprintf("Turbot Steampipe/%s (+https://steampipe.io)", version.SteampipeVersion.String())
}
// BuildRequestPayload merges the provided payload with the standard payload that needs to be sent
func BuildRequestPayload(signature string, payload map[string]interface{}) *bytes.Buffer {
requestPayload := map[string]interface{}{
"version": version.SteampipeVersion.String(),
"os_platform": runtime.GOOS,
"arch": runtime.GOARCH,
"signature": signature,
}
// change the platform to "windows_linux" if we are running in "Windows Subsystem for Linux"
if runtime.GOOS == "linux" {
if IsWSL() {
requestPayload["os_platform"] = "windows_linux"
}
}
// now merge the given payload
for k, v := range payload {
_, alreadyThere := requestPayload[k]
if alreadyThere {
panic("cannot merge already existing properties")
}
requestPayload[k] = v
}
jsonStr, _ := json.Marshal(requestPayload)
return bytes.NewBuffer(jsonStr)
}
// SendRequest makes a http call to the given URL
func SendRequest(ctx context.Context, signature string, method string, sendRequestTo url.URL, payload io.Reader) (*http.Response, error) {
// Set a default timeout of 3 sec for the check request (in milliseconds)
req, err := http.NewRequestWithContext(ctx, method, sendRequestTo.String(), payload)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", getUserAgent())
client := cleanhttp.DefaultClient()
return client.Do(req)
}