/
cql.go
40 lines (34 loc) · 1.07 KB
/
cql.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
package api
import (
"net/url"
"github.com/leancloud/lean-cli/version"
"github.com/levigross/grequests"
)
// ExecuteCQLResult is ExecuteCQL's result type
type ExecuteCQLResult struct {
ClassName string `json:"className"`
Results []map[string]interface{} `json:"results"`
Count int `json:"count"`
}
// ExecuteCQL will execute the cql, and returns' the result
func ExecuteCQL(appID string, masterKey string, cql string) (*ExecuteCQLResult, error) {
opts := &grequests.RequestOptions{
Headers: map[string]string{
"X-LC-Id": appID,
"X-LC-Key": masterKey + ",master",
"Content-Type": "application/zip, application/octet-stream",
},
UserAgent: "LeanCloud-CLI/" + version.Version,
}
resp, err := grequests.Get(NewClientByApp(appID).GetBaseURL()+"/1.1/cloudQuery?cql="+url.QueryEscape(cql), opts)
if err != nil {
return nil, err
}
if !resp.Ok {
return nil, NewErrorFromResponse(resp)
}
result := new(ExecuteCQLResult)
result.Count = -1 // means no count returned
err = resp.JSON(result)
return result, err
}