github.com/twilio/twilio-go@v1.20.1/rest/chat/v2/services_channels.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Chat 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 'CreateChannel' 28 type CreateChannelParams struct { 29 // The X-Twilio-Webhook-Enabled HTTP request header 30 XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"` 31 // A descriptive string that you create to describe the new resource. It can be up to 64 characters long. 32 FriendlyName *string `json:"FriendlyName,omitempty"` 33 // An application-defined string that uniquely identifies the resource. It can be used to address the resource in place of the Channel resource's `sid` in the URL. This value must be 64 characters or less in length and be unique within the Service. 34 UniqueName *string `json:"UniqueName,omitempty"` 35 // A valid JSON string that contains application-specific data. 36 Attributes *string `json:"Attributes,omitempty"` 37 // 38 Type *string `json:"Type,omitempty"` 39 // The date, specified in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format, to assign to the resource as the date it was created. The default value is the current time set by the Chat service. Note that this should only be used in cases where a Channel is being recreated from a backup/separate source. 40 DateCreated *time.Time `json:"DateCreated,omitempty"` 41 // The date, specified in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format, to assign to the resource as the date it was last updated. The default value is `null`. Note that this parameter should only be used in cases where a Channel is being recreated from a backup/separate source and where a Message was previously updated. 42 DateUpdated *time.Time `json:"DateUpdated,omitempty"` 43 // The `identity` of the User that created the channel. Default is: `system`. 44 CreatedBy *string `json:"CreatedBy,omitempty"` 45 } 46 47 func (params *CreateChannelParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *CreateChannelParams { 48 params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled 49 return params 50 } 51 func (params *CreateChannelParams) SetFriendlyName(FriendlyName string) *CreateChannelParams { 52 params.FriendlyName = &FriendlyName 53 return params 54 } 55 func (params *CreateChannelParams) SetUniqueName(UniqueName string) *CreateChannelParams { 56 params.UniqueName = &UniqueName 57 return params 58 } 59 func (params *CreateChannelParams) SetAttributes(Attributes string) *CreateChannelParams { 60 params.Attributes = &Attributes 61 return params 62 } 63 func (params *CreateChannelParams) SetType(Type string) *CreateChannelParams { 64 params.Type = &Type 65 return params 66 } 67 func (params *CreateChannelParams) SetDateCreated(DateCreated time.Time) *CreateChannelParams { 68 params.DateCreated = &DateCreated 69 return params 70 } 71 func (params *CreateChannelParams) SetDateUpdated(DateUpdated time.Time) *CreateChannelParams { 72 params.DateUpdated = &DateUpdated 73 return params 74 } 75 func (params *CreateChannelParams) SetCreatedBy(CreatedBy string) *CreateChannelParams { 76 params.CreatedBy = &CreatedBy 77 return params 78 } 79 80 // 81 func (c *ApiService) CreateChannel(ServiceSid string, params *CreateChannelParams) (*ChatV2Channel, error) { 82 path := "/v2/Services/{ServiceSid}/Channels" 83 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 84 85 data := url.Values{} 86 headers := make(map[string]interface{}) 87 88 if params != nil && params.FriendlyName != nil { 89 data.Set("FriendlyName", *params.FriendlyName) 90 } 91 if params != nil && params.UniqueName != nil { 92 data.Set("UniqueName", *params.UniqueName) 93 } 94 if params != nil && params.Attributes != nil { 95 data.Set("Attributes", *params.Attributes) 96 } 97 if params != nil && params.Type != nil { 98 data.Set("Type", *params.Type) 99 } 100 if params != nil && params.DateCreated != nil { 101 data.Set("DateCreated", fmt.Sprint((*params.DateCreated).Format(time.RFC3339))) 102 } 103 if params != nil && params.DateUpdated != nil { 104 data.Set("DateUpdated", fmt.Sprint((*params.DateUpdated).Format(time.RFC3339))) 105 } 106 if params != nil && params.CreatedBy != nil { 107 data.Set("CreatedBy", *params.CreatedBy) 108 } 109 110 if params != nil && params.XTwilioWebhookEnabled != nil { 111 headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled 112 } 113 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 114 if err != nil { 115 return nil, err 116 } 117 118 defer resp.Body.Close() 119 120 ps := &ChatV2Channel{} 121 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 122 return nil, err 123 } 124 125 return ps, err 126 } 127 128 // Optional parameters for the method 'DeleteChannel' 129 type DeleteChannelParams struct { 130 // The X-Twilio-Webhook-Enabled HTTP request header 131 XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"` 132 } 133 134 func (params *DeleteChannelParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *DeleteChannelParams { 135 params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled 136 return params 137 } 138 139 // 140 func (c *ApiService) DeleteChannel(ServiceSid string, Sid string, params *DeleteChannelParams) error { 141 path := "/v2/Services/{ServiceSid}/Channels/{Sid}" 142 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 143 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 144 145 data := url.Values{} 146 headers := make(map[string]interface{}) 147 148 if params != nil && params.XTwilioWebhookEnabled != nil { 149 headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled 150 } 151 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 152 if err != nil { 153 return err 154 } 155 156 defer resp.Body.Close() 157 158 return nil 159 } 160 161 // 162 func (c *ApiService) FetchChannel(ServiceSid string, Sid string) (*ChatV2Channel, error) { 163 path := "/v2/Services/{ServiceSid}/Channels/{Sid}" 164 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 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 := &ChatV2Channel{} 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 // The visibility of the Channels to read. Can be: `public` or `private` and defaults to `public`. 188 Type *[]string `json:"Type,omitempty"` 189 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 190 PageSize *int `json:"PageSize,omitempty"` 191 // Max number of records to return. 192 Limit *int `json:"limit,omitempty"` 193 } 194 195 func (params *ListChannelParams) SetType(Type []string) *ListChannelParams { 196 params.Type = &Type 197 return params 198 } 199 func (params *ListChannelParams) SetPageSize(PageSize int) *ListChannelParams { 200 params.PageSize = &PageSize 201 return params 202 } 203 func (params *ListChannelParams) SetLimit(Limit int) *ListChannelParams { 204 params.Limit = &Limit 205 return params 206 } 207 208 // Retrieve a single page of Channel records from the API. Request is executed immediately. 209 func (c *ApiService) PageChannel(ServiceSid string, params *ListChannelParams, pageToken, pageNumber string) (*ListChannelResponse, error) { 210 path := "/v2/Services/{ServiceSid}/Channels" 211 212 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 213 214 data := url.Values{} 215 headers := make(map[string]interface{}) 216 217 if params != nil && params.Type != nil { 218 for _, item := range *params.Type { 219 data.Add("Type", item) 220 } 221 } 222 if params != nil && params.PageSize != nil { 223 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 224 } 225 226 if pageToken != "" { 227 data.Set("PageToken", pageToken) 228 } 229 if pageNumber != "" { 230 data.Set("Page", pageNumber) 231 } 232 233 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 234 if err != nil { 235 return nil, err 236 } 237 238 defer resp.Body.Close() 239 240 ps := &ListChannelResponse{} 241 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 242 return nil, err 243 } 244 245 return ps, err 246 } 247 248 // Lists Channel records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 249 func (c *ApiService) ListChannel(ServiceSid string, params *ListChannelParams) ([]ChatV2Channel, error) { 250 response, errors := c.StreamChannel(ServiceSid, params) 251 252 records := make([]ChatV2Channel, 0) 253 for record := range response { 254 records = append(records, record) 255 } 256 257 if err := <-errors; err != nil { 258 return nil, err 259 } 260 261 return records, nil 262 } 263 264 // Streams Channel records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 265 func (c *ApiService) StreamChannel(ServiceSid string, params *ListChannelParams) (chan ChatV2Channel, chan error) { 266 if params == nil { 267 params = &ListChannelParams{} 268 } 269 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 270 271 recordChannel := make(chan ChatV2Channel, 1) 272 errorChannel := make(chan error, 1) 273 274 response, err := c.PageChannel(ServiceSid, params, "", "") 275 if err != nil { 276 errorChannel <- err 277 close(recordChannel) 278 close(errorChannel) 279 } else { 280 go c.streamChannel(response, params, recordChannel, errorChannel) 281 } 282 283 return recordChannel, errorChannel 284 } 285 286 func (c *ApiService) streamChannel(response *ListChannelResponse, params *ListChannelParams, recordChannel chan ChatV2Channel, errorChannel chan error) { 287 curRecord := 1 288 289 for response != nil { 290 responseRecords := response.Channels 291 for item := range responseRecords { 292 recordChannel <- responseRecords[item] 293 curRecord += 1 294 if params.Limit != nil && *params.Limit < curRecord { 295 close(recordChannel) 296 close(errorChannel) 297 return 298 } 299 } 300 301 record, err := client.GetNext(c.baseURL, response, c.getNextListChannelResponse) 302 if err != nil { 303 errorChannel <- err 304 break 305 } else if record == nil { 306 break 307 } 308 309 response = record.(*ListChannelResponse) 310 } 311 312 close(recordChannel) 313 close(errorChannel) 314 } 315 316 func (c *ApiService) getNextListChannelResponse(nextPageUrl string) (interface{}, error) { 317 if nextPageUrl == "" { 318 return nil, nil 319 } 320 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 321 if err != nil { 322 return nil, err 323 } 324 325 defer resp.Body.Close() 326 327 ps := &ListChannelResponse{} 328 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 329 return nil, err 330 } 331 return ps, nil 332 } 333 334 // Optional parameters for the method 'UpdateChannel' 335 type UpdateChannelParams struct { 336 // The X-Twilio-Webhook-Enabled HTTP request header 337 XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"` 338 // A descriptive string that you create to describe the resource. It can be up to 256 characters long. 339 FriendlyName *string `json:"FriendlyName,omitempty"` 340 // An application-defined string that uniquely identifies the resource. It can be used to address the resource in place of the resource's `sid` in the URL. This value must be 256 characters or less in length and unique within the Service. 341 UniqueName *string `json:"UniqueName,omitempty"` 342 // A valid JSON string that contains application-specific data. 343 Attributes *string `json:"Attributes,omitempty"` 344 // The date, specified in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format, to assign to the resource as the date it was created. The default value is the current time set by the Chat service. Note that this should only be used in cases where a Channel is being recreated from a backup/separate source. 345 DateCreated *time.Time `json:"DateCreated,omitempty"` 346 // The date, specified in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format, to assign to the resource as the date it was last updated. 347 DateUpdated *time.Time `json:"DateUpdated,omitempty"` 348 // The `identity` of the User that created the channel. Default is: `system`. 349 CreatedBy *string `json:"CreatedBy,omitempty"` 350 } 351 352 func (params *UpdateChannelParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *UpdateChannelParams { 353 params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled 354 return params 355 } 356 func (params *UpdateChannelParams) SetFriendlyName(FriendlyName string) *UpdateChannelParams { 357 params.FriendlyName = &FriendlyName 358 return params 359 } 360 func (params *UpdateChannelParams) SetUniqueName(UniqueName string) *UpdateChannelParams { 361 params.UniqueName = &UniqueName 362 return params 363 } 364 func (params *UpdateChannelParams) SetAttributes(Attributes string) *UpdateChannelParams { 365 params.Attributes = &Attributes 366 return params 367 } 368 func (params *UpdateChannelParams) SetDateCreated(DateCreated time.Time) *UpdateChannelParams { 369 params.DateCreated = &DateCreated 370 return params 371 } 372 func (params *UpdateChannelParams) SetDateUpdated(DateUpdated time.Time) *UpdateChannelParams { 373 params.DateUpdated = &DateUpdated 374 return params 375 } 376 func (params *UpdateChannelParams) SetCreatedBy(CreatedBy string) *UpdateChannelParams { 377 params.CreatedBy = &CreatedBy 378 return params 379 } 380 381 // 382 func (c *ApiService) UpdateChannel(ServiceSid string, Sid string, params *UpdateChannelParams) (*ChatV2Channel, error) { 383 path := "/v2/Services/{ServiceSid}/Channels/{Sid}" 384 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 385 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 386 387 data := url.Values{} 388 headers := make(map[string]interface{}) 389 390 if params != nil && params.FriendlyName != nil { 391 data.Set("FriendlyName", *params.FriendlyName) 392 } 393 if params != nil && params.UniqueName != nil { 394 data.Set("UniqueName", *params.UniqueName) 395 } 396 if params != nil && params.Attributes != nil { 397 data.Set("Attributes", *params.Attributes) 398 } 399 if params != nil && params.DateCreated != nil { 400 data.Set("DateCreated", fmt.Sprint((*params.DateCreated).Format(time.RFC3339))) 401 } 402 if params != nil && params.DateUpdated != nil { 403 data.Set("DateUpdated", fmt.Sprint((*params.DateUpdated).Format(time.RFC3339))) 404 } 405 if params != nil && params.CreatedBy != nil { 406 data.Set("CreatedBy", *params.CreatedBy) 407 } 408 409 if params != nil && params.XTwilioWebhookEnabled != nil { 410 headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled 411 } 412 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 413 if err != nil { 414 return nil, err 415 } 416 417 defer resp.Body.Close() 418 419 ps := &ChatV2Channel{} 420 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 421 return nil, err 422 } 423 424 return ps, err 425 }