/
http.go
75 lines (56 loc) · 1.21 KB
/
http.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
package util
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"github.com/google/go-querystring/query"
)
func UnmarshalBody(res *http.Response, v any) error {
body, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("Read response body failed: %w", err)
}
err = json.Unmarshal(body, v)
if err != nil {
return fmt.Errorf("Unmarshal response body failed: %w", err)
}
return nil
}
func CheckStatus(res *http.Response) error {
if 200 <= res.StatusCode && res.StatusCode <= 299 {
return nil
}
msg := fmt.Sprintf("HTTP status code not OK: %s", res.Status)
if res.Body != nil {
body, err := io.ReadAll(res.Body)
if err == nil && len(body) > 0 {
msg += "\n" + string(body)
}
}
return errors.New(msg)
}
func CloseResponse(res *http.Response) {
if res == nil || res.Body == nil {
return
}
io.Copy(io.Discard, res.Body) //nolint:errcheck
res.Body.Close()
}
func URLValuesFrom(params any) (url.Values, error) {
if m, ok := params.(map[string]string); ok {
values := url.Values{}
for k, v := range m {
values.Add(k, v)
}
return values, nil
} else {
values, err := query.Values(params)
if err != nil {
return nil, err
}
return values, nil
}
}