-
Notifications
You must be signed in to change notification settings - Fork 1
/
apiclient.go
79 lines (69 loc) · 1.78 KB
/
apiclient.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
package warrant
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
const (
ClientVersion string = "6.1.1"
)
type ApiClient struct {
HttpClient *http.Client
Config ClientConfig
}
func NewApiClient(config ClientConfig) *ApiClient {
httpClient := config.HttpClient
if httpClient == nil {
httpClient = http.DefaultClient
}
return &ApiClient{
HttpClient: httpClient,
Config: config,
}
}
func (client ApiClient) MakeRequest(method string, path string, payload interface{}, options *RequestOptions) (*http.Response, error) {
var request *http.Request
var err error
url := client.Config.ApiEndpoint + path
if payload != nil {
postBody, err := json.Marshal(payload)
if err != nil {
return nil, WrapError("Invalid request payload", err)
}
requestBody := bytes.NewBuffer(postBody)
request, err = http.NewRequest(method, url, requestBody)
if err != nil {
return nil, WrapError("Unable to create request", err)
}
} else {
request, err = http.NewRequest(method, url, nil)
if err != nil {
return nil, WrapError("Unable to create request", err)
}
}
if client.Config.ApiKey != "" {
request.Header.Add("Authorization", fmt.Sprintf("ApiKey %s", client.Config.ApiKey))
}
if options != nil && options.WarrantToken != "" {
request.Header.Add("Warrant-Token", options.WarrantToken)
}
request.Header.Add("User-Agent", fmt.Sprintf("warrant-go/%s", ClientVersion))
resp, err := client.HttpClient.Do(request)
if err != nil {
return nil, WrapError("Error making request", err)
}
respStatus := resp.StatusCode
if respStatus < 200 || respStatus >= 400 {
msg, err := io.ReadAll(resp.Body)
errMsg := ""
if err == nil {
errMsg = string(msg)
}
return nil, Error{
Message: fmt.Sprintf("HTTP %d %s", respStatus, errMsg),
}
}
return resp, nil
}