github.com/twilio/twilio-go@v1.20.1/rest/chat/v2/services_channels_members.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 'CreateMember' 28 type CreateMemberParams struct { 29 // The X-Twilio-Webhook-Enabled HTTP request header 30 XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"` 31 // The `identity` value that uniquely identifies the new resource's [User](https://www.twilio.com/docs/chat/rest/user-resource) within the [Service](https://www.twilio.com/docs/chat/rest/service-resource). See [access tokens](https://www.twilio.com/docs/chat/create-tokens) for more info. 32 Identity *string `json:"Identity,omitempty"` 33 // The SID of the [Role](https://www.twilio.com/docs/chat/rest/role-resource) to assign to the member. The default roles are those specified on the [Service](https://www.twilio.com/docs/chat/rest/service-resource). 34 RoleSid *string `json:"RoleSid,omitempty"` 35 // The index of the last [Message](https://www.twilio.com/docs/chat/rest/message-resource) in the [Channel](https://www.twilio.com/docs/chat/channels) that the Member has read. This parameter should only be used when recreating a Member from a backup/separate source. 36 LastConsumedMessageIndex *int `json:"LastConsumedMessageIndex,omitempty"` 37 // The [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) timestamp of the last [Message](https://www.twilio.com/docs/chat/rest/message-resource) read event for the Member within the [Channel](https://www.twilio.com/docs/chat/channels). 38 LastConsumptionTimestamp *time.Time `json:"LastConsumptionTimestamp,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 parameter should only be used when a Member 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 when a Member is being recreated from a backup/separate source and where a Member was previously updated. 42 DateUpdated *time.Time `json:"DateUpdated,omitempty"` 43 // A valid JSON string that contains application-specific data. 44 Attributes *string `json:"Attributes,omitempty"` 45 } 46 47 func (params *CreateMemberParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *CreateMemberParams { 48 params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled 49 return params 50 } 51 func (params *CreateMemberParams) SetIdentity(Identity string) *CreateMemberParams { 52 params.Identity = &Identity 53 return params 54 } 55 func (params *CreateMemberParams) SetRoleSid(RoleSid string) *CreateMemberParams { 56 params.RoleSid = &RoleSid 57 return params 58 } 59 func (params *CreateMemberParams) SetLastConsumedMessageIndex(LastConsumedMessageIndex int) *CreateMemberParams { 60 params.LastConsumedMessageIndex = &LastConsumedMessageIndex 61 return params 62 } 63 func (params *CreateMemberParams) SetLastConsumptionTimestamp(LastConsumptionTimestamp time.Time) *CreateMemberParams { 64 params.LastConsumptionTimestamp = &LastConsumptionTimestamp 65 return params 66 } 67 func (params *CreateMemberParams) SetDateCreated(DateCreated time.Time) *CreateMemberParams { 68 params.DateCreated = &DateCreated 69 return params 70 } 71 func (params *CreateMemberParams) SetDateUpdated(DateUpdated time.Time) *CreateMemberParams { 72 params.DateUpdated = &DateUpdated 73 return params 74 } 75 func (params *CreateMemberParams) SetAttributes(Attributes string) *CreateMemberParams { 76 params.Attributes = &Attributes 77 return params 78 } 79 80 // 81 func (c *ApiService) CreateMember(ServiceSid string, ChannelSid string, params *CreateMemberParams) (*ChatV2Member, error) { 82 path := "/v2/Services/{ServiceSid}/Channels/{ChannelSid}/Members" 83 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 84 path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1) 85 86 data := url.Values{} 87 headers := make(map[string]interface{}) 88 89 if params != nil && params.Identity != nil { 90 data.Set("Identity", *params.Identity) 91 } 92 if params != nil && params.RoleSid != nil { 93 data.Set("RoleSid", *params.RoleSid) 94 } 95 if params != nil && params.LastConsumedMessageIndex != nil { 96 data.Set("LastConsumedMessageIndex", fmt.Sprint(*params.LastConsumedMessageIndex)) 97 } 98 if params != nil && params.LastConsumptionTimestamp != nil { 99 data.Set("LastConsumptionTimestamp", fmt.Sprint((*params.LastConsumptionTimestamp).Format(time.RFC3339))) 100 } 101 if params != nil && params.DateCreated != nil { 102 data.Set("DateCreated", fmt.Sprint((*params.DateCreated).Format(time.RFC3339))) 103 } 104 if params != nil && params.DateUpdated != nil { 105 data.Set("DateUpdated", fmt.Sprint((*params.DateUpdated).Format(time.RFC3339))) 106 } 107 if params != nil && params.Attributes != nil { 108 data.Set("Attributes", *params.Attributes) 109 } 110 111 if params != nil && params.XTwilioWebhookEnabled != nil { 112 headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled 113 } 114 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 115 if err != nil { 116 return nil, err 117 } 118 119 defer resp.Body.Close() 120 121 ps := &ChatV2Member{} 122 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 123 return nil, err 124 } 125 126 return ps, err 127 } 128 129 // Optional parameters for the method 'DeleteMember' 130 type DeleteMemberParams struct { 131 // The X-Twilio-Webhook-Enabled HTTP request header 132 XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"` 133 } 134 135 func (params *DeleteMemberParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *DeleteMemberParams { 136 params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled 137 return params 138 } 139 140 // 141 func (c *ApiService) DeleteMember(ServiceSid string, ChannelSid string, Sid string, params *DeleteMemberParams) error { 142 path := "/v2/Services/{ServiceSid}/Channels/{ChannelSid}/Members/{Sid}" 143 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 144 path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1) 145 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 146 147 data := url.Values{} 148 headers := make(map[string]interface{}) 149 150 if params != nil && params.XTwilioWebhookEnabled != nil { 151 headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled 152 } 153 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 154 if err != nil { 155 return err 156 } 157 158 defer resp.Body.Close() 159 160 return nil 161 } 162 163 // 164 func (c *ApiService) FetchMember(ServiceSid string, ChannelSid string, Sid string) (*ChatV2Member, error) { 165 path := "/v2/Services/{ServiceSid}/Channels/{ChannelSid}/Members/{Sid}" 166 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 167 path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1) 168 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 169 170 data := url.Values{} 171 headers := make(map[string]interface{}) 172 173 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 174 if err != nil { 175 return nil, err 176 } 177 178 defer resp.Body.Close() 179 180 ps := &ChatV2Member{} 181 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 182 return nil, err 183 } 184 185 return ps, err 186 } 187 188 // Optional parameters for the method 'ListMember' 189 type ListMemberParams struct { 190 // The [User](https://www.twilio.com/docs/chat/rest/user-resource)'s `identity` value of the Member resources to read. See [access tokens](https://www.twilio.com/docs/chat/create-tokens) for more details. 191 Identity *[]string `json:"Identity,omitempty"` 192 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 193 PageSize *int `json:"PageSize,omitempty"` 194 // Max number of records to return. 195 Limit *int `json:"limit,omitempty"` 196 } 197 198 func (params *ListMemberParams) SetIdentity(Identity []string) *ListMemberParams { 199 params.Identity = &Identity 200 return params 201 } 202 func (params *ListMemberParams) SetPageSize(PageSize int) *ListMemberParams { 203 params.PageSize = &PageSize 204 return params 205 } 206 func (params *ListMemberParams) SetLimit(Limit int) *ListMemberParams { 207 params.Limit = &Limit 208 return params 209 } 210 211 // Retrieve a single page of Member records from the API. Request is executed immediately. 212 func (c *ApiService) PageMember(ServiceSid string, ChannelSid string, params *ListMemberParams, pageToken, pageNumber string) (*ListMemberResponse, error) { 213 path := "/v2/Services/{ServiceSid}/Channels/{ChannelSid}/Members" 214 215 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 216 path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1) 217 218 data := url.Values{} 219 headers := make(map[string]interface{}) 220 221 if params != nil && params.Identity != nil { 222 for _, item := range *params.Identity { 223 data.Add("Identity", item) 224 } 225 } 226 if params != nil && params.PageSize != nil { 227 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 228 } 229 230 if pageToken != "" { 231 data.Set("PageToken", pageToken) 232 } 233 if pageNumber != "" { 234 data.Set("Page", pageNumber) 235 } 236 237 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 238 if err != nil { 239 return nil, err 240 } 241 242 defer resp.Body.Close() 243 244 ps := &ListMemberResponse{} 245 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 246 return nil, err 247 } 248 249 return ps, err 250 } 251 252 // Lists Member records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 253 func (c *ApiService) ListMember(ServiceSid string, ChannelSid string, params *ListMemberParams) ([]ChatV2Member, error) { 254 response, errors := c.StreamMember(ServiceSid, ChannelSid, params) 255 256 records := make([]ChatV2Member, 0) 257 for record := range response { 258 records = append(records, record) 259 } 260 261 if err := <-errors; err != nil { 262 return nil, err 263 } 264 265 return records, nil 266 } 267 268 // Streams Member records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 269 func (c *ApiService) StreamMember(ServiceSid string, ChannelSid string, params *ListMemberParams) (chan ChatV2Member, chan error) { 270 if params == nil { 271 params = &ListMemberParams{} 272 } 273 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 274 275 recordChannel := make(chan ChatV2Member, 1) 276 errorChannel := make(chan error, 1) 277 278 response, err := c.PageMember(ServiceSid, ChannelSid, params, "", "") 279 if err != nil { 280 errorChannel <- err 281 close(recordChannel) 282 close(errorChannel) 283 } else { 284 go c.streamMember(response, params, recordChannel, errorChannel) 285 } 286 287 return recordChannel, errorChannel 288 } 289 290 func (c *ApiService) streamMember(response *ListMemberResponse, params *ListMemberParams, recordChannel chan ChatV2Member, errorChannel chan error) { 291 curRecord := 1 292 293 for response != nil { 294 responseRecords := response.Members 295 for item := range responseRecords { 296 recordChannel <- responseRecords[item] 297 curRecord += 1 298 if params.Limit != nil && *params.Limit < curRecord { 299 close(recordChannel) 300 close(errorChannel) 301 return 302 } 303 } 304 305 record, err := client.GetNext(c.baseURL, response, c.getNextListMemberResponse) 306 if err != nil { 307 errorChannel <- err 308 break 309 } else if record == nil { 310 break 311 } 312 313 response = record.(*ListMemberResponse) 314 } 315 316 close(recordChannel) 317 close(errorChannel) 318 } 319 320 func (c *ApiService) getNextListMemberResponse(nextPageUrl string) (interface{}, error) { 321 if nextPageUrl == "" { 322 return nil, nil 323 } 324 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 325 if err != nil { 326 return nil, err 327 } 328 329 defer resp.Body.Close() 330 331 ps := &ListMemberResponse{} 332 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 333 return nil, err 334 } 335 return ps, nil 336 } 337 338 // Optional parameters for the method 'UpdateMember' 339 type UpdateMemberParams struct { 340 // The X-Twilio-Webhook-Enabled HTTP request header 341 XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"` 342 // The SID of the [Role](https://www.twilio.com/docs/chat/rest/role-resource) to assign to the member. The default roles are those specified on the [Service](https://www.twilio.com/docs/chat/rest/service-resource). 343 RoleSid *string `json:"RoleSid,omitempty"` 344 // The index of the last [Message](https://www.twilio.com/docs/chat/rest/message-resource) that the Member has read within the [Channel](https://www.twilio.com/docs/chat/channels). 345 LastConsumedMessageIndex *int `json:"LastConsumedMessageIndex,omitempty"` 346 // The [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) timestamp of the last [Message](https://www.twilio.com/docs/chat/rest/message-resource) read event for the Member within the [Channel](https://www.twilio.com/docs/chat/channels). 347 LastConsumptionTimestamp *time.Time `json:"LastConsumptionTimestamp,omitempty"` 348 // 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 parameter should only be used when a Member is being recreated from a backup/separate source. 349 DateCreated *time.Time `json:"DateCreated,omitempty"` 350 // 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. 351 DateUpdated *time.Time `json:"DateUpdated,omitempty"` 352 // A valid JSON string that contains application-specific data. 353 Attributes *string `json:"Attributes,omitempty"` 354 } 355 356 func (params *UpdateMemberParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *UpdateMemberParams { 357 params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled 358 return params 359 } 360 func (params *UpdateMemberParams) SetRoleSid(RoleSid string) *UpdateMemberParams { 361 params.RoleSid = &RoleSid 362 return params 363 } 364 func (params *UpdateMemberParams) SetLastConsumedMessageIndex(LastConsumedMessageIndex int) *UpdateMemberParams { 365 params.LastConsumedMessageIndex = &LastConsumedMessageIndex 366 return params 367 } 368 func (params *UpdateMemberParams) SetLastConsumptionTimestamp(LastConsumptionTimestamp time.Time) *UpdateMemberParams { 369 params.LastConsumptionTimestamp = &LastConsumptionTimestamp 370 return params 371 } 372 func (params *UpdateMemberParams) SetDateCreated(DateCreated time.Time) *UpdateMemberParams { 373 params.DateCreated = &DateCreated 374 return params 375 } 376 func (params *UpdateMemberParams) SetDateUpdated(DateUpdated time.Time) *UpdateMemberParams { 377 params.DateUpdated = &DateUpdated 378 return params 379 } 380 func (params *UpdateMemberParams) SetAttributes(Attributes string) *UpdateMemberParams { 381 params.Attributes = &Attributes 382 return params 383 } 384 385 // 386 func (c *ApiService) UpdateMember(ServiceSid string, ChannelSid string, Sid string, params *UpdateMemberParams) (*ChatV2Member, error) { 387 path := "/v2/Services/{ServiceSid}/Channels/{ChannelSid}/Members/{Sid}" 388 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 389 path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1) 390 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 391 392 data := url.Values{} 393 headers := make(map[string]interface{}) 394 395 if params != nil && params.RoleSid != nil { 396 data.Set("RoleSid", *params.RoleSid) 397 } 398 if params != nil && params.LastConsumedMessageIndex != nil { 399 data.Set("LastConsumedMessageIndex", fmt.Sprint(*params.LastConsumedMessageIndex)) 400 } 401 if params != nil && params.LastConsumptionTimestamp != nil { 402 data.Set("LastConsumptionTimestamp", fmt.Sprint((*params.LastConsumptionTimestamp).Format(time.RFC3339))) 403 } 404 if params != nil && params.DateCreated != nil { 405 data.Set("DateCreated", fmt.Sprint((*params.DateCreated).Format(time.RFC3339))) 406 } 407 if params != nil && params.DateUpdated != nil { 408 data.Set("DateUpdated", fmt.Sprint((*params.DateUpdated).Format(time.RFC3339))) 409 } 410 if params != nil && params.Attributes != nil { 411 data.Set("Attributes", *params.Attributes) 412 } 413 414 if params != nil && params.XTwilioWebhookEnabled != nil { 415 headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled 416 } 417 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 418 if err != nil { 419 return nil, err 420 } 421 422 defer resp.Body.Close() 423 424 ps := &ChatV2Member{} 425 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 426 return nil, err 427 } 428 429 return ps, err 430 }