/
workspaces_events.go
286 lines (250 loc) · 9.21 KB
/
workspaces_events.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* This code was generated by
* ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __
* | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/
* | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \
*
* Twilio - Taskrouter
* This is the public Twilio REST API.
*
* NOTE: This class is auto generated by OpenAPI Generator.
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package openapi
import (
"encoding/json"
"fmt"
"net/url"
"strings"
"time"
"github.com/twilio/twilio-go/client"
)
//
func (c *ApiService) FetchEvent(WorkspaceSid string, Sid string) (*TaskrouterV1Event, error) {
path := "/v1/Workspaces/{WorkspaceSid}/Events/{Sid}"
path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1)
path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
data := url.Values{}
headers := make(map[string]interface{})
resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
if err != nil {
return nil, err
}
defer resp.Body.Close()
ps := &TaskrouterV1Event{}
if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
return nil, err
}
return ps, err
}
// Optional parameters for the method 'ListEvent'
type ListEventParams struct {
// Only include Events that occurred on or before this date, specified in GMT as an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date-time.
EndDate *time.Time `json:"EndDate,omitempty"`
// The type of Events to read. Returns only Events of the type specified.
EventType *string `json:"EventType,omitempty"`
// The period of events to read in minutes. Returns only Events that occurred since this many minutes in the past. The default is `15` minutes. Task Attributes for Events occuring more 43,200 minutes ago will be redacted.
Minutes *int `json:"Minutes,omitempty"`
// The SID of the Reservation with the Events to read. Returns only Events that pertain to the specified Reservation.
ReservationSid *string `json:"ReservationSid,omitempty"`
// Only include Events from on or after this date and time, specified in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format. Task Attributes for Events older than 30 days will be redacted.
StartDate *time.Time `json:"StartDate,omitempty"`
// The SID of the TaskQueue with the Events to read. Returns only the Events that pertain to the specified TaskQueue.
TaskQueueSid *string `json:"TaskQueueSid,omitempty"`
// The SID of the Task with the Events to read. Returns only the Events that pertain to the specified Task.
TaskSid *string `json:"TaskSid,omitempty"`
// The SID of the Worker with the Events to read. Returns only the Events that pertain to the specified Worker.
WorkerSid *string `json:"WorkerSid,omitempty"`
// The SID of the Workflow with the Events to read. Returns only the Events that pertain to the specified Workflow.
WorkflowSid *string `json:"WorkflowSid,omitempty"`
// The TaskChannel with the Events to read. Returns only the Events that pertain to the specified TaskChannel.
TaskChannel *string `json:"TaskChannel,omitempty"`
// The SID of the Event resource to read.
Sid *string `json:"Sid,omitempty"`
// How many resources to return in each list page. The default is 50, and the maximum is 1000.
PageSize *int `json:"PageSize,omitempty"`
// Max number of records to return.
Limit *int `json:"limit,omitempty"`
}
func (params *ListEventParams) SetEndDate(EndDate time.Time) *ListEventParams {
params.EndDate = &EndDate
return params
}
func (params *ListEventParams) SetEventType(EventType string) *ListEventParams {
params.EventType = &EventType
return params
}
func (params *ListEventParams) SetMinutes(Minutes int) *ListEventParams {
params.Minutes = &Minutes
return params
}
func (params *ListEventParams) SetReservationSid(ReservationSid string) *ListEventParams {
params.ReservationSid = &ReservationSid
return params
}
func (params *ListEventParams) SetStartDate(StartDate time.Time) *ListEventParams {
params.StartDate = &StartDate
return params
}
func (params *ListEventParams) SetTaskQueueSid(TaskQueueSid string) *ListEventParams {
params.TaskQueueSid = &TaskQueueSid
return params
}
func (params *ListEventParams) SetTaskSid(TaskSid string) *ListEventParams {
params.TaskSid = &TaskSid
return params
}
func (params *ListEventParams) SetWorkerSid(WorkerSid string) *ListEventParams {
params.WorkerSid = &WorkerSid
return params
}
func (params *ListEventParams) SetWorkflowSid(WorkflowSid string) *ListEventParams {
params.WorkflowSid = &WorkflowSid
return params
}
func (params *ListEventParams) SetTaskChannel(TaskChannel string) *ListEventParams {
params.TaskChannel = &TaskChannel
return params
}
func (params *ListEventParams) SetSid(Sid string) *ListEventParams {
params.Sid = &Sid
return params
}
func (params *ListEventParams) SetPageSize(PageSize int) *ListEventParams {
params.PageSize = &PageSize
return params
}
func (params *ListEventParams) SetLimit(Limit int) *ListEventParams {
params.Limit = &Limit
return params
}
// Retrieve a single page of Event records from the API. Request is executed immediately.
func (c *ApiService) PageEvent(WorkspaceSid string, params *ListEventParams, pageToken, pageNumber string) (*ListEventResponse, error) {
path := "/v1/Workspaces/{WorkspaceSid}/Events"
path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1)
data := url.Values{}
headers := make(map[string]interface{})
if params != nil && params.EndDate != nil {
data.Set("EndDate", fmt.Sprint((*params.EndDate).Format(time.RFC3339)))
}
if params != nil && params.EventType != nil {
data.Set("EventType", *params.EventType)
}
if params != nil && params.Minutes != nil {
data.Set("Minutes", fmt.Sprint(*params.Minutes))
}
if params != nil && params.ReservationSid != nil {
data.Set("ReservationSid", *params.ReservationSid)
}
if params != nil && params.StartDate != nil {
data.Set("StartDate", fmt.Sprint((*params.StartDate).Format(time.RFC3339)))
}
if params != nil && params.TaskQueueSid != nil {
data.Set("TaskQueueSid", *params.TaskQueueSid)
}
if params != nil && params.TaskSid != nil {
data.Set("TaskSid", *params.TaskSid)
}
if params != nil && params.WorkerSid != nil {
data.Set("WorkerSid", *params.WorkerSid)
}
if params != nil && params.WorkflowSid != nil {
data.Set("WorkflowSid", *params.WorkflowSid)
}
if params != nil && params.TaskChannel != nil {
data.Set("TaskChannel", *params.TaskChannel)
}
if params != nil && params.Sid != nil {
data.Set("Sid", *params.Sid)
}
if params != nil && params.PageSize != nil {
data.Set("PageSize", fmt.Sprint(*params.PageSize))
}
if pageToken != "" {
data.Set("PageToken", pageToken)
}
if pageNumber != "" {
data.Set("Page", pageNumber)
}
resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
if err != nil {
return nil, err
}
defer resp.Body.Close()
ps := &ListEventResponse{}
if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
return nil, err
}
return ps, err
}
// Lists Event records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
func (c *ApiService) ListEvent(WorkspaceSid string, params *ListEventParams) ([]TaskrouterV1Event, error) {
response, errors := c.StreamEvent(WorkspaceSid, params)
records := make([]TaskrouterV1Event, 0)
for record := range response {
records = append(records, record)
}
if err := <-errors; err != nil {
return nil, err
}
return records, nil
}
// Streams Event records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
func (c *ApiService) StreamEvent(WorkspaceSid string, params *ListEventParams) (chan TaskrouterV1Event, chan error) {
if params == nil {
params = &ListEventParams{}
}
params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
recordChannel := make(chan TaskrouterV1Event, 1)
errorChannel := make(chan error, 1)
response, err := c.PageEvent(WorkspaceSid, params, "", "")
if err != nil {
errorChannel <- err
close(recordChannel)
close(errorChannel)
} else {
go c.streamEvent(response, params, recordChannel, errorChannel)
}
return recordChannel, errorChannel
}
func (c *ApiService) streamEvent(response *ListEventResponse, params *ListEventParams, recordChannel chan TaskrouterV1Event, errorChannel chan error) {
curRecord := 1
for response != nil {
responseRecords := response.Events
for item := range responseRecords {
recordChannel <- responseRecords[item]
curRecord += 1
if params.Limit != nil && *params.Limit < curRecord {
close(recordChannel)
close(errorChannel)
return
}
}
record, err := client.GetNext(c.baseURL, response, c.getNextListEventResponse)
if err != nil {
errorChannel <- err
break
} else if record == nil {
break
}
response = record.(*ListEventResponse)
}
close(recordChannel)
close(errorChannel)
}
func (c *ApiService) getNextListEventResponse(nextPageUrl string) (interface{}, error) {
if nextPageUrl == "" {
return nil, nil
}
resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
ps := &ListEventResponse{}
if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
return nil, err
}
return ps, nil
}