github.com/twilio/twilio-go@v1.20.1/rest/chat/v2/services_users_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 'DeleteUserChannel' 28 type DeleteUserChannelParams struct { 29 // The X-Twilio-Webhook-Enabled HTTP request header 30 XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"` 31 } 32 33 func (params *DeleteUserChannelParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *DeleteUserChannelParams { 34 params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled 35 return params 36 } 37 38 // Removes User from selected Channel. 39 func (c *ApiService) DeleteUserChannel(ServiceSid string, UserSid string, ChannelSid string, params *DeleteUserChannelParams) error { 40 path := "/v2/Services/{ServiceSid}/Users/{UserSid}/Channels/{ChannelSid}" 41 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 42 path = strings.Replace(path, "{"+"UserSid"+"}", UserSid, -1) 43 path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1) 44 45 data := url.Values{} 46 headers := make(map[string]interface{}) 47 48 if params != nil && params.XTwilioWebhookEnabled != nil { 49 headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled 50 } 51 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 52 if err != nil { 53 return err 54 } 55 56 defer resp.Body.Close() 57 58 return nil 59 } 60 61 // 62 func (c *ApiService) FetchUserChannel(ServiceSid string, UserSid string, ChannelSid string) (*ChatV2UserChannel, error) { 63 path := "/v2/Services/{ServiceSid}/Users/{UserSid}/Channels/{ChannelSid}" 64 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 65 path = strings.Replace(path, "{"+"UserSid"+"}", UserSid, -1) 66 path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1) 67 68 data := url.Values{} 69 headers := make(map[string]interface{}) 70 71 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 72 if err != nil { 73 return nil, err 74 } 75 76 defer resp.Body.Close() 77 78 ps := &ChatV2UserChannel{} 79 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 80 return nil, err 81 } 82 83 return ps, err 84 } 85 86 // Optional parameters for the method 'ListUserChannel' 87 type ListUserChannelParams struct { 88 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 89 PageSize *int `json:"PageSize,omitempty"` 90 // Max number of records to return. 91 Limit *int `json:"limit,omitempty"` 92 } 93 94 func (params *ListUserChannelParams) SetPageSize(PageSize int) *ListUserChannelParams { 95 params.PageSize = &PageSize 96 return params 97 } 98 func (params *ListUserChannelParams) SetLimit(Limit int) *ListUserChannelParams { 99 params.Limit = &Limit 100 return params 101 } 102 103 // Retrieve a single page of UserChannel records from the API. Request is executed immediately. 104 func (c *ApiService) PageUserChannel(ServiceSid string, UserSid string, params *ListUserChannelParams, pageToken, pageNumber string) (*ListUserChannelResponse, error) { 105 path := "/v2/Services/{ServiceSid}/Users/{UserSid}/Channels" 106 107 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 108 path = strings.Replace(path, "{"+"UserSid"+"}", UserSid, -1) 109 110 data := url.Values{} 111 headers := make(map[string]interface{}) 112 113 if params != nil && params.PageSize != nil { 114 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 115 } 116 117 if pageToken != "" { 118 data.Set("PageToken", pageToken) 119 } 120 if pageNumber != "" { 121 data.Set("Page", pageNumber) 122 } 123 124 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 125 if err != nil { 126 return nil, err 127 } 128 129 defer resp.Body.Close() 130 131 ps := &ListUserChannelResponse{} 132 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 133 return nil, err 134 } 135 136 return ps, err 137 } 138 139 // Lists UserChannel records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 140 func (c *ApiService) ListUserChannel(ServiceSid string, UserSid string, params *ListUserChannelParams) ([]ChatV2UserChannel, error) { 141 response, errors := c.StreamUserChannel(ServiceSid, UserSid, params) 142 143 records := make([]ChatV2UserChannel, 0) 144 for record := range response { 145 records = append(records, record) 146 } 147 148 if err := <-errors; err != nil { 149 return nil, err 150 } 151 152 return records, nil 153 } 154 155 // Streams UserChannel records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 156 func (c *ApiService) StreamUserChannel(ServiceSid string, UserSid string, params *ListUserChannelParams) (chan ChatV2UserChannel, chan error) { 157 if params == nil { 158 params = &ListUserChannelParams{} 159 } 160 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 161 162 recordChannel := make(chan ChatV2UserChannel, 1) 163 errorChannel := make(chan error, 1) 164 165 response, err := c.PageUserChannel(ServiceSid, UserSid, params, "", "") 166 if err != nil { 167 errorChannel <- err 168 close(recordChannel) 169 close(errorChannel) 170 } else { 171 go c.streamUserChannel(response, params, recordChannel, errorChannel) 172 } 173 174 return recordChannel, errorChannel 175 } 176 177 func (c *ApiService) streamUserChannel(response *ListUserChannelResponse, params *ListUserChannelParams, recordChannel chan ChatV2UserChannel, errorChannel chan error) { 178 curRecord := 1 179 180 for response != nil { 181 responseRecords := response.Channels 182 for item := range responseRecords { 183 recordChannel <- responseRecords[item] 184 curRecord += 1 185 if params.Limit != nil && *params.Limit < curRecord { 186 close(recordChannel) 187 close(errorChannel) 188 return 189 } 190 } 191 192 record, err := client.GetNext(c.baseURL, response, c.getNextListUserChannelResponse) 193 if err != nil { 194 errorChannel <- err 195 break 196 } else if record == nil { 197 break 198 } 199 200 response = record.(*ListUserChannelResponse) 201 } 202 203 close(recordChannel) 204 close(errorChannel) 205 } 206 207 func (c *ApiService) getNextListUserChannelResponse(nextPageUrl string) (interface{}, error) { 208 if nextPageUrl == "" { 209 return nil, nil 210 } 211 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 212 if err != nil { 213 return nil, err 214 } 215 216 defer resp.Body.Close() 217 218 ps := &ListUserChannelResponse{} 219 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 220 return nil, err 221 } 222 return ps, nil 223 } 224 225 // Optional parameters for the method 'UpdateUserChannel' 226 type UpdateUserChannelParams struct { 227 // 228 NotificationLevel *string `json:"NotificationLevel,omitempty"` 229 // 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. 230 LastConsumedMessageIndex *int `json:"LastConsumedMessageIndex,omitempty"` 231 // 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). 232 LastConsumptionTimestamp *time.Time `json:"LastConsumptionTimestamp,omitempty"` 233 } 234 235 func (params *UpdateUserChannelParams) SetNotificationLevel(NotificationLevel string) *UpdateUserChannelParams { 236 params.NotificationLevel = &NotificationLevel 237 return params 238 } 239 func (params *UpdateUserChannelParams) SetLastConsumedMessageIndex(LastConsumedMessageIndex int) *UpdateUserChannelParams { 240 params.LastConsumedMessageIndex = &LastConsumedMessageIndex 241 return params 242 } 243 func (params *UpdateUserChannelParams) SetLastConsumptionTimestamp(LastConsumptionTimestamp time.Time) *UpdateUserChannelParams { 244 params.LastConsumptionTimestamp = &LastConsumptionTimestamp 245 return params 246 } 247 248 // 249 func (c *ApiService) UpdateUserChannel(ServiceSid string, UserSid string, ChannelSid string, params *UpdateUserChannelParams) (*ChatV2UserChannel, error) { 250 path := "/v2/Services/{ServiceSid}/Users/{UserSid}/Channels/{ChannelSid}" 251 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 252 path = strings.Replace(path, "{"+"UserSid"+"}", UserSid, -1) 253 path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1) 254 255 data := url.Values{} 256 headers := make(map[string]interface{}) 257 258 if params != nil && params.NotificationLevel != nil { 259 data.Set("NotificationLevel", *params.NotificationLevel) 260 } 261 if params != nil && params.LastConsumedMessageIndex != nil { 262 data.Set("LastConsumedMessageIndex", fmt.Sprint(*params.LastConsumedMessageIndex)) 263 } 264 if params != nil && params.LastConsumptionTimestamp != nil { 265 data.Set("LastConsumptionTimestamp", fmt.Sprint((*params.LastConsumptionTimestamp).Format(time.RFC3339))) 266 } 267 268 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 269 if err != nil { 270 return nil, err 271 } 272 273 defer resp.Body.Close() 274 275 ps := &ChatV2UserChannel{} 276 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 277 return nil, err 278 } 279 280 return ps, err 281 }