-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrequest.go
82 lines (64 loc) · 1.6 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
82
package impl
import (
"encoding/json"
"github.com/anonymous5l/goflow/interfaces"
"github.com/valyala/fasthttp"
)
type RequestImpl struct {
ctx *fasthttp.RequestCtx
m map[string]interface{}
}
func NewRequestImpl(ctx *fasthttp.RequestCtx) *RequestImpl {
return &RequestImpl{
ctx: ctx,
m: make(map[string]interface{}),
}
}
func (self *RequestImpl) GetContext() *fasthttp.RequestCtx {
return self.ctx
}
func (self *RequestImpl) SetValue(key string, value interface{}) {
self.m[key] = value
}
func (self *RequestImpl) GetValue(key string) (interface{}, bool) {
v, o := self.m[key]
return v, o
}
func (self *RequestImpl) Body() []byte {
return self.ctx.PostBody()
}
func (self *RequestImpl) Method() string {
return string(self.ctx.Method())
}
func (self *RequestImpl) URI() *fasthttp.URI {
return self.ctx.URI()
}
func (self *RequestImpl) QueryArgs() *fasthttp.Args {
return self.ctx.QueryArgs()
}
func (self *RequestImpl) JsonBody() (interface{}, error) {
var result interface{}
err := json.Unmarshal(self.Body(), &result)
if err != nil {
return nil, err
}
return result, nil
}
func (self *RequestImpl) JsonMapBody() (map[string]interface{}, error) {
if raw, err := self.JsonBody(); err != nil {
return nil, err
} else if args, ok := raw.(map[string]interface{}); ok {
return args, nil
} else {
return nil, interfaces.ErrBodyCovert
}
}
func (self *RequestImpl) JsonArrayBody() ([]interface{}, error) {
if raw, err := self.JsonBody(); err != nil {
return nil, err
} else if args, ok := raw.([]interface{}); ok {
return args, nil
} else {
return nil, interfaces.ErrBodyCovert
}
}