/
fetch_schedule.go
83 lines (70 loc) · 1.64 KB
/
fetch_schedule.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
package dementor
import (
"net/url"
"strings"
"fmt"
"io"
"io/ioutil"
"github.com/franela/goreq"
"github.com/tidwall/gjson"
)
type FetchScheduleReq struct {
ProjectId string
FlowId string
CommonConf
}
type FetchScheduleRes struct {
CronExpression string `json:"cronExpression"`
ScheduleId string `json:"scheduleId"`
SubmitUser string `json:"submitUser"`
}
// Fetch a schedule of flow
func FetchSchedule(sessionId string, fq *FetchScheduleReq) (*FetchScheduleRes, error) {
u, err := url.Parse(fq.Url)
if err != nil {
return nil, err
}
q := u.Query()
q.Set("session.id", sessionId)
q.Set("ajax", "fetchSchedule")
q.Set("projectId", fq.ProjectId)
q.Set("flowId", fq.FlowId)
u.Path = strings.Trim(u.Path, "/") + "/schedule"
u.RawQuery = q.Encode()
res, err := goreq.Request{
Method: "GET",
Uri: u.String(),
}.Do()
if err != nil {
return nil, err
}
defer func() {
io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
}()
body, _ := res.Body.ToString()
// check status
if res.StatusCode < 200 || res.StatusCode > 399 {
return nil, fmt.Errorf("ERROR: StatusCode is %d", res.StatusCode)
}
// check error
if errName := gjson.Get(body, "error"); errName.Exists() {
return nil, fmt.Errorf("ERROR: %s", errName)
}
// check empty
v := gjson.Parse(body).Get("schedule")
if !v.Exists() {
return nil, nil
}
// parse body
var fs FetchScheduleRes
err = gjson.Unmarshal([]byte(gjson.Get(body, "schedule").Raw), &fs)
if err != nil {
return nil, fmt.Errorf(
"ERROR: Invalid fetch_schedule response\nProjectId -> %s\nFlowId -> %s\nResp -> %s",
fq.ProjectId,
fq.FlowId,
body)
}
return &fs, nil
}