github.com/twilio/twilio-go@v1.20.1/rest/voice/v1/connection_policies.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 'CreateConnectionPolicy' 27 type CreateConnectionPolicyParams struct { 28 // A descriptive string that you create to describe the resource. It is not unique and can be up to 255 characters long. 29 FriendlyName *string `json:"FriendlyName,omitempty"` 30 } 31 32 func (params *CreateConnectionPolicyParams) SetFriendlyName(FriendlyName string) *CreateConnectionPolicyParams { 33 params.FriendlyName = &FriendlyName 34 return params 35 } 36 37 // 38 func (c *ApiService) CreateConnectionPolicy(params *CreateConnectionPolicyParams) (*VoiceV1ConnectionPolicy, error) { 39 path := "/v1/ConnectionPolicies" 40 41 data := url.Values{} 42 headers := make(map[string]interface{}) 43 44 if params != nil && params.FriendlyName != nil { 45 data.Set("FriendlyName", *params.FriendlyName) 46 } 47 48 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 49 if err != nil { 50 return nil, err 51 } 52 53 defer resp.Body.Close() 54 55 ps := &VoiceV1ConnectionPolicy{} 56 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 57 return nil, err 58 } 59 60 return ps, err 61 } 62 63 // 64 func (c *ApiService) DeleteConnectionPolicy(Sid string) error { 65 path := "/v1/ConnectionPolicies/{Sid}" 66 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 67 68 data := url.Values{} 69 headers := make(map[string]interface{}) 70 71 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 72 if err != nil { 73 return err 74 } 75 76 defer resp.Body.Close() 77 78 return nil 79 } 80 81 // 82 func (c *ApiService) FetchConnectionPolicy(Sid string) (*VoiceV1ConnectionPolicy, error) { 83 path := "/v1/ConnectionPolicies/{Sid}" 84 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 85 86 data := url.Values{} 87 headers := make(map[string]interface{}) 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 := &VoiceV1ConnectionPolicy{} 97 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 98 return nil, err 99 } 100 101 return ps, err 102 } 103 104 // Optional parameters for the method 'ListConnectionPolicy' 105 type ListConnectionPolicyParams struct { 106 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 107 PageSize *int `json:"PageSize,omitempty"` 108 // Max number of records to return. 109 Limit *int `json:"limit,omitempty"` 110 } 111 112 func (params *ListConnectionPolicyParams) SetPageSize(PageSize int) *ListConnectionPolicyParams { 113 params.PageSize = &PageSize 114 return params 115 } 116 func (params *ListConnectionPolicyParams) SetLimit(Limit int) *ListConnectionPolicyParams { 117 params.Limit = &Limit 118 return params 119 } 120 121 // Retrieve a single page of ConnectionPolicy records from the API. Request is executed immediately. 122 func (c *ApiService) PageConnectionPolicy(params *ListConnectionPolicyParams, pageToken, pageNumber string) (*ListConnectionPolicyResponse, error) { 123 path := "/v1/ConnectionPolicies" 124 125 data := url.Values{} 126 headers := make(map[string]interface{}) 127 128 if params != nil && params.PageSize != nil { 129 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 130 } 131 132 if pageToken != "" { 133 data.Set("PageToken", pageToken) 134 } 135 if pageNumber != "" { 136 data.Set("Page", pageNumber) 137 } 138 139 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 140 if err != nil { 141 return nil, err 142 } 143 144 defer resp.Body.Close() 145 146 ps := &ListConnectionPolicyResponse{} 147 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 148 return nil, err 149 } 150 151 return ps, err 152 } 153 154 // Lists ConnectionPolicy records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 155 func (c *ApiService) ListConnectionPolicy(params *ListConnectionPolicyParams) ([]VoiceV1ConnectionPolicy, error) { 156 response, errors := c.StreamConnectionPolicy(params) 157 158 records := make([]VoiceV1ConnectionPolicy, 0) 159 for record := range response { 160 records = append(records, record) 161 } 162 163 if err := <-errors; err != nil { 164 return nil, err 165 } 166 167 return records, nil 168 } 169 170 // Streams ConnectionPolicy records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 171 func (c *ApiService) StreamConnectionPolicy(params *ListConnectionPolicyParams) (chan VoiceV1ConnectionPolicy, chan error) { 172 if params == nil { 173 params = &ListConnectionPolicyParams{} 174 } 175 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 176 177 recordChannel := make(chan VoiceV1ConnectionPolicy, 1) 178 errorChannel := make(chan error, 1) 179 180 response, err := c.PageConnectionPolicy(params, "", "") 181 if err != nil { 182 errorChannel <- err 183 close(recordChannel) 184 close(errorChannel) 185 } else { 186 go c.streamConnectionPolicy(response, params, recordChannel, errorChannel) 187 } 188 189 return recordChannel, errorChannel 190 } 191 192 func (c *ApiService) streamConnectionPolicy(response *ListConnectionPolicyResponse, params *ListConnectionPolicyParams, recordChannel chan VoiceV1ConnectionPolicy, errorChannel chan error) { 193 curRecord := 1 194 195 for response != nil { 196 responseRecords := response.ConnectionPolicies 197 for item := range responseRecords { 198 recordChannel <- responseRecords[item] 199 curRecord += 1 200 if params.Limit != nil && *params.Limit < curRecord { 201 close(recordChannel) 202 close(errorChannel) 203 return 204 } 205 } 206 207 record, err := client.GetNext(c.baseURL, response, c.getNextListConnectionPolicyResponse) 208 if err != nil { 209 errorChannel <- err 210 break 211 } else if record == nil { 212 break 213 } 214 215 response = record.(*ListConnectionPolicyResponse) 216 } 217 218 close(recordChannel) 219 close(errorChannel) 220 } 221 222 func (c *ApiService) getNextListConnectionPolicyResponse(nextPageUrl string) (interface{}, error) { 223 if nextPageUrl == "" { 224 return nil, nil 225 } 226 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 227 if err != nil { 228 return nil, err 229 } 230 231 defer resp.Body.Close() 232 233 ps := &ListConnectionPolicyResponse{} 234 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 235 return nil, err 236 } 237 return ps, nil 238 } 239 240 // Optional parameters for the method 'UpdateConnectionPolicy' 241 type UpdateConnectionPolicyParams struct { 242 // A descriptive string that you create to describe the resource. It is not unique and can be up to 255 characters long. 243 FriendlyName *string `json:"FriendlyName,omitempty"` 244 } 245 246 func (params *UpdateConnectionPolicyParams) SetFriendlyName(FriendlyName string) *UpdateConnectionPolicyParams { 247 params.FriendlyName = &FriendlyName 248 return params 249 } 250 251 // 252 func (c *ApiService) UpdateConnectionPolicy(Sid string, params *UpdateConnectionPolicyParams) (*VoiceV1ConnectionPolicy, error) { 253 path := "/v1/ConnectionPolicies/{Sid}" 254 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 255 256 data := url.Values{} 257 headers := make(map[string]interface{}) 258 259 if params != nil && params.FriendlyName != nil { 260 data.Set("FriendlyName", *params.FriendlyName) 261 } 262 263 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 264 if err != nil { 265 return nil, err 266 } 267 268 defer resp.Body.Close() 269 270 ps := &VoiceV1ConnectionPolicy{} 271 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 272 return nil, err 273 } 274 275 return ps, err 276 }