-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
124 lines (111 loc) · 3.08 KB
/
util.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
package iis
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
func getJson(ctx context.Context, client Client, path string, r interface{}) error {
data, err := httpGet(ctx, client, path)
if err != nil {
return err
}
return json.Unmarshal(data, &r)
}
func httpGet(ctx context.Context, client Client, path string) ([]byte, error) {
response, err := request(ctx, client, "GET", path, nil)
if err != nil {
return nil, err
}
return fetchBody(response)
}
func httpPost(ctx context.Context, client Client, path string, body interface{}) ([]byte, error) {
response, err := request(ctx, client, "POST", path, body)
if err != nil {
return nil, err
}
return fetchBody(response)
}
func httpPatch(ctx context.Context, client Client, path string, body interface{}) ([]byte, error) {
response, err := request(ctx, client, "PATCH", path, body)
if err != nil {
return nil, err
}
return fetchBody(response)
}
func httpDelete(ctx context.Context, client Client, path string) error {
if _, err := request(ctx, client, "DELETE", path, nil); err != nil {
return err
}
return nil
}
func buildHttpClient() *http.Client {
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
return &http.Client{Transport: transport}
}
func buildRequest(ctx context.Context, client Client, method, path string, body interface{}) (*http.Request, error) {
log.Printf("%s %s", method, path)
b := new(bytes.Buffer)
if body != nil {
if err := json.NewEncoder(b).Encode(body); err != nil {
return nil, err
}
log.Printf("%s %s %s", method, path, b)
}
requestUrl := fmt.Sprintf("%s%s", client.Host, path)
req, err := http.NewRequestWithContext(ctx, method, requestUrl, b)
if err != nil {
return nil, err
}
req.Header.Set("Access-Token", fmt.Sprintf("Bearer %s", client.AccessKey))
req.Header.Set("Accept", "application/hal+json")
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
return req, nil
}
func executeRequest(req *http.Request) (*http.Response, error) {
httpClient := buildHttpClient()
response, err := httpClient.Do(req)
if err != nil {
return nil, err
}
if err := guardStatusCode(req.Method, req.URL, response); err != nil {
return nil, err
}
return response, err
}
func request(ctx context.Context, client Client, method, path string, body interface{}) (*http.Response, error) {
req, err := buildRequest(ctx, client, method, path, body)
if err != nil {
return nil, err
}
return executeRequest(req)
}
func fetchBody(res *http.Response) ([]byte, error) {
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
if err = res.Body.Close(); err != nil {
return nil, err
}
return resBody, nil
}
func guardStatusCode(method string, url *url.URL, response *http.Response) error {
if response.StatusCode < 200 || response.StatusCode > 400 {
var body string
if buffer, err := fetchBody(response); err == nil {
body = string(buffer[:])
}
return fmt.Errorf("%s %s returned invalid status code: %s\n%s", method, url, response.Status, body)
}
return nil
}