github.com/twilio/twilio-go@v1.20.1/rest/flex/v1/channels.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Flex 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 23 "github.com/twilio/twilio-go/client" 24 ) 25 26 // Optional parameters for the method 'CreateChannel' 27 type CreateChannelParams struct { 28 // The SID of the Flex Flow. 29 FlexFlowSid *string `json:"FlexFlowSid,omitempty"` 30 // The `identity` value that uniquely identifies the new resource's chat User. 31 Identity *string `json:"Identity,omitempty"` 32 // The chat participant's friendly name. 33 ChatUserFriendlyName *string `json:"ChatUserFriendlyName,omitempty"` 34 // The chat channel's friendly name. 35 ChatFriendlyName *string `json:"ChatFriendlyName,omitempty"` 36 // The Target Contact Identity, for example the phone number of an SMS. 37 Target *string `json:"Target,omitempty"` 38 // The chat channel's unique name. 39 ChatUniqueName *string `json:"ChatUniqueName,omitempty"` 40 // The pre-engagement data. 41 PreEngagementData *string `json:"PreEngagementData,omitempty"` 42 // The SID of the TaskRouter Task. Only valid when integration type is `task`. `null` for integration types `studio` & `external` 43 TaskSid *string `json:"TaskSid,omitempty"` 44 // The Task attributes to be added for the TaskRouter Task. 45 TaskAttributes *string `json:"TaskAttributes,omitempty"` 46 // Whether to create the channel as long-lived. 47 LongLived *bool `json:"LongLived,omitempty"` 48 } 49 50 func (params *CreateChannelParams) SetFlexFlowSid(FlexFlowSid string) *CreateChannelParams { 51 params.FlexFlowSid = &FlexFlowSid 52 return params 53 } 54 func (params *CreateChannelParams) SetIdentity(Identity string) *CreateChannelParams { 55 params.Identity = &Identity 56 return params 57 } 58 func (params *CreateChannelParams) SetChatUserFriendlyName(ChatUserFriendlyName string) *CreateChannelParams { 59 params.ChatUserFriendlyName = &ChatUserFriendlyName 60 return params 61 } 62 func (params *CreateChannelParams) SetChatFriendlyName(ChatFriendlyName string) *CreateChannelParams { 63 params.ChatFriendlyName = &ChatFriendlyName 64 return params 65 } 66 func (params *CreateChannelParams) SetTarget(Target string) *CreateChannelParams { 67 params.Target = &Target 68 return params 69 } 70 func (params *CreateChannelParams) SetChatUniqueName(ChatUniqueName string) *CreateChannelParams { 71 params.ChatUniqueName = &ChatUniqueName 72 return params 73 } 74 func (params *CreateChannelParams) SetPreEngagementData(PreEngagementData string) *CreateChannelParams { 75 params.PreEngagementData = &PreEngagementData 76 return params 77 } 78 func (params *CreateChannelParams) SetTaskSid(TaskSid string) *CreateChannelParams { 79 params.TaskSid = &TaskSid 80 return params 81 } 82 func (params *CreateChannelParams) SetTaskAttributes(TaskAttributes string) *CreateChannelParams { 83 params.TaskAttributes = &TaskAttributes 84 return params 85 } 86 func (params *CreateChannelParams) SetLongLived(LongLived bool) *CreateChannelParams { 87 params.LongLived = &LongLived 88 return params 89 } 90 91 // 92 func (c *ApiService) CreateChannel(params *CreateChannelParams) (*FlexV1Channel, error) { 93 path := "/v1/Channels" 94 95 data := url.Values{} 96 headers := make(map[string]interface{}) 97 98 if params != nil && params.FlexFlowSid != nil { 99 data.Set("FlexFlowSid", *params.FlexFlowSid) 100 } 101 if params != nil && params.Identity != nil { 102 data.Set("Identity", *params.Identity) 103 } 104 if params != nil && params.ChatUserFriendlyName != nil { 105 data.Set("ChatUserFriendlyName", *params.ChatUserFriendlyName) 106 } 107 if params != nil && params.ChatFriendlyName != nil { 108 data.Set("ChatFriendlyName", *params.ChatFriendlyName) 109 } 110 if params != nil && params.Target != nil { 111 data.Set("Target", *params.Target) 112 } 113 if params != nil && params.ChatUniqueName != nil { 114 data.Set("ChatUniqueName", *params.ChatUniqueName) 115 } 116 if params != nil && params.PreEngagementData != nil { 117 data.Set("PreEngagementData", *params.PreEngagementData) 118 } 119 if params != nil && params.TaskSid != nil { 120 data.Set("TaskSid", *params.TaskSid) 121 } 122 if params != nil && params.TaskAttributes != nil { 123 data.Set("TaskAttributes", *params.TaskAttributes) 124 } 125 if params != nil && params.LongLived != nil { 126 data.Set("LongLived", fmt.Sprint(*params.LongLived)) 127 } 128 129 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 130 if err != nil { 131 return nil, err 132 } 133 134 defer resp.Body.Close() 135 136 ps := &FlexV1Channel{} 137 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 138 return nil, err 139 } 140 141 return ps, err 142 } 143 144 // 145 func (c *ApiService) DeleteChannel(Sid string) error { 146 path := "/v1/Channels/{Sid}" 147 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 148 149 data := url.Values{} 150 headers := make(map[string]interface{}) 151 152 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 153 if err != nil { 154 return err 155 } 156 157 defer resp.Body.Close() 158 159 return nil 160 } 161 162 // 163 func (c *ApiService) FetchChannel(Sid string) (*FlexV1Channel, error) { 164 path := "/v1/Channels/{Sid}" 165 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 166 167 data := url.Values{} 168 headers := make(map[string]interface{}) 169 170 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 171 if err != nil { 172 return nil, err 173 } 174 175 defer resp.Body.Close() 176 177 ps := &FlexV1Channel{} 178 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 179 return nil, err 180 } 181 182 return ps, err 183 } 184 185 // Optional parameters for the method 'ListChannel' 186 type ListChannelParams struct { 187 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 188 PageSize *int `json:"PageSize,omitempty"` 189 // Max number of records to return. 190 Limit *int `json:"limit,omitempty"` 191 } 192 193 func (params *ListChannelParams) SetPageSize(PageSize int) *ListChannelParams { 194 params.PageSize = &PageSize 195 return params 196 } 197 func (params *ListChannelParams) SetLimit(Limit int) *ListChannelParams { 198 params.Limit = &Limit 199 return params 200 } 201 202 // Retrieve a single page of Channel records from the API. Request is executed immediately. 203 func (c *ApiService) PageChannel(params *ListChannelParams, pageToken, pageNumber string) (*ListChannelResponse, error) { 204 path := "/v1/Channels" 205 206 data := url.Values{} 207 headers := make(map[string]interface{}) 208 209 if params != nil && params.PageSize != nil { 210 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 211 } 212 213 if pageToken != "" { 214 data.Set("PageToken", pageToken) 215 } 216 if pageNumber != "" { 217 data.Set("Page", pageNumber) 218 } 219 220 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 221 if err != nil { 222 return nil, err 223 } 224 225 defer resp.Body.Close() 226 227 ps := &ListChannelResponse{} 228 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 229 return nil, err 230 } 231 232 return ps, err 233 } 234 235 // Lists Channel records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 236 func (c *ApiService) ListChannel(params *ListChannelParams) ([]FlexV1Channel, error) { 237 response, errors := c.StreamChannel(params) 238 239 records := make([]FlexV1Channel, 0) 240 for record := range response { 241 records = append(records, record) 242 } 243 244 if err := <-errors; err != nil { 245 return nil, err 246 } 247 248 return records, nil 249 } 250 251 // Streams Channel records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 252 func (c *ApiService) StreamChannel(params *ListChannelParams) (chan FlexV1Channel, chan error) { 253 if params == nil { 254 params = &ListChannelParams{} 255 } 256 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 257 258 recordChannel := make(chan FlexV1Channel, 1) 259 errorChannel := make(chan error, 1) 260 261 response, err := c.PageChannel(params, "", "") 262 if err != nil { 263 errorChannel <- err 264 close(recordChannel) 265 close(errorChannel) 266 } else { 267 go c.streamChannel(response, params, recordChannel, errorChannel) 268 } 269 270 return recordChannel, errorChannel 271 } 272 273 func (c *ApiService) streamChannel(response *ListChannelResponse, params *ListChannelParams, recordChannel chan FlexV1Channel, errorChannel chan error) { 274 curRecord := 1 275 276 for response != nil { 277 responseRecords := response.FlexChatChannels 278 for item := range responseRecords { 279 recordChannel <- responseRecords[item] 280 curRecord += 1 281 if params.Limit != nil && *params.Limit < curRecord { 282 close(recordChannel) 283 close(errorChannel) 284 return 285 } 286 } 287 288 record, err := client.GetNext(c.baseURL, response, c.getNextListChannelResponse) 289 if err != nil { 290 errorChannel <- err 291 break 292 } else if record == nil { 293 break 294 } 295 296 response = record.(*ListChannelResponse) 297 } 298 299 close(recordChannel) 300 close(errorChannel) 301 } 302 303 func (c *ApiService) getNextListChannelResponse(nextPageUrl string) (interface{}, error) { 304 if nextPageUrl == "" { 305 return nil, nil 306 } 307 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 308 if err != nil { 309 return nil, err 310 } 311 312 defer resp.Body.Close() 313 314 ps := &ListChannelResponse{} 315 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 316 return nil, err 317 } 318 return ps, nil 319 }