-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrequest.go
81 lines (66 loc) · 1.58 KB
/
request.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
package gilmour
type Request struct {
topic string
gData *Message
}
func (r *Request) Sender() string {
return r.gData.GetSender()
}
func (r *Request) RawData() interface{} {
return r.gData.rawData()
}
func (r *Request) Data(t interface{}) error {
return r.gData.GetData(t)
}
func (r *Request) Topic() string {
return r.topic
}
func (r *Request) Code() int {
return r.gData.GetCode()
}
func (r *Request) bytes() []byte {
byt, _ := r.gData.Bytes()
return byt
}
//New Request composition
func (g *Gilmour) NewRequest(topic string) *RequestComposer {
rc := new(RequestComposer)
rc.setEngine(g)
rc.topic = topic
rc.opts = nil
return rc
}
func (g *Gilmour) NewRequestWithOpts(topic string, opts *RequestOpts) *RequestComposer {
rc := new(RequestComposer)
rc.setEngine(g)
rc.topic = topic
rc.opts = opts
return rc
}
// Command represent the each command inside a Pipeline.
// Requires a topic to send the message to, and an optional transformer.
type RequestComposer struct {
topic string
opts *RequestOpts
engine *Gilmour
message interface{}
}
//Set the Gilmour Engine required for execution
func (rc *RequestComposer) setEngine(g *Gilmour) {
rc.engine = g
}
func (rc *RequestComposer) With(t interface{}) *RequestComposer {
if rc.message != nil {
panic("Cannot change the message after its been set")
}
rc.message = t
return rc
}
func (rc *RequestComposer) Execute(m *Message) (*Response, error) {
if rc.message != nil {
if err := compositionMerge(m.rawData(), &rc.message); err != nil {
return nil, err
}
}
return rc.engine.request(rc.topic, m, rc.opts)
}