forked from Eun/go-hit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.go
204 lines (180 loc) · 4.49 KB
/
send.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package hit
import (
"github.com/Eun/go-hit/errortrace"
"github.com/Eun/go-hit/internal"
"golang.org/x/xerrors"
)
type ISend interface {
IStep
// Body sets the request body to the specified value.
//
// If you omit the argument you can fine tune the send value.
//
// Usage:
// Send().Body("Hello World")
// Send().Body().Interface("Hello World")
//
// Example:
// MustDo(
// Get("https://example.com"),
// Send().Body("Hello World"),
// )
Body(value ...interface{}) ISendBody
// Interface sets the request body to the specified value.
//
// Usage:
// Send().Interface("Hello World")
// Send().Interface(map[string]interface{}{"Name": "Joe"})
//
// Example:
// MustDo(
// Get("https://example.com"),
// Send().Interface("Hello World"),
// )
Interface(value interface{}) IStep
// JSON sets the request body to the specified json value.
//
// Usage:
// Send().JSON(map[string]interface{}{"Name": "Joe"})
//
// Example:
// MustDo(
// Get("https://example.com"),
// Send().JSON(map[string]interface{}{"Name": "Joe"}),
// )
JSON(value interface{}) IStep
// Header sets the specified request header to the specified value
//
// Usage:
// Send().Header("Content-Type", "application/json")
//
// Example:
// MustDo(
// Get("https://example.com"),
// Send().Header("Content-Type", "application/json"),
// )
Header(name string, value interface{}) IStep
// Custom can be used to send a custom behaviour.
//
// Example:
// MustDo(
// Get("https://example.com"),
// Send().Custom(func(hit Hit) {
// hit.Request().Body().SetString("Hello World")
// }),
// )
Custom(fn Callback) IStep
}
type send struct {
body ISendBody
cleanPath clearPath
trace *errortrace.ErrorTrace
}
func newSend(clearPath clearPath, params []interface{}) ISend {
snd := &send{
cleanPath: clearPath,
trace: ett.Prepare(),
}
if param, ok := internal.GetLastArgument(params); ok {
// default action is Interface()
return &finalSend{
&hitStep{
Trace: snd.trace,
When: SendStep,
ClearPath: clearPath,
Exec: snd.Interface(param).exec,
},
"only usable with Send() not with Send(value)",
}
}
return snd
}
func (*send) when() StepTime {
return SendStep
}
func (snd *send) exec(hit Hit) error {
return snd.trace.Format(hit.Description(), "unable to run Send() without an argument or without a chain. Please use Send(something) or Send().Something")
}
func (snd *send) clearPath() clearPath {
return snd.cleanPath
}
func (snd *send) Body(value ...interface{}) ISendBody {
if snd.body == nil {
snd.body = newSendBody(snd.clearPath().Push("Body", value), value)
}
return snd.body
}
func (snd *send) Interface(value interface{}) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: SendStep,
ClearPath: snd.clearPath().Push("Interface", []interface{}{value}),
Exec: snd.Body(value).exec,
}
}
func (snd *send) JSON(data interface{}) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: SendStep,
ClearPath: snd.clearPath().Push("JSON", []interface{}{data}),
Exec: snd.Body().JSON(data).exec,
}
}
func (snd *send) Header(name string, value interface{}) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: SendStep,
ClearPath: snd.clearPath().Push("Header", []interface{}{name, value}),
Exec: func(hit Hit) error {
var s string
if err := converter.Convert(value, &s); err != nil {
return err
}
hit.Request().Header.Set(name, s)
return nil
},
}
}
func (snd *send) Custom(fn Callback) IStep {
return &hitStep{
Trace: ett.Prepare(),
When: SendStep,
ClearPath: snd.clearPath().Push("Custom", []interface{}{fn}),
Exec: func(hit Hit) error {
fn(hit)
return nil
},
}
}
type finalSend struct {
IStep
message string
}
func (snd *finalSend) fail() IStep {
return &hitStep{
Trace: ett.Prepare(),
When: CleanStep,
ClearPath: nil,
Exec: func(hit Hit) error {
return xerrors.New(snd.message)
},
}
}
func (snd *finalSend) Body(...interface{}) ISendBody {
return &finalSendBody{
snd.fail(),
snd.message,
}
}
func (snd *finalSend) Custom(Callback) IStep {
return snd.fail()
}
func (snd *finalSend) JSON(interface{}) IStep {
return snd.fail()
}
func (snd *finalSend) Header(string, interface{}) IStep {
return snd.fail()
}
func (snd *finalSend) Interface(interface{}) IStep {
return snd.fail()
}