github.com/twilio/twilio-go@v1.20.1/rest/taskrouter/v1/workspaces_workers.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Taskrouter 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 'CreateWorker' 27 type CreateWorkerParams struct { 28 // A descriptive string that you create to describe the new Worker. It can be up to 64 characters long. 29 FriendlyName *string `json:"FriendlyName,omitempty"` 30 // The SID of a valid Activity that will describe the new Worker's initial state. See [Activities](https://www.twilio.com/docs/taskrouter/api/activity) for more information. If not provided, the new Worker's initial state is the `default_activity_sid` configured on the Workspace. 31 ActivitySid *string `json:"ActivitySid,omitempty"` 32 // A valid JSON string that describes the new Worker. For example: `{ \\\"email\\\": \\\"Bob@example.com\\\", \\\"phone\\\": \\\"+5095551234\\\" }`. This data is passed to the `assignment_callback_url` when TaskRouter assigns a Task to the Worker. Defaults to {}. 33 Attributes *string `json:"Attributes,omitempty"` 34 } 35 36 func (params *CreateWorkerParams) SetFriendlyName(FriendlyName string) *CreateWorkerParams { 37 params.FriendlyName = &FriendlyName 38 return params 39 } 40 func (params *CreateWorkerParams) SetActivitySid(ActivitySid string) *CreateWorkerParams { 41 params.ActivitySid = &ActivitySid 42 return params 43 } 44 func (params *CreateWorkerParams) SetAttributes(Attributes string) *CreateWorkerParams { 45 params.Attributes = &Attributes 46 return params 47 } 48 49 // 50 func (c *ApiService) CreateWorker(WorkspaceSid string, params *CreateWorkerParams) (*TaskrouterV1Worker, error) { 51 path := "/v1/Workspaces/{WorkspaceSid}/Workers" 52 path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1) 53 54 data := url.Values{} 55 headers := make(map[string]interface{}) 56 57 if params != nil && params.FriendlyName != nil { 58 data.Set("FriendlyName", *params.FriendlyName) 59 } 60 if params != nil && params.ActivitySid != nil { 61 data.Set("ActivitySid", *params.ActivitySid) 62 } 63 if params != nil && params.Attributes != nil { 64 data.Set("Attributes", *params.Attributes) 65 } 66 67 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 68 if err != nil { 69 return nil, err 70 } 71 72 defer resp.Body.Close() 73 74 ps := &TaskrouterV1Worker{} 75 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 76 return nil, err 77 } 78 79 return ps, err 80 } 81 82 // Optional parameters for the method 'DeleteWorker' 83 type DeleteWorkerParams struct { 84 // The If-Match HTTP request header 85 IfMatch *string `json:"If-Match,omitempty"` 86 } 87 88 func (params *DeleteWorkerParams) SetIfMatch(IfMatch string) *DeleteWorkerParams { 89 params.IfMatch = &IfMatch 90 return params 91 } 92 93 // 94 func (c *ApiService) DeleteWorker(WorkspaceSid string, Sid string, params *DeleteWorkerParams) error { 95 path := "/v1/Workspaces/{WorkspaceSid}/Workers/{Sid}" 96 path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1) 97 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 98 99 data := url.Values{} 100 headers := make(map[string]interface{}) 101 102 if params != nil && params.IfMatch != nil { 103 headers["If-Match"] = *params.IfMatch 104 } 105 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 106 if err != nil { 107 return err 108 } 109 110 defer resp.Body.Close() 111 112 return nil 113 } 114 115 // 116 func (c *ApiService) FetchWorker(WorkspaceSid string, Sid string) (*TaskrouterV1Worker, error) { 117 path := "/v1/Workspaces/{WorkspaceSid}/Workers/{Sid}" 118 path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1) 119 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 120 121 data := url.Values{} 122 headers := make(map[string]interface{}) 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 := &TaskrouterV1Worker{} 132 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 133 return nil, err 134 } 135 136 return ps, err 137 } 138 139 // Optional parameters for the method 'ListWorker' 140 type ListWorkerParams struct { 141 // The `activity_name` of the Worker resources to read. 142 ActivityName *string `json:"ActivityName,omitempty"` 143 // The `activity_sid` of the Worker resources to read. 144 ActivitySid *string `json:"ActivitySid,omitempty"` 145 // Whether to return only Worker resources that are available or unavailable. Can be `true`, `1`, or `yes` to return Worker resources that are available, and `false`, or any value returns the Worker resources that are not available. 146 Available *string `json:"Available,omitempty"` 147 // The `friendly_name` of the Worker resources to read. 148 FriendlyName *string `json:"FriendlyName,omitempty"` 149 // Filter by Workers that would match an expression. In addition to fields in the workers' attributes, the expression can include the following worker fields: `sid`, `friendly_name`, `activity_sid`, or `activity_name` 150 TargetWorkersExpression *string `json:"TargetWorkersExpression,omitempty"` 151 // The `friendly_name` of the TaskQueue that the Workers to read are eligible for. 152 TaskQueueName *string `json:"TaskQueueName,omitempty"` 153 // The SID of the TaskQueue that the Workers to read are eligible for. 154 TaskQueueSid *string `json:"TaskQueueSid,omitempty"` 155 // Sorting parameter for Workers 156 Ordering *string `json:"Ordering,omitempty"` 157 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 158 PageSize *int `json:"PageSize,omitempty"` 159 // Max number of records to return. 160 Limit *int `json:"limit,omitempty"` 161 } 162 163 func (params *ListWorkerParams) SetActivityName(ActivityName string) *ListWorkerParams { 164 params.ActivityName = &ActivityName 165 return params 166 } 167 func (params *ListWorkerParams) SetActivitySid(ActivitySid string) *ListWorkerParams { 168 params.ActivitySid = &ActivitySid 169 return params 170 } 171 func (params *ListWorkerParams) SetAvailable(Available string) *ListWorkerParams { 172 params.Available = &Available 173 return params 174 } 175 func (params *ListWorkerParams) SetFriendlyName(FriendlyName string) *ListWorkerParams { 176 params.FriendlyName = &FriendlyName 177 return params 178 } 179 func (params *ListWorkerParams) SetTargetWorkersExpression(TargetWorkersExpression string) *ListWorkerParams { 180 params.TargetWorkersExpression = &TargetWorkersExpression 181 return params 182 } 183 func (params *ListWorkerParams) SetTaskQueueName(TaskQueueName string) *ListWorkerParams { 184 params.TaskQueueName = &TaskQueueName 185 return params 186 } 187 func (params *ListWorkerParams) SetTaskQueueSid(TaskQueueSid string) *ListWorkerParams { 188 params.TaskQueueSid = &TaskQueueSid 189 return params 190 } 191 func (params *ListWorkerParams) SetOrdering(Ordering string) *ListWorkerParams { 192 params.Ordering = &Ordering 193 return params 194 } 195 func (params *ListWorkerParams) SetPageSize(PageSize int) *ListWorkerParams { 196 params.PageSize = &PageSize 197 return params 198 } 199 func (params *ListWorkerParams) SetLimit(Limit int) *ListWorkerParams { 200 params.Limit = &Limit 201 return params 202 } 203 204 // Retrieve a single page of Worker records from the API. Request is executed immediately. 205 func (c *ApiService) PageWorker(WorkspaceSid string, params *ListWorkerParams, pageToken, pageNumber string) (*ListWorkerResponse, error) { 206 path := "/v1/Workspaces/{WorkspaceSid}/Workers" 207 208 path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1) 209 210 data := url.Values{} 211 headers := make(map[string]interface{}) 212 213 if params != nil && params.ActivityName != nil { 214 data.Set("ActivityName", *params.ActivityName) 215 } 216 if params != nil && params.ActivitySid != nil { 217 data.Set("ActivitySid", *params.ActivitySid) 218 } 219 if params != nil && params.Available != nil { 220 data.Set("Available", *params.Available) 221 } 222 if params != nil && params.FriendlyName != nil { 223 data.Set("FriendlyName", *params.FriendlyName) 224 } 225 if params != nil && params.TargetWorkersExpression != nil { 226 data.Set("TargetWorkersExpression", *params.TargetWorkersExpression) 227 } 228 if params != nil && params.TaskQueueName != nil { 229 data.Set("TaskQueueName", *params.TaskQueueName) 230 } 231 if params != nil && params.TaskQueueSid != nil { 232 data.Set("TaskQueueSid", *params.TaskQueueSid) 233 } 234 if params != nil && params.Ordering != nil { 235 data.Set("Ordering", *params.Ordering) 236 } 237 if params != nil && params.PageSize != nil { 238 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 239 } 240 241 if pageToken != "" { 242 data.Set("PageToken", pageToken) 243 } 244 if pageNumber != "" { 245 data.Set("Page", pageNumber) 246 } 247 248 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 249 if err != nil { 250 return nil, err 251 } 252 253 defer resp.Body.Close() 254 255 ps := &ListWorkerResponse{} 256 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 257 return nil, err 258 } 259 260 return ps, err 261 } 262 263 // Lists Worker records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 264 func (c *ApiService) ListWorker(WorkspaceSid string, params *ListWorkerParams) ([]TaskrouterV1Worker, error) { 265 response, errors := c.StreamWorker(WorkspaceSid, params) 266 267 records := make([]TaskrouterV1Worker, 0) 268 for record := range response { 269 records = append(records, record) 270 } 271 272 if err := <-errors; err != nil { 273 return nil, err 274 } 275 276 return records, nil 277 } 278 279 // Streams Worker records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 280 func (c *ApiService) StreamWorker(WorkspaceSid string, params *ListWorkerParams) (chan TaskrouterV1Worker, chan error) { 281 if params == nil { 282 params = &ListWorkerParams{} 283 } 284 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 285 286 recordChannel := make(chan TaskrouterV1Worker, 1) 287 errorChannel := make(chan error, 1) 288 289 response, err := c.PageWorker(WorkspaceSid, params, "", "") 290 if err != nil { 291 errorChannel <- err 292 close(recordChannel) 293 close(errorChannel) 294 } else { 295 go c.streamWorker(response, params, recordChannel, errorChannel) 296 } 297 298 return recordChannel, errorChannel 299 } 300 301 func (c *ApiService) streamWorker(response *ListWorkerResponse, params *ListWorkerParams, recordChannel chan TaskrouterV1Worker, errorChannel chan error) { 302 curRecord := 1 303 304 for response != nil { 305 responseRecords := response.Workers 306 for item := range responseRecords { 307 recordChannel <- responseRecords[item] 308 curRecord += 1 309 if params.Limit != nil && *params.Limit < curRecord { 310 close(recordChannel) 311 close(errorChannel) 312 return 313 } 314 } 315 316 record, err := client.GetNext(c.baseURL, response, c.getNextListWorkerResponse) 317 if err != nil { 318 errorChannel <- err 319 break 320 } else if record == nil { 321 break 322 } 323 324 response = record.(*ListWorkerResponse) 325 } 326 327 close(recordChannel) 328 close(errorChannel) 329 } 330 331 func (c *ApiService) getNextListWorkerResponse(nextPageUrl string) (interface{}, error) { 332 if nextPageUrl == "" { 333 return nil, nil 334 } 335 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 336 if err != nil { 337 return nil, err 338 } 339 340 defer resp.Body.Close() 341 342 ps := &ListWorkerResponse{} 343 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 344 return nil, err 345 } 346 return ps, nil 347 } 348 349 // Optional parameters for the method 'UpdateWorker' 350 type UpdateWorkerParams struct { 351 // The If-Match HTTP request header 352 IfMatch *string `json:"If-Match,omitempty"` 353 // The SID of a valid Activity that will describe the Worker's initial state. See [Activities](https://www.twilio.com/docs/taskrouter/api/activity) for more information. 354 ActivitySid *string `json:"ActivitySid,omitempty"` 355 // The JSON string that describes the Worker. For example: `{ \\\"email\\\": \\\"Bob@example.com\\\", \\\"phone\\\": \\\"+5095551234\\\" }`. This data is passed to the `assignment_callback_url` when TaskRouter assigns a Task to the Worker. Defaults to {}. 356 Attributes *string `json:"Attributes,omitempty"` 357 // A descriptive string that you create to describe the Worker. It can be up to 64 characters long. 358 FriendlyName *string `json:"FriendlyName,omitempty"` 359 // Whether to reject the Worker's pending reservations. This option is only valid if the Worker's new [Activity](https://www.twilio.com/docs/taskrouter/api/activity) resource has its `availability` property set to `False`. 360 RejectPendingReservations *bool `json:"RejectPendingReservations,omitempty"` 361 } 362 363 func (params *UpdateWorkerParams) SetIfMatch(IfMatch string) *UpdateWorkerParams { 364 params.IfMatch = &IfMatch 365 return params 366 } 367 func (params *UpdateWorkerParams) SetActivitySid(ActivitySid string) *UpdateWorkerParams { 368 params.ActivitySid = &ActivitySid 369 return params 370 } 371 func (params *UpdateWorkerParams) SetAttributes(Attributes string) *UpdateWorkerParams { 372 params.Attributes = &Attributes 373 return params 374 } 375 func (params *UpdateWorkerParams) SetFriendlyName(FriendlyName string) *UpdateWorkerParams { 376 params.FriendlyName = &FriendlyName 377 return params 378 } 379 func (params *UpdateWorkerParams) SetRejectPendingReservations(RejectPendingReservations bool) *UpdateWorkerParams { 380 params.RejectPendingReservations = &RejectPendingReservations 381 return params 382 } 383 384 // 385 func (c *ApiService) UpdateWorker(WorkspaceSid string, Sid string, params *UpdateWorkerParams) (*TaskrouterV1Worker, error) { 386 path := "/v1/Workspaces/{WorkspaceSid}/Workers/{Sid}" 387 path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1) 388 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 389 390 data := url.Values{} 391 headers := make(map[string]interface{}) 392 393 if params != nil && params.ActivitySid != nil { 394 data.Set("ActivitySid", *params.ActivitySid) 395 } 396 if params != nil && params.Attributes != nil { 397 data.Set("Attributes", *params.Attributes) 398 } 399 if params != nil && params.FriendlyName != nil { 400 data.Set("FriendlyName", *params.FriendlyName) 401 } 402 if params != nil && params.RejectPendingReservations != nil { 403 data.Set("RejectPendingReservations", fmt.Sprint(*params.RejectPendingReservations)) 404 } 405 406 if params != nil && params.IfMatch != nil { 407 headers["If-Match"] = *params.IfMatch 408 } 409 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 410 if err != nil { 411 return nil, err 412 } 413 414 defer resp.Body.Close() 415 416 ps := &TaskrouterV1Worker{} 417 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 418 return nil, err 419 } 420 421 return ps, err 422 }