github.com/twilio/twilio-go@v1.20.1/rest/studio/v1/flows_executions.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Studio 8 * This is the public Twilio REST API. 9 * 10 * NOTE: This class is auto generated by OpenAPI Generator. 11 * https://openapi-generator.tech 12 * Do not edit the class manually. 13 */ 14 15 package openapi 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "net/url" 21 "strings" 22 "time" 23 24 "github.com/twilio/twilio-go/client" 25 ) 26 27 // Optional parameters for the method 'CreateExecution' 28 type CreateExecutionParams struct { 29 // The Contact phone number to start a Studio Flow Execution, available as variable `{{contact.channel.address}}`. 30 To *string `json:"To,omitempty"` 31 // The Twilio phone number to send messages or initiate calls from during the Flow's Execution. Available as variable `{{flow.channel.address}}`. For SMS, this can also be a Messaging Service SID. 32 From *string `json:"From,omitempty"` 33 // JSON data that will be added to the Flow's context and that can be accessed as variables inside your Flow. For example, if you pass in `Parameters={\\\"name\\\":\\\"Zeke\\\"}`, a widget in your Flow can reference the variable `{{flow.data.name}}`, which returns \\\"Zeke\\\". Note: the JSON value must explicitly be passed as a string, not as a hash object. Depending on your particular HTTP library, you may need to add quotes or URL encode the JSON string. 34 Parameters *interface{} `json:"Parameters,omitempty"` 35 } 36 37 func (params *CreateExecutionParams) SetTo(To string) *CreateExecutionParams { 38 params.To = &To 39 return params 40 } 41 func (params *CreateExecutionParams) SetFrom(From string) *CreateExecutionParams { 42 params.From = &From 43 return params 44 } 45 func (params *CreateExecutionParams) SetParameters(Parameters interface{}) *CreateExecutionParams { 46 params.Parameters = &Parameters 47 return params 48 } 49 50 // Triggers a new Execution for the Flow 51 func (c *ApiService) CreateExecution(FlowSid string, params *CreateExecutionParams) (*StudioV1Execution, error) { 52 path := "/v1/Flows/{FlowSid}/Executions" 53 path = strings.Replace(path, "{"+"FlowSid"+"}", FlowSid, -1) 54 55 data := url.Values{} 56 headers := make(map[string]interface{}) 57 58 if params != nil && params.To != nil { 59 data.Set("To", *params.To) 60 } 61 if params != nil && params.From != nil { 62 data.Set("From", *params.From) 63 } 64 if params != nil && params.Parameters != nil { 65 v, err := json.Marshal(params.Parameters) 66 67 if err != nil { 68 return nil, err 69 } 70 71 data.Set("Parameters", string(v)) 72 } 73 74 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 75 if err != nil { 76 return nil, err 77 } 78 79 defer resp.Body.Close() 80 81 ps := &StudioV1Execution{} 82 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 83 return nil, err 84 } 85 86 return ps, err 87 } 88 89 // Delete the Execution and all Steps relating to it. 90 func (c *ApiService) DeleteExecution(FlowSid string, Sid string) error { 91 path := "/v1/Flows/{FlowSid}/Executions/{Sid}" 92 path = strings.Replace(path, "{"+"FlowSid"+"}", FlowSid, -1) 93 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 94 95 data := url.Values{} 96 headers := make(map[string]interface{}) 97 98 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 99 if err != nil { 100 return err 101 } 102 103 defer resp.Body.Close() 104 105 return nil 106 } 107 108 // Retrieve an Execution 109 func (c *ApiService) FetchExecution(FlowSid string, Sid string) (*StudioV1Execution, error) { 110 path := "/v1/Flows/{FlowSid}/Executions/{Sid}" 111 path = strings.Replace(path, "{"+"FlowSid"+"}", FlowSid, -1) 112 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 113 114 data := url.Values{} 115 headers := make(map[string]interface{}) 116 117 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 118 if err != nil { 119 return nil, err 120 } 121 122 defer resp.Body.Close() 123 124 ps := &StudioV1Execution{} 125 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 126 return nil, err 127 } 128 129 return ps, err 130 } 131 132 // Optional parameters for the method 'ListExecution' 133 type ListExecutionParams struct { 134 // Only show Execution resources starting on or after this [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date-time, given as `YYYY-MM-DDThh:mm:ss-hh:mm`. 135 DateCreatedFrom *time.Time `json:"DateCreatedFrom,omitempty"` 136 // Only show Execution resources starting before this [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date-time, given as `YYYY-MM-DDThh:mm:ss-hh:mm`. 137 DateCreatedTo *time.Time `json:"DateCreatedTo,omitempty"` 138 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 139 PageSize *int `json:"PageSize,omitempty"` 140 // Max number of records to return. 141 Limit *int `json:"limit,omitempty"` 142 } 143 144 func (params *ListExecutionParams) SetDateCreatedFrom(DateCreatedFrom time.Time) *ListExecutionParams { 145 params.DateCreatedFrom = &DateCreatedFrom 146 return params 147 } 148 func (params *ListExecutionParams) SetDateCreatedTo(DateCreatedTo time.Time) *ListExecutionParams { 149 params.DateCreatedTo = &DateCreatedTo 150 return params 151 } 152 func (params *ListExecutionParams) SetPageSize(PageSize int) *ListExecutionParams { 153 params.PageSize = &PageSize 154 return params 155 } 156 func (params *ListExecutionParams) SetLimit(Limit int) *ListExecutionParams { 157 params.Limit = &Limit 158 return params 159 } 160 161 // Retrieve a single page of Execution records from the API. Request is executed immediately. 162 func (c *ApiService) PageExecution(FlowSid string, params *ListExecutionParams, pageToken, pageNumber string) (*ListExecutionResponse, error) { 163 path := "/v1/Flows/{FlowSid}/Executions" 164 165 path = strings.Replace(path, "{"+"FlowSid"+"}", FlowSid, -1) 166 167 data := url.Values{} 168 headers := make(map[string]interface{}) 169 170 if params != nil && params.DateCreatedFrom != nil { 171 data.Set("DateCreatedFrom", fmt.Sprint((*params.DateCreatedFrom).Format(time.RFC3339))) 172 } 173 if params != nil && params.DateCreatedTo != nil { 174 data.Set("DateCreatedTo", fmt.Sprint((*params.DateCreatedTo).Format(time.RFC3339))) 175 } 176 if params != nil && params.PageSize != nil { 177 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 178 } 179 180 if pageToken != "" { 181 data.Set("PageToken", pageToken) 182 } 183 if pageNumber != "" { 184 data.Set("Page", pageNumber) 185 } 186 187 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 188 if err != nil { 189 return nil, err 190 } 191 192 defer resp.Body.Close() 193 194 ps := &ListExecutionResponse{} 195 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 196 return nil, err 197 } 198 199 return ps, err 200 } 201 202 // Lists Execution records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 203 func (c *ApiService) ListExecution(FlowSid string, params *ListExecutionParams) ([]StudioV1Execution, error) { 204 response, errors := c.StreamExecution(FlowSid, params) 205 206 records := make([]StudioV1Execution, 0) 207 for record := range response { 208 records = append(records, record) 209 } 210 211 if err := <-errors; err != nil { 212 return nil, err 213 } 214 215 return records, nil 216 } 217 218 // Streams Execution records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 219 func (c *ApiService) StreamExecution(FlowSid string, params *ListExecutionParams) (chan StudioV1Execution, chan error) { 220 if params == nil { 221 params = &ListExecutionParams{} 222 } 223 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 224 225 recordChannel := make(chan StudioV1Execution, 1) 226 errorChannel := make(chan error, 1) 227 228 response, err := c.PageExecution(FlowSid, params, "", "") 229 if err != nil { 230 errorChannel <- err 231 close(recordChannel) 232 close(errorChannel) 233 } else { 234 go c.streamExecution(response, params, recordChannel, errorChannel) 235 } 236 237 return recordChannel, errorChannel 238 } 239 240 func (c *ApiService) streamExecution(response *ListExecutionResponse, params *ListExecutionParams, recordChannel chan StudioV1Execution, errorChannel chan error) { 241 curRecord := 1 242 243 for response != nil { 244 responseRecords := response.Executions 245 for item := range responseRecords { 246 recordChannel <- responseRecords[item] 247 curRecord += 1 248 if params.Limit != nil && *params.Limit < curRecord { 249 close(recordChannel) 250 close(errorChannel) 251 return 252 } 253 } 254 255 record, err := client.GetNext(c.baseURL, response, c.getNextListExecutionResponse) 256 if err != nil { 257 errorChannel <- err 258 break 259 } else if record == nil { 260 break 261 } 262 263 response = record.(*ListExecutionResponse) 264 } 265 266 close(recordChannel) 267 close(errorChannel) 268 } 269 270 func (c *ApiService) getNextListExecutionResponse(nextPageUrl string) (interface{}, error) { 271 if nextPageUrl == "" { 272 return nil, nil 273 } 274 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 275 if err != nil { 276 return nil, err 277 } 278 279 defer resp.Body.Close() 280 281 ps := &ListExecutionResponse{} 282 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 283 return nil, err 284 } 285 return ps, nil 286 } 287 288 // Optional parameters for the method 'UpdateExecution' 289 type UpdateExecutionParams struct { 290 // 291 Status *string `json:"Status,omitempty"` 292 } 293 294 func (params *UpdateExecutionParams) SetStatus(Status string) *UpdateExecutionParams { 295 params.Status = &Status 296 return params 297 } 298 299 // Update the status of an Execution to `ended`. 300 func (c *ApiService) UpdateExecution(FlowSid string, Sid string, params *UpdateExecutionParams) (*StudioV1Execution, error) { 301 path := "/v1/Flows/{FlowSid}/Executions/{Sid}" 302 path = strings.Replace(path, "{"+"FlowSid"+"}", FlowSid, -1) 303 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 304 305 data := url.Values{} 306 headers := make(map[string]interface{}) 307 308 if params != nil && params.Status != nil { 309 data.Set("Status", *params.Status) 310 } 311 312 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 313 if err != nil { 314 return nil, err 315 } 316 317 defer resp.Body.Close() 318 319 ps := &StudioV1Execution{} 320 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 321 return nil, err 322 } 323 324 return ps, err 325 }