forked from earthboundkid/requests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
43 lines (39 loc) · 1.05 KB
/
config.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
package requests
import (
"compress/gzip"
"io"
"net/http/httptest"
)
// Config allows Builder to be extended by setting several options at once.
// For example, a Config might set a Body and its ContentType.
type Config = func(rb *Builder)
// GzipConfig writes a gzip stream to its request body using a callback.
// It also sets the appropriate Content-Encoding header and automatically
// closes and the stream when the callback returns.
func GzipConfig(level int, h func(gw *gzip.Writer) error) Config {
return func(rb *Builder) {
rb.
Header("Content-Encoding", "gzip").
BodyWriter(func(w io.Writer) error {
gw, err := gzip.NewWriterLevel(w, level)
if err != nil {
return err
}
if err = h(gw); err != nil {
gw.Close()
return err
}
return gw.Close()
})
}
}
// TestServerConfig returns a Config
// which sets the Builder's BaseURL to s.URL
// and the Builder's Client to s.Client().
func TestServerConfig(s *httptest.Server) Config {
return func(rb *Builder) {
rb.
BaseURL(s.URL).
Client(s.Client())
}
}