-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
54 lines (45 loc) · 1.1 KB
/
utils.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
package http
import (
"io"
hqgoreaderutil "github.com/hueristiq/hqgoutils/reader"
)
type ContextOverride string
const (
RetryMax ContextOverride = "retry-max"
)
func getReusableBodyandContentLength(rawBody interface{}) (reader *hqgoreaderutil.ReusableReadCloser, length int64, err error) {
if rawBody != nil {
switch body := rawBody.(type) {
// If they gave us a function already, great! Use it.
case hqgoreaderutil.ReusableReadCloser:
reader = &body
case *hqgoreaderutil.ReusableReadCloser:
reader = body
// If they gave us a reader function read it and get reusablereader
case func() (io.Reader, error):
var tmp io.Reader
tmp, err = body()
if err != nil {
return
}
reader, err = hqgoreaderutil.NewReusableReadCloser(tmp)
if err != nil {
return
}
// If ReusableReadCloser is not given try to create new from it
// if not possible return error
default:
reader, err = hqgoreaderutil.NewReusableReadCloser(body)
if err != nil {
return
}
}
}
if reader != nil {
length, err = getReaderLength(reader)
if err != nil {
return
}
}
return
}