-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest.go
58 lines (46 loc) · 1.29 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
package easycall
import (
"bytes"
"encoding/json"
"errors"
"time"
"github.com/vmihailenco/msgpack"
)
//Request for EasyService
type Request struct {
format byte // request package format 0 for MSGPACK,1 for Json
head *EasyHead //request head struct
bodyData []byte //request body byte array
createTime time.Time //request create time
ext map[string]interface{} //for data transmission among middlewares
}
func (r *Request) GetBody(body interface{}) error {
if r.format == FORMAT_MSGPACK {
return msgpack.NewDecoder(bytes.NewReader(r.bodyData)).UseJSONTag(true).Decode(body)
} else if r.format == FORMAT_JSON {
return json.NewDecoder(bytes.NewReader(r.bodyData)).Decode(body)
} else {
return errors.New("invalid package format")
}
}
func (r *Request) GetBodyData() []byte {
return r.bodyData
}
func (r *Request) GetHead() *EasyHead {
return r.head
}
func (r *Request) GetCreateTime() time.Time {
return r.createTime
}
func (r *Request) GetFormat() byte {
return r.format
}
func (r *Request) GetExt() map[string]interface{} {
return r.ext
}
func (r *Request) GetExtValue(key string) interface{} {
return r.ext[key]
}
func (r *Request) SetExtValue(key string, value interface{}) {
r.ext[key] = value
}