-
Notifications
You must be signed in to change notification settings - Fork 41
/
response.go
78 lines (61 loc) · 1.23 KB
/
response.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 canopus
import (
"strings"
)
func NoResponse() Response {
return NilResponse{}
}
type NilResponse struct {
}
func (c NilResponse) GetMessage() Message {
return nil
}
func (c NilResponse) GetError() error {
return nil
}
func (c NilResponse) GetPayload() []byte {
return nil
}
func (c NilResponse) GetURIQuery(q string) string {
return ""
}
// Creates a new Response object with a Message object and any error messages
func NewResponse(msg Message, err error) Response {
resp := &DefaultResponse{
msg: msg,
err: err,
}
return resp
}
// Creates a new response object with a Message object
func NewResponseWithMessage(msg Message) Response {
resp := &DefaultResponse{
msg: msg,
}
return resp
}
type DefaultResponse struct {
msg Message
err error
}
func (c *DefaultResponse) GetMessage() Message {
return c.msg
}
func (c *DefaultResponse) GetError() error {
return c.err
}
func (c *DefaultResponse) GetPayload() []byte {
return c.GetMessage().GetPayload().GetBytes()
}
func (c *DefaultResponse) GetURIQuery(q string) string {
qs := c.GetMessage().GetOptionsAsString(OptionURIQuery)
for _, o := range qs {
ps := strings.Split(o, "=")
if len(ps) == 2 {
if ps[0] == q {
return ps[1]
}
}
}
return ""
}