-
Notifications
You must be signed in to change notification settings - Fork 5
/
request_helpers.go
125 lines (111 loc) · 3.08 KB
/
request_helpers.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
package taxjar
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os/exec"
"regexp"
"runtime"
"strings"
"time"
"github.com/google/go-querystring/query"
)
// DefaultTimeout for requests is `30 * time.Second`․
//
// Override this by setting `Config.Timeout` to a different time value․
const DefaultTimeout = 30 * time.Second
// DefaultTransport is the default `*http.Transport` for requests․
//
// Override this by setting `Config.Transport` to a different `*http.Transport` (from net/http package - https://godoc.org/net/http#Transport)․
var DefaultTransport = &http.Transport{
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 10 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 4 * time.Second,
ResponseHeaderTimeout: 3 * time.Second,
}
func (client *Config) setTimeouts() {
if client.HTTPClient == nil {
client.HTTPClient = &http.Client{}
}
if client.Timeout > 0 {
client.HTTPClient.Timeout = client.Timeout
} else if client.HTTPClient.Timeout <= 0 {
client.HTTPClient.Timeout = DefaultTimeout
}
if client.Transport != nil {
client.HTTPClient.Transport = client.Transport
} else if client.HTTPClient.Transport == nil {
client.HTTPClient.Transport = DefaultTransport
}
}
func (client *Config) url(endpoint string) string {
return fmt.Sprintf("%v/%v/%v", client.APIURL, client.APIVersion, endpoint)
}
func getUserAgent() string {
platform := runtime.GOOS
uname, err := exec.LookPath("uname")
if err == nil {
cmd := exec.Command(uname, "-a")
var out bytes.Buffer
cmd.Stderr = nil
cmd.Stdout = &out
err = cmd.Run()
if err == nil {
platform = strings.TrimSpace(out.String())
}
}
goVersion := runtime.Version()
re, _ := regexp.Compile(`go(.+)`)
matches := re.FindStringSubmatch(goVersion)
if len(matches) > 1 {
goVersion = fmt.Sprintf("%v", matches[1])
}
return fmt.Sprintf("TaxJar/Go (%v; %v; go %v) taxjar-go/%v", platform, runtime.GOARCH, goVersion, version)
}
func (client *Config) addHeaders(req *http.Request) {
if client.APIKey == "" {
log.Fatal("taxjar: missing `APIKey` field must be set on client")
}
req.Header.Add("Authorization", "Bearer "+client.APIKey)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", getUserAgent())
for key, val := range client.Headers {
if key = http.CanonicalHeaderKey(key); key != "Authorization" && key != "Content-Type" && key != "User-Agent" {
req.Header.Add(key, val.(string))
}
}
}
func addQueryParams(req *http.Request, params interface{}) error {
queryParams, err := query.Values(params)
if err != nil {
return err
}
req.URL.RawQuery = queryParams.Encode()
return nil
}
func (client *Config) sendRequest(req *http.Request) ([]byte, error) {
client.addHeaders(req)
client.setTimeouts()
res, err := client.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
if res.StatusCode >= 400 {
err := new(Error)
json.Unmarshal(body, err)
return nil, err
}
return body, nil
}