github.com/twilio/twilio-go@v1.20.1/rest/chat/v1/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 23 "github.com/twilio/twilio-go/client" 24 ) 25 26 // Optional parameters for the method 'CreateMember' 27 type CreateMemberParams struct { 28 // The `identity` value that uniquely identifies the new resource's [User](https://www.twilio.com/docs/api/chat/rest/v1/user) within the [Service](https://www.twilio.com/docs/api/chat/rest/services). See [access tokens](https://www.twilio.com/docs/api/chat/guides/create-tokens) for more details. 29 Identity *string `json:"Identity,omitempty"` 30 // The SID of the [Role](https://www.twilio.com/docs/api/chat/rest/roles) to assign to the member. The default roles are those specified on the [Service](https://www.twilio.com/docs/chat/api/services). 31 RoleSid *string `json:"RoleSid,omitempty"` 32 } 33 34 func (params *CreateMemberParams) SetIdentity(Identity string) *CreateMemberParams { 35 params.Identity = &Identity 36 return params 37 } 38 func (params *CreateMemberParams) SetRoleSid(RoleSid string) *CreateMemberParams { 39 params.RoleSid = &RoleSid 40 return params 41 } 42 43 // 44 func (c *ApiService) CreateMember(ServiceSid string, ChannelSid string, params *CreateMemberParams) (*ChatV1Member, error) { 45 path := "/v1/Services/{ServiceSid}/Channels/{ChannelSid}/Members" 46 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 47 path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1) 48 49 data := url.Values{} 50 headers := make(map[string]interface{}) 51 52 if params != nil && params.Identity != nil { 53 data.Set("Identity", *params.Identity) 54 } 55 if params != nil && params.RoleSid != nil { 56 data.Set("RoleSid", *params.RoleSid) 57 } 58 59 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 60 if err != nil { 61 return nil, err 62 } 63 64 defer resp.Body.Close() 65 66 ps := &ChatV1Member{} 67 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 68 return nil, err 69 } 70 71 return ps, err 72 } 73 74 // 75 func (c *ApiService) DeleteMember(ServiceSid string, ChannelSid string, Sid string) error { 76 path := "/v1/Services/{ServiceSid}/Channels/{ChannelSid}/Members/{Sid}" 77 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 78 path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1) 79 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 80 81 data := url.Values{} 82 headers := make(map[string]interface{}) 83 84 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 85 if err != nil { 86 return err 87 } 88 89 defer resp.Body.Close() 90 91 return nil 92 } 93 94 // 95 func (c *ApiService) FetchMember(ServiceSid string, ChannelSid string, Sid string) (*ChatV1Member, error) { 96 path := "/v1/Services/{ServiceSid}/Channels/{ChannelSid}/Members/{Sid}" 97 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 98 path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1) 99 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 100 101 data := url.Values{} 102 headers := make(map[string]interface{}) 103 104 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 105 if err != nil { 106 return nil, err 107 } 108 109 defer resp.Body.Close() 110 111 ps := &ChatV1Member{} 112 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 113 return nil, err 114 } 115 116 return ps, err 117 } 118 119 // Optional parameters for the method 'ListMember' 120 type ListMemberParams struct { 121 // The [User](https://www.twilio.com/docs/api/chat/rest/v1/user)'s `identity` value of the resources to read. See [access tokens](https://www.twilio.com/docs/api/chat/guides/create-tokens) for more details. 122 Identity *[]string `json:"Identity,omitempty"` 123 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 124 PageSize *int `json:"PageSize,omitempty"` 125 // Max number of records to return. 126 Limit *int `json:"limit,omitempty"` 127 } 128 129 func (params *ListMemberParams) SetIdentity(Identity []string) *ListMemberParams { 130 params.Identity = &Identity 131 return params 132 } 133 func (params *ListMemberParams) SetPageSize(PageSize int) *ListMemberParams { 134 params.PageSize = &PageSize 135 return params 136 } 137 func (params *ListMemberParams) SetLimit(Limit int) *ListMemberParams { 138 params.Limit = &Limit 139 return params 140 } 141 142 // Retrieve a single page of Member records from the API. Request is executed immediately. 143 func (c *ApiService) PageMember(ServiceSid string, ChannelSid string, params *ListMemberParams, pageToken, pageNumber string) (*ListMemberResponse, error) { 144 path := "/v1/Services/{ServiceSid}/Channels/{ChannelSid}/Members" 145 146 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 147 path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1) 148 149 data := url.Values{} 150 headers := make(map[string]interface{}) 151 152 if params != nil && params.Identity != nil { 153 for _, item := range *params.Identity { 154 data.Add("Identity", item) 155 } 156 } 157 if params != nil && params.PageSize != nil { 158 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 159 } 160 161 if pageToken != "" { 162 data.Set("PageToken", pageToken) 163 } 164 if pageNumber != "" { 165 data.Set("Page", pageNumber) 166 } 167 168 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 169 if err != nil { 170 return nil, err 171 } 172 173 defer resp.Body.Close() 174 175 ps := &ListMemberResponse{} 176 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 177 return nil, err 178 } 179 180 return ps, err 181 } 182 183 // Lists Member records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 184 func (c *ApiService) ListMember(ServiceSid string, ChannelSid string, params *ListMemberParams) ([]ChatV1Member, error) { 185 response, errors := c.StreamMember(ServiceSid, ChannelSid, params) 186 187 records := make([]ChatV1Member, 0) 188 for record := range response { 189 records = append(records, record) 190 } 191 192 if err := <-errors; err != nil { 193 return nil, err 194 } 195 196 return records, nil 197 } 198 199 // Streams Member records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 200 func (c *ApiService) StreamMember(ServiceSid string, ChannelSid string, params *ListMemberParams) (chan ChatV1Member, chan error) { 201 if params == nil { 202 params = &ListMemberParams{} 203 } 204 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 205 206 recordChannel := make(chan ChatV1Member, 1) 207 errorChannel := make(chan error, 1) 208 209 response, err := c.PageMember(ServiceSid, ChannelSid, params, "", "") 210 if err != nil { 211 errorChannel <- err 212 close(recordChannel) 213 close(errorChannel) 214 } else { 215 go c.streamMember(response, params, recordChannel, errorChannel) 216 } 217 218 return recordChannel, errorChannel 219 } 220 221 func (c *ApiService) streamMember(response *ListMemberResponse, params *ListMemberParams, recordChannel chan ChatV1Member, errorChannel chan error) { 222 curRecord := 1 223 224 for response != nil { 225 responseRecords := response.Members 226 for item := range responseRecords { 227 recordChannel <- responseRecords[item] 228 curRecord += 1 229 if params.Limit != nil && *params.Limit < curRecord { 230 close(recordChannel) 231 close(errorChannel) 232 return 233 } 234 } 235 236 record, err := client.GetNext(c.baseURL, response, c.getNextListMemberResponse) 237 if err != nil { 238 errorChannel <- err 239 break 240 } else if record == nil { 241 break 242 } 243 244 response = record.(*ListMemberResponse) 245 } 246 247 close(recordChannel) 248 close(errorChannel) 249 } 250 251 func (c *ApiService) getNextListMemberResponse(nextPageUrl string) (interface{}, error) { 252 if nextPageUrl == "" { 253 return nil, nil 254 } 255 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 256 if err != nil { 257 return nil, err 258 } 259 260 defer resp.Body.Close() 261 262 ps := &ListMemberResponse{} 263 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 264 return nil, err 265 } 266 return ps, nil 267 } 268 269 // Optional parameters for the method 'UpdateMember' 270 type UpdateMemberParams struct { 271 // The SID of the [Role](https://www.twilio.com/docs/api/chat/rest/roles) to assign to the member. The default roles are those specified on the [Service](https://www.twilio.com/docs/chat/api/services). 272 RoleSid *string `json:"RoleSid,omitempty"` 273 // The index of the last [Message](https://www.twilio.com/docs/api/chat/rest/messages) that the Member has read within the [Channel](https://www.twilio.com/docs/api/chat/rest/channels). 274 LastConsumedMessageIndex *int `json:"LastConsumedMessageIndex,omitempty"` 275 } 276 277 func (params *UpdateMemberParams) SetRoleSid(RoleSid string) *UpdateMemberParams { 278 params.RoleSid = &RoleSid 279 return params 280 } 281 func (params *UpdateMemberParams) SetLastConsumedMessageIndex(LastConsumedMessageIndex int) *UpdateMemberParams { 282 params.LastConsumedMessageIndex = &LastConsumedMessageIndex 283 return params 284 } 285 286 // 287 func (c *ApiService) UpdateMember(ServiceSid string, ChannelSid string, Sid string, params *UpdateMemberParams) (*ChatV1Member, error) { 288 path := "/v1/Services/{ServiceSid}/Channels/{ChannelSid}/Members/{Sid}" 289 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 290 path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1) 291 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 292 293 data := url.Values{} 294 headers := make(map[string]interface{}) 295 296 if params != nil && params.RoleSid != nil { 297 data.Set("RoleSid", *params.RoleSid) 298 } 299 if params != nil && params.LastConsumedMessageIndex != nil { 300 data.Set("LastConsumedMessageIndex", fmt.Sprint(*params.LastConsumedMessageIndex)) 301 } 302 303 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 304 if err != nil { 305 return nil, err 306 } 307 308 defer resp.Body.Close() 309 310 ps := &ChatV1Member{} 311 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 312 return nil, err 313 } 314 315 return ps, err 316 }