-
Notifications
You must be signed in to change notification settings - Fork 4
/
Classes.go
102 lines (92 loc) · 2.52 KB
/
Classes.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
package lean
import (
"github.com/parnurzeal/gorequest"
)
type Collection struct {
client *leanClient
class string
classSubfix string
}
func (this *leanClient) Collection(collection string) Collection {
return Collection{
client: this,
class: collection,
classSubfix: "/classes/" + collection,
}
}
//create an object
func (this Collection) Create(obj interface{}) *Agent {
request := gorequest.New()
classesUrl := UrlBase + this.classSubfix
superAgent := request.Post(classesUrl).
Send(obj)
return &Agent{
superAgent: superAgent,
client: this.client,
}
}
//get an object by objectId
func (this Collection) GetObjectById(objectId string) *Agent {
request := gorequest.New()
classesUrl := UrlBase + this.classSubfix + "/" + objectId
superAgent := request.Get(classesUrl)
return &Agent{
superAgent: superAgent,
client: this.client,
}
}
//you can also specialfy the query parameter, if you don't provide the id, you will delete the objects by query
func (this Collection) DeleteObjectById(objectId string) *QueryAgent {
request := gorequest.New()
classesUrl := UrlBase + this.classSubfix
if "" != objectId {
classesUrl = classesUrl + "/" + objectId
}
superAgent := request.Delete(classesUrl)
return &QueryAgent{Agent{
superAgent: superAgent,
client: this.client,
}}
}
//you can also specialfy the query parameter, if you don't provide the id, you will update the object by query
func (this Collection) UpdateObjectById(objectId string, obj interface{}) *UpdateAgent {
request := gorequest.New()
classesUrl := UrlBase + this.classSubfix
if "" != objectId {
classesUrl = classesUrl + "/" + objectId
}
superAgent := request.Put(classesUrl).
Send(obj)
return &UpdateAgent{QueryAgent{Agent{
superAgent: superAgent,
client: this.client,
}}}
}
//leave cursor and key empty if they are empty
func (this Collection) Scan(cursor, key string) *QueryAgent {
request := gorequest.New()
classesUrl := UrlBase + "/scan" + this.classSubfix
superAgent := request.Get(classesUrl)
if "" != cursor {
superAgent.QueryData.Add("cursor", cursor)
}
if "" != key {
superAgent.QueryData.Add("scan_key", key)
}
agent := Agent{
superAgent: superAgent,
client: this.client,
}
agent.UseMasterKey()
return &QueryAgent{agent}
}
func (this Collection) Query() *QueryAgent {
request := gorequest.New()
classesUrl := UrlBase + this.classSubfix
superAgent := request.Get(classesUrl)
agent := Agent{
superAgent: superAgent,
client: this.client,
}
return &QueryAgent{agent}
}