github.com/twilio/twilio-go@v1.20.1/rest/taskrouter/v1/workspaces_workers_channels.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 // 27 func (c *ApiService) FetchWorkerChannel(WorkspaceSid string, WorkerSid string, Sid string) (*TaskrouterV1WorkerChannel, error) { 28 path := "/v1/Workspaces/{WorkspaceSid}/Workers/{WorkerSid}/Channels/{Sid}" 29 path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1) 30 path = strings.Replace(path, "{"+"WorkerSid"+"}", WorkerSid, -1) 31 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 32 33 data := url.Values{} 34 headers := make(map[string]interface{}) 35 36 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 37 if err != nil { 38 return nil, err 39 } 40 41 defer resp.Body.Close() 42 43 ps := &TaskrouterV1WorkerChannel{} 44 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 45 return nil, err 46 } 47 48 return ps, err 49 } 50 51 // Optional parameters for the method 'ListWorkerChannel' 52 type ListWorkerChannelParams struct { 53 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 54 PageSize *int `json:"PageSize,omitempty"` 55 // Max number of records to return. 56 Limit *int `json:"limit,omitempty"` 57 } 58 59 func (params *ListWorkerChannelParams) SetPageSize(PageSize int) *ListWorkerChannelParams { 60 params.PageSize = &PageSize 61 return params 62 } 63 func (params *ListWorkerChannelParams) SetLimit(Limit int) *ListWorkerChannelParams { 64 params.Limit = &Limit 65 return params 66 } 67 68 // Retrieve a single page of WorkerChannel records from the API. Request is executed immediately. 69 func (c *ApiService) PageWorkerChannel(WorkspaceSid string, WorkerSid string, params *ListWorkerChannelParams, pageToken, pageNumber string) (*ListWorkerChannelResponse, error) { 70 path := "/v1/Workspaces/{WorkspaceSid}/Workers/{WorkerSid}/Channels" 71 72 path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1) 73 path = strings.Replace(path, "{"+"WorkerSid"+"}", WorkerSid, -1) 74 75 data := url.Values{} 76 headers := make(map[string]interface{}) 77 78 if params != nil && params.PageSize != nil { 79 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 80 } 81 82 if pageToken != "" { 83 data.Set("PageToken", pageToken) 84 } 85 if pageNumber != "" { 86 data.Set("Page", pageNumber) 87 } 88 89 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 90 if err != nil { 91 return nil, err 92 } 93 94 defer resp.Body.Close() 95 96 ps := &ListWorkerChannelResponse{} 97 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 98 return nil, err 99 } 100 101 return ps, err 102 } 103 104 // Lists WorkerChannel records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 105 func (c *ApiService) ListWorkerChannel(WorkspaceSid string, WorkerSid string, params *ListWorkerChannelParams) ([]TaskrouterV1WorkerChannel, error) { 106 response, errors := c.StreamWorkerChannel(WorkspaceSid, WorkerSid, params) 107 108 records := make([]TaskrouterV1WorkerChannel, 0) 109 for record := range response { 110 records = append(records, record) 111 } 112 113 if err := <-errors; err != nil { 114 return nil, err 115 } 116 117 return records, nil 118 } 119 120 // Streams WorkerChannel records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 121 func (c *ApiService) StreamWorkerChannel(WorkspaceSid string, WorkerSid string, params *ListWorkerChannelParams) (chan TaskrouterV1WorkerChannel, chan error) { 122 if params == nil { 123 params = &ListWorkerChannelParams{} 124 } 125 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 126 127 recordChannel := make(chan TaskrouterV1WorkerChannel, 1) 128 errorChannel := make(chan error, 1) 129 130 response, err := c.PageWorkerChannel(WorkspaceSid, WorkerSid, params, "", "") 131 if err != nil { 132 errorChannel <- err 133 close(recordChannel) 134 close(errorChannel) 135 } else { 136 go c.streamWorkerChannel(response, params, recordChannel, errorChannel) 137 } 138 139 return recordChannel, errorChannel 140 } 141 142 func (c *ApiService) streamWorkerChannel(response *ListWorkerChannelResponse, params *ListWorkerChannelParams, recordChannel chan TaskrouterV1WorkerChannel, errorChannel chan error) { 143 curRecord := 1 144 145 for response != nil { 146 responseRecords := response.Channels 147 for item := range responseRecords { 148 recordChannel <- responseRecords[item] 149 curRecord += 1 150 if params.Limit != nil && *params.Limit < curRecord { 151 close(recordChannel) 152 close(errorChannel) 153 return 154 } 155 } 156 157 record, err := client.GetNext(c.baseURL, response, c.getNextListWorkerChannelResponse) 158 if err != nil { 159 errorChannel <- err 160 break 161 } else if record == nil { 162 break 163 } 164 165 response = record.(*ListWorkerChannelResponse) 166 } 167 168 close(recordChannel) 169 close(errorChannel) 170 } 171 172 func (c *ApiService) getNextListWorkerChannelResponse(nextPageUrl string) (interface{}, error) { 173 if nextPageUrl == "" { 174 return nil, nil 175 } 176 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 177 if err != nil { 178 return nil, err 179 } 180 181 defer resp.Body.Close() 182 183 ps := &ListWorkerChannelResponse{} 184 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 185 return nil, err 186 } 187 return ps, nil 188 } 189 190 // Optional parameters for the method 'UpdateWorkerChannel' 191 type UpdateWorkerChannelParams struct { 192 // The total number of Tasks that the Worker should handle for the TaskChannel type. TaskRouter creates reservations for Tasks of this TaskChannel type up to the specified capacity. If the capacity is 0, no new reservations will be created. 193 Capacity *int `json:"Capacity,omitempty"` 194 // Whether the WorkerChannel is available. Set to `false` to prevent the Worker from receiving any new Tasks of this TaskChannel type. 195 Available *bool `json:"Available,omitempty"` 196 } 197 198 func (params *UpdateWorkerChannelParams) SetCapacity(Capacity int) *UpdateWorkerChannelParams { 199 params.Capacity = &Capacity 200 return params 201 } 202 func (params *UpdateWorkerChannelParams) SetAvailable(Available bool) *UpdateWorkerChannelParams { 203 params.Available = &Available 204 return params 205 } 206 207 // 208 func (c *ApiService) UpdateWorkerChannel(WorkspaceSid string, WorkerSid string, Sid string, params *UpdateWorkerChannelParams) (*TaskrouterV1WorkerChannel, error) { 209 path := "/v1/Workspaces/{WorkspaceSid}/Workers/{WorkerSid}/Channels/{Sid}" 210 path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1) 211 path = strings.Replace(path, "{"+"WorkerSid"+"}", WorkerSid, -1) 212 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 213 214 data := url.Values{} 215 headers := make(map[string]interface{}) 216 217 if params != nil && params.Capacity != nil { 218 data.Set("Capacity", fmt.Sprint(*params.Capacity)) 219 } 220 if params != nil && params.Available != nil { 221 data.Set("Available", fmt.Sprint(*params.Available)) 222 } 223 224 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 225 if err != nil { 226 return nil, err 227 } 228 229 defer resp.Body.Close() 230 231 ps := &TaskrouterV1WorkerChannel{} 232 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 233 return nil, err 234 } 235 236 return ps, err 237 }