-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
handler.go
139 lines (121 loc) · 3.15 KB
/
handler.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package requests
import (
"bufio"
"bytes"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"golang.org/x/net/html"
)
// ResponseHandler is used to validate or handle the response to a request.
type ResponseHandler = func(*http.Response) error
// ChainHandlers allows for the composing of validators or response handlers.
func ChainHandlers(handlers ...ResponseHandler) ResponseHandler {
return func(r *http.Response) error {
for _, h := range handlers {
if h == nil {
continue
}
if err := h(r); err != nil {
return err
}
}
return nil
}
}
func consumeBody(res *http.Response) (err error) {
const maxDiscardSize = 640 * 1 << 10
if _, err = io.CopyN(io.Discard, res.Body, maxDiscardSize); err == io.EOF {
err = nil
}
return err
}
// ToDeserializer decodes a response into v using a [Deserializer].
func ToDeserializer(d Deserializer, v any) ResponseHandler {
return func(res *http.Response) error {
data, err := io.ReadAll(res.Body)
if err != nil {
return err
}
if err = d(data, v); err != nil {
return err
}
return nil
}
}
// ToJSON decodes a response as a JSON object.
//
// It uses [JSONDeserializer] to unmarshal the object.
func ToJSON(v any) ResponseHandler {
return ToDeserializer(JSONDeserializer, v)
}
// ToString writes the response body to the provided string pointer.
func ToString(sp *string) ResponseHandler {
return func(res *http.Response) error {
var buf strings.Builder
_, err := io.Copy(&buf, res.Body)
if err == nil {
*sp = buf.String()
}
return err
}
}
// ToBytesBuffer writes the response body to the provided bytes.Buffer.
func ToBytesBuffer(buf *bytes.Buffer) ResponseHandler {
return func(res *http.Response) error {
_, err := io.Copy(buf, res.Body)
return err
}
}
// ToBufioReader takes a callback which wraps the response body in a bufio.Reader.
func ToBufioReader(f func(r *bufio.Reader) error) ResponseHandler {
return func(res *http.Response) error {
return f(bufio.NewReader(res.Body))
}
}
// ToBufioScanner takes a callback which wraps the response body in a bufio.Scanner.
func ToBufioScanner(f func(r *bufio.Scanner) error) ResponseHandler {
return func(res *http.Response) error {
return f(bufio.NewScanner(res.Body))
}
}
// ToHTML parses the page with x/net/html.Parse.
//
// Deprecated: Use reqhtml.To.
func ToHTML(n *html.Node) ResponseHandler {
return ToBufioReader(func(r *bufio.Reader) error {
n2, err := html.Parse(r)
if err != nil {
return err
}
*n = *n2
return nil
})
}
// ToWriter copies the response body to w.
func ToWriter(w io.Writer) ResponseHandler {
return ToBufioReader(func(r *bufio.Reader) error {
_, err := io.Copy(w, r)
return err
})
}
// ToFile writes the response body at the provided file path.
// The file and its parent directories are created automatically.
func ToFile(name string) ResponseHandler {
return func(res *http.Response) error {
_ = os.MkdirAll(filepath.Dir(name), 0777)
f, err := os.Create(name)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, res.Body)
return err
}
}
// ToHeaders is an alias for backwards compatibility.
//
// Deprecated: Use CopyHeaders
var ToHeaders = CopyHeaders