forked from ghetzel/go-stockutil
/
anyget_http.go
89 lines (79 loc) · 1.91 KB
/
anyget_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package fileutil
import (
"context"
"crypto/tls"
"fmt"
"io"
"net/http"
"net/url"
"time"
"github.com/ghetzel/go-stockutil/maputil"
"github.com/ghetzel/go-stockutil/sliceutil"
"github.com/ghetzel/go-stockutil/typeutil"
)
var HttpDefaultTimeout = 10 * time.Second
// Retrieve a file via HTTP or HTTPS.
//
// Supported Context Values:
//
// insecure:
// (bool) specify that strict TLS validation should be optional.
//
// method:
// (string) the HTTP method to use, defaults to GET.
//
// metadata:
// (map[string]interface{}) a key-value set of HTTP request headers to include.
//
// safeResponseCodes:
// ([]int) a list of one or more HTTP status codes that are considered successful for this request.
//
func RetrieveViaHTTP(ctx context.Context, u *url.URL) (io.ReadCloser, error) {
ctx, timeout := ctxToTimeout(ctx, HttpDefaultTimeout)
var client = &http.Client{
Timeout: timeout,
}
if typeutil.Bool(ctx.Value(`insecure`)) {
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
}
if req, err := http.NewRequest(
typeutil.OrString(ctx.Value(`method`), http.MethodGet),
u.String(),
nil,
); err == nil {
for kv := range maputil.M(ctx.Value(`metadata`)).Iter() {
if k := kv.K; k != `` {
if v := kv.V.String(); v != `` {
req.Header.Set(k, v)
}
}
}
var statusOk = func(res *http.Response) bool {
var codes = sliceutil.Stringify(ctx.Value(`safeResponseCodes`))
if len(codes) > 0 {
return sliceutil.ContainsString(codes, typeutil.String(res.StatusCode))
} else {
return res.StatusCode < 400
}
}
if res, err := client.Do(req); err == nil {
if statusOk(res) {
if res.Body != nil {
return res.Body, nil
} else {
return NullReadCloser, nil
}
} else {
return nil, fmt.Errorf("responded HTTP: %v", res.Status)
}
} else {
return nil, err
}
} else {
return nil, err
}
}