-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops.go
78 lines (68 loc) · 2.01 KB
/
ops.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
package request
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"strings"
)
// Options defines request options, like request modifiers and which host to target
type Options struct {
host string
requestModifiers []func(*http.Request) error
}
// Host creates a modifier that sets the specified host as the request URL host
func Host(host string) func(*Options) {
return func(o *Options) {
o.host = host
}
}
// With adds a request modifier to the options
func With(f func(*http.Request) error) func(*Options) {
return func(o *Options) {
o.requestModifiers = append(o.requestModifiers, f)
}
}
// Method creates a modifier that sets the specified string as the request method
func Method(method string) func(*Options) {
return With(func(req *http.Request) error {
req.Method = method
return nil
})
}
// RawString sets the specified string as body for the request
func RawString(content string) func(*Options) {
return RawContent(ioutil.NopCloser(strings.NewReader(content)))
}
// RawContent sets the specified reader as body for the request
func RawContent(reader io.ReadCloser) func(*Options) {
return With(func(req *http.Request) error {
req.Body = reader
return nil
})
}
// ContentType sets the specified Content-Type request header
func ContentType(contentType string) func(*Options) {
return With(func(req *http.Request) error {
req.Header.Set("Content-Type", contentType)
return nil
})
}
// JSON sets the Content-Type request header to json
func JSON(o *Options) {
ContentType("application/json")(o)
}
// JSONBody creates a modifier that encodes the specified data to a JSON string and set it as request body. It also sets
// the Content-Type header of the request.
func JSONBody(data interface{}) func(*Options) {
return With(func(req *http.Request) error {
jsonData := bytes.NewBuffer(nil)
if err := json.NewEncoder(jsonData).Encode(data); err != nil {
return err
}
req.Body = ioutil.NopCloser(jsonData)
req.Header.Set("Content-Type", "application/json")
return nil
})
}