github.com/twilio/twilio-go@v1.20.1/rest/conversations/v1/services_users.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Conversations 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 'CreateServiceUser' 27 type CreateServiceUserParams struct { 28 // The X-Twilio-Webhook-Enabled HTTP request header 29 XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"` 30 // The application-defined string that uniquely identifies the resource's User within the [Conversation Service](https://www.twilio.com/docs/conversations/api/service-resource). This value is often a username or an email address, and is case-sensitive. 31 Identity *string `json:"Identity,omitempty"` 32 // The string that you assigned to describe the resource. 33 FriendlyName *string `json:"FriendlyName,omitempty"` 34 // The JSON Object string that stores application-specific data. If attributes have not been set, `{}` is returned. 35 Attributes *string `json:"Attributes,omitempty"` 36 // The SID of a service-level [Role](https://www.twilio.com/docs/conversations/api/role-resource) to assign to the user. 37 RoleSid *string `json:"RoleSid,omitempty"` 38 } 39 40 func (params *CreateServiceUserParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *CreateServiceUserParams { 41 params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled 42 return params 43 } 44 func (params *CreateServiceUserParams) SetIdentity(Identity string) *CreateServiceUserParams { 45 params.Identity = &Identity 46 return params 47 } 48 func (params *CreateServiceUserParams) SetFriendlyName(FriendlyName string) *CreateServiceUserParams { 49 params.FriendlyName = &FriendlyName 50 return params 51 } 52 func (params *CreateServiceUserParams) SetAttributes(Attributes string) *CreateServiceUserParams { 53 params.Attributes = &Attributes 54 return params 55 } 56 func (params *CreateServiceUserParams) SetRoleSid(RoleSid string) *CreateServiceUserParams { 57 params.RoleSid = &RoleSid 58 return params 59 } 60 61 // Add a new conversation user to your service 62 func (c *ApiService) CreateServiceUser(ChatServiceSid string, params *CreateServiceUserParams) (*ConversationsV1ServiceUser, error) { 63 path := "/v1/Services/{ChatServiceSid}/Users" 64 path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1) 65 66 data := url.Values{} 67 headers := make(map[string]interface{}) 68 69 if params != nil && params.Identity != nil { 70 data.Set("Identity", *params.Identity) 71 } 72 if params != nil && params.FriendlyName != nil { 73 data.Set("FriendlyName", *params.FriendlyName) 74 } 75 if params != nil && params.Attributes != nil { 76 data.Set("Attributes", *params.Attributes) 77 } 78 if params != nil && params.RoleSid != nil { 79 data.Set("RoleSid", *params.RoleSid) 80 } 81 82 if params != nil && params.XTwilioWebhookEnabled != nil { 83 headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled 84 } 85 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 86 if err != nil { 87 return nil, err 88 } 89 90 defer resp.Body.Close() 91 92 ps := &ConversationsV1ServiceUser{} 93 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 94 return nil, err 95 } 96 97 return ps, err 98 } 99 100 // Optional parameters for the method 'DeleteServiceUser' 101 type DeleteServiceUserParams struct { 102 // The X-Twilio-Webhook-Enabled HTTP request header 103 XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"` 104 } 105 106 func (params *DeleteServiceUserParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *DeleteServiceUserParams { 107 params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled 108 return params 109 } 110 111 // Remove a conversation user from your service 112 func (c *ApiService) DeleteServiceUser(ChatServiceSid string, Sid string, params *DeleteServiceUserParams) error { 113 path := "/v1/Services/{ChatServiceSid}/Users/{Sid}" 114 path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1) 115 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 116 117 data := url.Values{} 118 headers := make(map[string]interface{}) 119 120 if params != nil && params.XTwilioWebhookEnabled != nil { 121 headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled 122 } 123 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 124 if err != nil { 125 return err 126 } 127 128 defer resp.Body.Close() 129 130 return nil 131 } 132 133 // Fetch a conversation user from your service 134 func (c *ApiService) FetchServiceUser(ChatServiceSid string, Sid string) (*ConversationsV1ServiceUser, error) { 135 path := "/v1/Services/{ChatServiceSid}/Users/{Sid}" 136 path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1) 137 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 138 139 data := url.Values{} 140 headers := make(map[string]interface{}) 141 142 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 143 if err != nil { 144 return nil, err 145 } 146 147 defer resp.Body.Close() 148 149 ps := &ConversationsV1ServiceUser{} 150 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 151 return nil, err 152 } 153 154 return ps, err 155 } 156 157 // Optional parameters for the method 'ListServiceUser' 158 type ListServiceUserParams struct { 159 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 160 PageSize *int `json:"PageSize,omitempty"` 161 // Max number of records to return. 162 Limit *int `json:"limit,omitempty"` 163 } 164 165 func (params *ListServiceUserParams) SetPageSize(PageSize int) *ListServiceUserParams { 166 params.PageSize = &PageSize 167 return params 168 } 169 func (params *ListServiceUserParams) SetLimit(Limit int) *ListServiceUserParams { 170 params.Limit = &Limit 171 return params 172 } 173 174 // Retrieve a single page of ServiceUser records from the API. Request is executed immediately. 175 func (c *ApiService) PageServiceUser(ChatServiceSid string, params *ListServiceUserParams, pageToken, pageNumber string) (*ListServiceUserResponse, error) { 176 path := "/v1/Services/{ChatServiceSid}/Users" 177 178 path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1) 179 180 data := url.Values{} 181 headers := make(map[string]interface{}) 182 183 if params != nil && params.PageSize != nil { 184 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 185 } 186 187 if pageToken != "" { 188 data.Set("PageToken", pageToken) 189 } 190 if pageNumber != "" { 191 data.Set("Page", pageNumber) 192 } 193 194 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 195 if err != nil { 196 return nil, err 197 } 198 199 defer resp.Body.Close() 200 201 ps := &ListServiceUserResponse{} 202 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 203 return nil, err 204 } 205 206 return ps, err 207 } 208 209 // Lists ServiceUser records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 210 func (c *ApiService) ListServiceUser(ChatServiceSid string, params *ListServiceUserParams) ([]ConversationsV1ServiceUser, error) { 211 response, errors := c.StreamServiceUser(ChatServiceSid, params) 212 213 records := make([]ConversationsV1ServiceUser, 0) 214 for record := range response { 215 records = append(records, record) 216 } 217 218 if err := <-errors; err != nil { 219 return nil, err 220 } 221 222 return records, nil 223 } 224 225 // Streams ServiceUser records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 226 func (c *ApiService) StreamServiceUser(ChatServiceSid string, params *ListServiceUserParams) (chan ConversationsV1ServiceUser, chan error) { 227 if params == nil { 228 params = &ListServiceUserParams{} 229 } 230 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 231 232 recordChannel := make(chan ConversationsV1ServiceUser, 1) 233 errorChannel := make(chan error, 1) 234 235 response, err := c.PageServiceUser(ChatServiceSid, params, "", "") 236 if err != nil { 237 errorChannel <- err 238 close(recordChannel) 239 close(errorChannel) 240 } else { 241 go c.streamServiceUser(response, params, recordChannel, errorChannel) 242 } 243 244 return recordChannel, errorChannel 245 } 246 247 func (c *ApiService) streamServiceUser(response *ListServiceUserResponse, params *ListServiceUserParams, recordChannel chan ConversationsV1ServiceUser, errorChannel chan error) { 248 curRecord := 1 249 250 for response != nil { 251 responseRecords := response.Users 252 for item := range responseRecords { 253 recordChannel <- responseRecords[item] 254 curRecord += 1 255 if params.Limit != nil && *params.Limit < curRecord { 256 close(recordChannel) 257 close(errorChannel) 258 return 259 } 260 } 261 262 record, err := client.GetNext(c.baseURL, response, c.getNextListServiceUserResponse) 263 if err != nil { 264 errorChannel <- err 265 break 266 } else if record == nil { 267 break 268 } 269 270 response = record.(*ListServiceUserResponse) 271 } 272 273 close(recordChannel) 274 close(errorChannel) 275 } 276 277 func (c *ApiService) getNextListServiceUserResponse(nextPageUrl string) (interface{}, error) { 278 if nextPageUrl == "" { 279 return nil, nil 280 } 281 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 282 if err != nil { 283 return nil, err 284 } 285 286 defer resp.Body.Close() 287 288 ps := &ListServiceUserResponse{} 289 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 290 return nil, err 291 } 292 return ps, nil 293 } 294 295 // Optional parameters for the method 'UpdateServiceUser' 296 type UpdateServiceUserParams struct { 297 // The X-Twilio-Webhook-Enabled HTTP request header 298 XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"` 299 // The string that you assigned to describe the resource. 300 FriendlyName *string `json:"FriendlyName,omitempty"` 301 // The JSON Object string that stores application-specific data. If attributes have not been set, `{}` is returned. 302 Attributes *string `json:"Attributes,omitempty"` 303 // The SID of a service-level [Role](https://www.twilio.com/docs/conversations/api/role-resource) to assign to the user. 304 RoleSid *string `json:"RoleSid,omitempty"` 305 } 306 307 func (params *UpdateServiceUserParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *UpdateServiceUserParams { 308 params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled 309 return params 310 } 311 func (params *UpdateServiceUserParams) SetFriendlyName(FriendlyName string) *UpdateServiceUserParams { 312 params.FriendlyName = &FriendlyName 313 return params 314 } 315 func (params *UpdateServiceUserParams) SetAttributes(Attributes string) *UpdateServiceUserParams { 316 params.Attributes = &Attributes 317 return params 318 } 319 func (params *UpdateServiceUserParams) SetRoleSid(RoleSid string) *UpdateServiceUserParams { 320 params.RoleSid = &RoleSid 321 return params 322 } 323 324 // Update an existing conversation user in your service 325 func (c *ApiService) UpdateServiceUser(ChatServiceSid string, Sid string, params *UpdateServiceUserParams) (*ConversationsV1ServiceUser, error) { 326 path := "/v1/Services/{ChatServiceSid}/Users/{Sid}" 327 path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1) 328 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 329 330 data := url.Values{} 331 headers := make(map[string]interface{}) 332 333 if params != nil && params.FriendlyName != nil { 334 data.Set("FriendlyName", *params.FriendlyName) 335 } 336 if params != nil && params.Attributes != nil { 337 data.Set("Attributes", *params.Attributes) 338 } 339 if params != nil && params.RoleSid != nil { 340 data.Set("RoleSid", *params.RoleSid) 341 } 342 343 if params != nil && params.XTwilioWebhookEnabled != nil { 344 headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled 345 } 346 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 347 if err != nil { 348 return nil, err 349 } 350 351 defer resp.Body.Close() 352 353 ps := &ConversationsV1ServiceUser{} 354 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 355 return nil, err 356 } 357 358 return ps, err 359 }