-
Notifications
You must be signed in to change notification settings - Fork 4
/
Agent.go
134 lines (114 loc) · 2.79 KB
/
Agent.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
package lean
import (
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"github.com/johnzeng/leancloud-go-sdk/query"
"github.com/parnurzeal/gorequest"
"time"
)
type Agent struct {
client *leanClient
superAgent *gorequest.SuperAgent
body string
useSignature bool
useMasterKey bool
ts int64
token string
}
type QueryAgent struct {
Agent
}
type UpdateAgent struct {
QueryAgent
}
func (this *Agent) UseSessionToken(token string) *Agent {
this.superAgent.
Set("X-LC-Session", token)
return this
}
func (this *QueryAgent) WithKeys(key string) *QueryAgent {
this.superAgent.QueryData.Add("keys", key)
return this
}
func (this *QueryAgent) WithCount() *QueryAgent {
this.superAgent.QueryData.Add("count", "1")
return this
}
func (this *QueryAgent) WithQuery(q *query.Query) *QueryAgent {
this.superAgent.QueryData.Add("where", q.String())
return this
}
func (this *QueryAgent) WithCql(cql string) *QueryAgent {
this.superAgent.QueryData.Add("cql", cql)
return this
}
func (this *QueryAgent) Limit(l int) *QueryAgent {
this.superAgent.QueryData.Add("limit", fmt.Sprintf("%d", l))
return this
}
func (this *QueryAgent) Skip(s int) *QueryAgent {
this.superAgent.QueryData.Add("skip", fmt.Sprintf("%d", s))
return this
}
func (this *QueryAgent) Order(s string) *QueryAgent {
this.superAgent.QueryData.Add("order", fmt.Sprintf("%d", s))
return this
}
func (this *Agent) ScanResponse(ret interface{}) error {
if err := json.Unmarshal([]byte(this.body), ret); nil != err {
println(this.body)
return err
}
return nil
}
//if you wanna use signature instead of key directlly , use this
func (this *Agent) UseSignature() *Agent {
this.useSignature = true
this.ts = time.Now().UnixNano() / 1000
return this
}
//if you wanna use master key, use it
func (this *Agent) UseMasterKey() *Agent {
this.useMasterKey = true
return this
}
func (this *Agent) Do() error {
this.superAgent.Set("X-LC-Id", this.client.appId)
if this.useSignature {
this.superAgent.
Set("X-LC-Sign", this.getSignature())
} else if this.useMasterKey {
this.superAgent.
Set("X-LC-Key", this.client.masterKey+",master")
} else {
this.superAgent.
Set("X-LC-Key", this.client.appKey)
}
resp, body, err := this.superAgent.End()
this.body = body
if resp.StatusCode >= 400 {
return errors.New(body)
}
if len(err) != 0 {
for i := range err {
println(err[i].Error())
}
println("response bod:" + body)
return err[0]
}
return nil
}
func (this Agent) getSignature() string {
toSign := ""
append := ""
if this.useMasterKey {
toSign = fmt.Sprintf("%d%s", this.ts, this.client.masterKey)
append = ",master"
} else {
toSign = fmt.Sprintf("%d%s", this.ts, this.client.appKey)
}
sign := fmt.Sprintf("%x", md5.Sum([]byte(toSign)))
return fmt.Sprintf("%s,%d%s", sign, this.ts, append)
}