-
Notifications
You must be signed in to change notification settings - Fork 0
/
with_res.go
121 lines (98 loc) Β· 2.62 KB
/
with_res.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
package withttp
import (
"encoding/json"
"io"
"github.com/pkg/errors"
)
func WithCloseBody[T any]() CallResOptionFunc[T] {
return func(c *Call[T], res Response) (err error) {
rc := c.bodyReader(res)
defer func() { _ = rc.Close() }()
return
}
}
func WithIgnoredBody[T any]() CallResOptionFunc[T] {
return func(c *Call[T], res Response) (err error) {
rc := c.bodyReader(res)
defer func() { _ = rc.Close() }()
_, err = io.Copy(io.Discard, rc)
return
}
}
func WithParseBodyRaw[T any]() CallResOptionFunc[T] {
return func(c *Call[T], res Response) (err error) {
rc := c.bodyReader(res)
defer func() { _ = rc.Close() }()
c.BodyRaw, err = io.ReadAll(rc)
return
}
}
func WithParseJSON[T any]() CallResOptionFunc[T] {
return func(c *Call[T], res Response) (err error) {
c.BodyParsed, err = ReadJSON[T](c.bodyReader(res))
return
}
}
func WithParseStream[T any](factory StreamFactory[T], fn func(T) bool) CallResOptionFunc[T] {
return func(c *Call[T], res Response) (err error) {
return ReadStream[T](c.bodyReader(res), factory, fn)
}
}
func WithParseStreamChan[T any](factory StreamFactory[T], out chan<- T) CallResOptionFunc[T] {
return func(c *Call[T], res Response) (err error) {
return ReadStreamChan(c.bodyReader(res), factory, out)
}
}
func WithExpectedStatusCodes[T any](states ...int) CallResOptionFunc[T] {
return WithAssertion[T](func(res Response) error {
for _, status := range states {
if status == res.Status() {
return nil
}
}
return errors.Wrapf(ErrUnexpectedStatusCode, "want: %v, have: %d", states, res.Status())
})
}
func WithAssertion[T any](fn func(res Response) error) CallResOptionFunc[T] {
return func(c *Call[T], res Response) error {
if err := fn(res); err != nil {
return errors.Wrapf(ErrAssertion, err.Error())
}
return nil
}
}
func WithMockedRes(fn func(response Response)) ResOption {
return ResOptionFunc(func(res Response) (err error) {
fn(res)
return
})
}
func ReadStreamChan[T any](rc io.ReadCloser, factory StreamFactory[T], out chan<- T) (err error) {
defer func() {
close(out)
}()
err = ReadStream[T](rc, factory, func(item T) bool {
out <- item
return true
})
return
}
func ReadStream[T any](rc io.ReadCloser, factory StreamFactory[T], fn func(T) bool) (err error) {
defer func() { _ = rc.Close() }()
stream := factory.Get(rc)
keep := true
for keep && stream.Next(nil) {
if err = stream.Err(); err != nil {
return
}
keep = fn(stream.Data())
}
return
}
func ReadJSON[T any](rc io.ReadCloser) (res T, err error) {
defer func() { _ = rc.Close() }()
if err = json.NewDecoder(rc).Decode(&res); err != nil {
return
}
return
}