github.com/twilio/twilio-go@v1.20.1/rest/voice/v1/connection_policies_targets.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Voice 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 'CreateConnectionPolicyTarget' 27 type CreateConnectionPolicyTargetParams struct { 28 // The SIP address you want Twilio to route your calls to. This must be a `sip:` schema. `sips` is NOT supported. 29 Target *string `json:"Target,omitempty"` 30 // A descriptive string that you create to describe the resource. It is not unique and can be up to 255 characters long. 31 FriendlyName *string `json:"FriendlyName,omitempty"` 32 // The relative importance of the target. Can be an integer from 0 to 65535, inclusive, and the default is 10. The lowest number represents the most important target. 33 Priority *int `json:"Priority,omitempty"` 34 // The value that determines the relative share of the load the Target should receive compared to other Targets with the same priority. Can be an integer from 1 to 65535, inclusive, and the default is 10. Targets with higher values receive more load than those with lower ones with the same priority. 35 Weight *int `json:"Weight,omitempty"` 36 // Whether the Target is enabled. The default is `true`. 37 Enabled *bool `json:"Enabled,omitempty"` 38 } 39 40 func (params *CreateConnectionPolicyTargetParams) SetTarget(Target string) *CreateConnectionPolicyTargetParams { 41 params.Target = &Target 42 return params 43 } 44 func (params *CreateConnectionPolicyTargetParams) SetFriendlyName(FriendlyName string) *CreateConnectionPolicyTargetParams { 45 params.FriendlyName = &FriendlyName 46 return params 47 } 48 func (params *CreateConnectionPolicyTargetParams) SetPriority(Priority int) *CreateConnectionPolicyTargetParams { 49 params.Priority = &Priority 50 return params 51 } 52 func (params *CreateConnectionPolicyTargetParams) SetWeight(Weight int) *CreateConnectionPolicyTargetParams { 53 params.Weight = &Weight 54 return params 55 } 56 func (params *CreateConnectionPolicyTargetParams) SetEnabled(Enabled bool) *CreateConnectionPolicyTargetParams { 57 params.Enabled = &Enabled 58 return params 59 } 60 61 // 62 func (c *ApiService) CreateConnectionPolicyTarget(ConnectionPolicySid string, params *CreateConnectionPolicyTargetParams) (*VoiceV1ConnectionPolicyTarget, error) { 63 path := "/v1/ConnectionPolicies/{ConnectionPolicySid}/Targets" 64 path = strings.Replace(path, "{"+"ConnectionPolicySid"+"}", ConnectionPolicySid, -1) 65 66 data := url.Values{} 67 headers := make(map[string]interface{}) 68 69 if params != nil && params.Target != nil { 70 data.Set("Target", *params.Target) 71 } 72 if params != nil && params.FriendlyName != nil { 73 data.Set("FriendlyName", *params.FriendlyName) 74 } 75 if params != nil && params.Priority != nil { 76 data.Set("Priority", fmt.Sprint(*params.Priority)) 77 } 78 if params != nil && params.Weight != nil { 79 data.Set("Weight", fmt.Sprint(*params.Weight)) 80 } 81 if params != nil && params.Enabled != nil { 82 data.Set("Enabled", fmt.Sprint(*params.Enabled)) 83 } 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 := &VoiceV1ConnectionPolicyTarget{} 93 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 94 return nil, err 95 } 96 97 return ps, err 98 } 99 100 // 101 func (c *ApiService) DeleteConnectionPolicyTarget(ConnectionPolicySid string, Sid string) error { 102 path := "/v1/ConnectionPolicies/{ConnectionPolicySid}/Targets/{Sid}" 103 path = strings.Replace(path, "{"+"ConnectionPolicySid"+"}", ConnectionPolicySid, -1) 104 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 105 106 data := url.Values{} 107 headers := make(map[string]interface{}) 108 109 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 110 if err != nil { 111 return err 112 } 113 114 defer resp.Body.Close() 115 116 return nil 117 } 118 119 // 120 func (c *ApiService) FetchConnectionPolicyTarget(ConnectionPolicySid string, Sid string) (*VoiceV1ConnectionPolicyTarget, error) { 121 path := "/v1/ConnectionPolicies/{ConnectionPolicySid}/Targets/{Sid}" 122 path = strings.Replace(path, "{"+"ConnectionPolicySid"+"}", ConnectionPolicySid, -1) 123 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 124 125 data := url.Values{} 126 headers := make(map[string]interface{}) 127 128 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 129 if err != nil { 130 return nil, err 131 } 132 133 defer resp.Body.Close() 134 135 ps := &VoiceV1ConnectionPolicyTarget{} 136 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 137 return nil, err 138 } 139 140 return ps, err 141 } 142 143 // Optional parameters for the method 'ListConnectionPolicyTarget' 144 type ListConnectionPolicyTargetParams struct { 145 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 146 PageSize *int `json:"PageSize,omitempty"` 147 // Max number of records to return. 148 Limit *int `json:"limit,omitempty"` 149 } 150 151 func (params *ListConnectionPolicyTargetParams) SetPageSize(PageSize int) *ListConnectionPolicyTargetParams { 152 params.PageSize = &PageSize 153 return params 154 } 155 func (params *ListConnectionPolicyTargetParams) SetLimit(Limit int) *ListConnectionPolicyTargetParams { 156 params.Limit = &Limit 157 return params 158 } 159 160 // Retrieve a single page of ConnectionPolicyTarget records from the API. Request is executed immediately. 161 func (c *ApiService) PageConnectionPolicyTarget(ConnectionPolicySid string, params *ListConnectionPolicyTargetParams, pageToken, pageNumber string) (*ListConnectionPolicyTargetResponse, error) { 162 path := "/v1/ConnectionPolicies/{ConnectionPolicySid}/Targets" 163 164 path = strings.Replace(path, "{"+"ConnectionPolicySid"+"}", ConnectionPolicySid, -1) 165 166 data := url.Values{} 167 headers := make(map[string]interface{}) 168 169 if params != nil && params.PageSize != nil { 170 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 171 } 172 173 if pageToken != "" { 174 data.Set("PageToken", pageToken) 175 } 176 if pageNumber != "" { 177 data.Set("Page", pageNumber) 178 } 179 180 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 181 if err != nil { 182 return nil, err 183 } 184 185 defer resp.Body.Close() 186 187 ps := &ListConnectionPolicyTargetResponse{} 188 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 189 return nil, err 190 } 191 192 return ps, err 193 } 194 195 // Lists ConnectionPolicyTarget records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 196 func (c *ApiService) ListConnectionPolicyTarget(ConnectionPolicySid string, params *ListConnectionPolicyTargetParams) ([]VoiceV1ConnectionPolicyTarget, error) { 197 response, errors := c.StreamConnectionPolicyTarget(ConnectionPolicySid, params) 198 199 records := make([]VoiceV1ConnectionPolicyTarget, 0) 200 for record := range response { 201 records = append(records, record) 202 } 203 204 if err := <-errors; err != nil { 205 return nil, err 206 } 207 208 return records, nil 209 } 210 211 // Streams ConnectionPolicyTarget records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 212 func (c *ApiService) StreamConnectionPolicyTarget(ConnectionPolicySid string, params *ListConnectionPolicyTargetParams) (chan VoiceV1ConnectionPolicyTarget, chan error) { 213 if params == nil { 214 params = &ListConnectionPolicyTargetParams{} 215 } 216 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 217 218 recordChannel := make(chan VoiceV1ConnectionPolicyTarget, 1) 219 errorChannel := make(chan error, 1) 220 221 response, err := c.PageConnectionPolicyTarget(ConnectionPolicySid, params, "", "") 222 if err != nil { 223 errorChannel <- err 224 close(recordChannel) 225 close(errorChannel) 226 } else { 227 go c.streamConnectionPolicyTarget(response, params, recordChannel, errorChannel) 228 } 229 230 return recordChannel, errorChannel 231 } 232 233 func (c *ApiService) streamConnectionPolicyTarget(response *ListConnectionPolicyTargetResponse, params *ListConnectionPolicyTargetParams, recordChannel chan VoiceV1ConnectionPolicyTarget, errorChannel chan error) { 234 curRecord := 1 235 236 for response != nil { 237 responseRecords := response.Targets 238 for item := range responseRecords { 239 recordChannel <- responseRecords[item] 240 curRecord += 1 241 if params.Limit != nil && *params.Limit < curRecord { 242 close(recordChannel) 243 close(errorChannel) 244 return 245 } 246 } 247 248 record, err := client.GetNext(c.baseURL, response, c.getNextListConnectionPolicyTargetResponse) 249 if err != nil { 250 errorChannel <- err 251 break 252 } else if record == nil { 253 break 254 } 255 256 response = record.(*ListConnectionPolicyTargetResponse) 257 } 258 259 close(recordChannel) 260 close(errorChannel) 261 } 262 263 func (c *ApiService) getNextListConnectionPolicyTargetResponse(nextPageUrl string) (interface{}, error) { 264 if nextPageUrl == "" { 265 return nil, nil 266 } 267 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 268 if err != nil { 269 return nil, err 270 } 271 272 defer resp.Body.Close() 273 274 ps := &ListConnectionPolicyTargetResponse{} 275 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 276 return nil, err 277 } 278 return ps, nil 279 } 280 281 // Optional parameters for the method 'UpdateConnectionPolicyTarget' 282 type UpdateConnectionPolicyTargetParams struct { 283 // A descriptive string that you create to describe the resource. It is not unique and can be up to 255 characters long. 284 FriendlyName *string `json:"FriendlyName,omitempty"` 285 // The SIP address you want Twilio to route your calls to. This must be a `sip:` schema. `sips` is NOT supported. 286 Target *string `json:"Target,omitempty"` 287 // The relative importance of the target. Can be an integer from 0 to 65535, inclusive. The lowest number represents the most important target. 288 Priority *int `json:"Priority,omitempty"` 289 // The value that determines the relative share of the load the Target should receive compared to other Targets with the same priority. Can be an integer from 1 to 65535, inclusive. Targets with higher values receive more load than those with lower ones with the same priority. 290 Weight *int `json:"Weight,omitempty"` 291 // Whether the Target is enabled. 292 Enabled *bool `json:"Enabled,omitempty"` 293 } 294 295 func (params *UpdateConnectionPolicyTargetParams) SetFriendlyName(FriendlyName string) *UpdateConnectionPolicyTargetParams { 296 params.FriendlyName = &FriendlyName 297 return params 298 } 299 func (params *UpdateConnectionPolicyTargetParams) SetTarget(Target string) *UpdateConnectionPolicyTargetParams { 300 params.Target = &Target 301 return params 302 } 303 func (params *UpdateConnectionPolicyTargetParams) SetPriority(Priority int) *UpdateConnectionPolicyTargetParams { 304 params.Priority = &Priority 305 return params 306 } 307 func (params *UpdateConnectionPolicyTargetParams) SetWeight(Weight int) *UpdateConnectionPolicyTargetParams { 308 params.Weight = &Weight 309 return params 310 } 311 func (params *UpdateConnectionPolicyTargetParams) SetEnabled(Enabled bool) *UpdateConnectionPolicyTargetParams { 312 params.Enabled = &Enabled 313 return params 314 } 315 316 // 317 func (c *ApiService) UpdateConnectionPolicyTarget(ConnectionPolicySid string, Sid string, params *UpdateConnectionPolicyTargetParams) (*VoiceV1ConnectionPolicyTarget, error) { 318 path := "/v1/ConnectionPolicies/{ConnectionPolicySid}/Targets/{Sid}" 319 path = strings.Replace(path, "{"+"ConnectionPolicySid"+"}", ConnectionPolicySid, -1) 320 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 321 322 data := url.Values{} 323 headers := make(map[string]interface{}) 324 325 if params != nil && params.FriendlyName != nil { 326 data.Set("FriendlyName", *params.FriendlyName) 327 } 328 if params != nil && params.Target != nil { 329 data.Set("Target", *params.Target) 330 } 331 if params != nil && params.Priority != nil { 332 data.Set("Priority", fmt.Sprint(*params.Priority)) 333 } 334 if params != nil && params.Weight != nil { 335 data.Set("Weight", fmt.Sprint(*params.Weight)) 336 } 337 if params != nil && params.Enabled != nil { 338 data.Set("Enabled", fmt.Sprint(*params.Enabled)) 339 } 340 341 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 342 if err != nil { 343 return nil, err 344 } 345 346 defer resp.Body.Close() 347 348 ps := &VoiceV1ConnectionPolicyTarget{} 349 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 350 return nil, err 351 } 352 353 return ps, err 354 }