-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschedule.go
227 lines (193 loc) · 6.46 KB
/
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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package ilert
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
)
// Schedule definition https://api.ilert.com/api-docs/#tag/Schedules
type Schedule struct {
ID int64 `json:"id,omitempty"`
Name string `json:"name"`
Timezone string `json:"timezone,omitempty"`
StartsOn string `json:"startsOn,omitempty"` // Date time string in ISO format
CurrentShift *Shift `json:"currentShift,omitempty"`
NextShift *Shift `json:"nextShift,omitempty"`
Teams []TeamShort `json:"teams,omitempty"`
}
// Shift definition
type Shift struct {
User User `json:"user"`
Start string `json:"start"` // Date time string in ISO format
End string `json:"end"` // Date time string in ISO format
}
// GetScheduleInput represents the input of a GetSchedule operation.
type GetScheduleInput struct {
_ struct{}
ScheduleID *int64
}
// GetScheduleOutput represents the output of a GetSchedule operation.
type GetScheduleOutput struct {
_ struct{}
Schedule *Schedule
}
// GetSchedule gets the on-call schedule with the specified id. https://api.ilert.com/api-docs/#tag/Schedules/paths/~1schedules~1{id}/get
func (c *Client) GetSchedule(input *GetScheduleInput) (*GetScheduleOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.ScheduleID == nil {
return nil, errors.New("Schedule id is required")
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s/%d", apiRoutes.schedules, *input.ScheduleID))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 201); apiErr != nil {
return nil, apiErr
}
schedule := &Schedule{}
err = json.Unmarshal(resp.Body(), schedule)
if err != nil {
return nil, err
}
return &GetScheduleOutput{Schedule: schedule}, nil
}
// GetSchedulesInput represents the input of a GetSchedules operation.
type GetSchedulesInput struct {
_ struct{}
}
// GetSchedulesOutput represents the output of a GetSchedules operation.
type GetSchedulesOutput struct {
_ struct{}
Schedules []*Schedule
}
// GetSchedules gets list on-call schedules. https://api.ilert.com/api-docs/#tag/Schedules/paths/~1schedules/get
func (c *Client) GetSchedules(input *GetSchedulesInput) (*GetSchedulesOutput, error) {
resp, err := c.httpClient.R().Get(apiRoutes.schedules)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
schedules := make([]*Schedule, 0)
err = json.Unmarshal(resp.Body(), &schedules)
if err != nil {
return nil, err
}
return &GetSchedulesOutput{Schedules: schedules}, nil
}
// GetScheduleShiftsInput represents the input of a GetScheduleShifts operation.
type GetScheduleShiftsInput struct {
_ struct{}
ScheduleID *int64
From *string // Date time string in ISO format
Until *string // Date time string in ISO format
ExcludeOverrides *bool
}
// GetScheduleShiftsOutput represents the output of a GetScheduleShifts operation.
type GetScheduleShiftsOutput struct {
_ struct{}
Shifts []*Shift
}
// GetScheduleShifts gets shifts for the specified schedule and date range. https://api.ilert.com/api-docs/#tag/Schedules/paths/~1schedules~1{id}~1shifts/get
func (c *Client) GetScheduleShifts(input *GetScheduleShiftsInput) (*GetScheduleShiftsOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.ScheduleID == nil {
return nil, errors.New("Schedule id is required")
}
q := url.Values{}
if input.From != nil {
q.Add("from", *input.From)
}
if input.Until != nil {
q.Add("until", *input.From)
}
if input.ExcludeOverrides != nil {
q.Add("exclude-overrides", strconv.FormatBool(*input.ExcludeOverrides))
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s/%d/shifts?%s", apiRoutes.schedules, *input.ScheduleID, q.Encode()))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
shifts := make([]*Shift, 0)
err = json.Unmarshal(resp.Body(), &shifts)
if err != nil {
return nil, err
}
return &GetScheduleShiftsOutput{Shifts: shifts}, nil
}
// GetScheduleOverridesInput represents the input of a GetScheduleOverrides operation.
type GetScheduleOverridesInput struct {
_ struct{}
ScheduleID *int64
}
// GetScheduleOverridesOutput represents the output of a GetScheduleOverrides operation.
type GetScheduleOverridesOutput struct {
_ struct{}
Overrides []*Shift
}
// GetScheduleOverrides gets overrides for the specified schedule. https://api.ilert.com/api-docs/#tag/Schedules/paths/~1schedules~1{id}~1overrides/get
func (c *Client) GetScheduleOverrides(input *GetScheduleOverridesInput) (*GetScheduleOverridesOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.ScheduleID == nil {
return nil, errors.New("Schedule id is required")
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s/%d/overrides", apiRoutes.schedules, *input.ScheduleID))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
overrides := make([]*Shift, 0)
err = json.Unmarshal(resp.Body(), &overrides)
if err != nil {
return nil, err
}
return &GetScheduleOverridesOutput{Overrides: overrides}, nil
}
// GetScheduleUserOnCallInput represents the input of a GetScheduleUserOnCall operation.
type GetScheduleUserOnCallInput struct {
_ struct{}
ScheduleID *int64
}
// GetScheduleUserOnCallOutput represents the output of a GetScheduleUserOnCall operation.
type GetScheduleUserOnCallOutput struct {
_ struct{}
Shift *Shift
}
// GetScheduleUserOnCall gets overrides for the specified schedule. https://api.ilert.com/api-docs/#tag/Schedules/paths/~1schedules~1{id}~1user-on-call/get
func (c *Client) GetScheduleUserOnCall(input *GetScheduleUserOnCallInput) (*GetScheduleUserOnCallOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.ScheduleID == nil {
return nil, errors.New("Schedule id is required")
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s/%d/user-on-call", apiRoutes.schedules, *input.ScheduleID))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200, 204); apiErr != nil {
return nil, apiErr
}
if resp.StatusCode() == 204 {
return &GetScheduleUserOnCallOutput{}, nil
}
shift := &Shift{}
err = json.Unmarshal(resp.Body(), shift)
if err != nil {
return nil, err
}
return &GetScheduleUserOnCallOutput{Shift: shift}, nil
}