-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_scripting.go
More file actions
61 lines (48 loc) · 1.3 KB
/
api_scripting.go
File metadata and controls
61 lines (48 loc) · 1.3 KB
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
package squeeze_go_client
import (
"encoding/json"
"fmt"
)
type ScriptingApi struct {
client *Client
}
func newScriptingApi(client *Client) *ScriptingApi {
return &ScriptingApi{client: client}
}
func (api *ScriptingApi) GetScripts() ([]*ScriptDto, *Error) {
req, err := api.client.newRequest("GET", "/scripting/scripts", nil)
if err != nil {
return nil, newErr(err)
}
res, err := api.client.http.Do(req)
if err != nil {
return nil, newApiErr(err, res)
}
if res.StatusCode != 200 {
return nil, newApiErr(fmt.Errorf("unexpected status code: %d", res.StatusCode), res)
}
var data []*ScriptDto
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, newApiErr(fmt.Errorf("unmarshaling json failed: %s", err), res)
}
return data, nil
}
func (api *ScriptingApi) ExecuteScript(scriptId string, async bool) *Error {
req, err := api.client.newRequest("POST", fmt.Sprintf("/scripting/scripts/%s/execute", scriptId), nil)
if err != nil {
return newErr(err)
}
if async {
req.URL.Query().Set("async", "true")
} else {
req.URL.Query().Set("async", "false")
}
res, err := api.client.http.Do(req)
if err != nil {
return newApiErr(err, res)
}
if res.StatusCode != 204 {
return newApiErr(fmt.Errorf("unexpected status code: %d", res.StatusCode), res)
}
return nil
}