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