github.com/twilio/twilio-go@v1.20.1/rest/voice/v1/dialing_permissions_countries.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 // Retrieve voice dialing country permissions identified by the given ISO country code 27 func (c *ApiService) FetchDialingPermissionsCountry(IsoCode string) (*VoiceV1DialingPermissionsCountryInstance, error) { 28 path := "/v1/DialingPermissions/Countries/{IsoCode}" 29 path = strings.Replace(path, "{"+"IsoCode"+"}", IsoCode, -1) 30 31 data := url.Values{} 32 headers := make(map[string]interface{}) 33 34 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 35 if err != nil { 36 return nil, err 37 } 38 39 defer resp.Body.Close() 40 41 ps := &VoiceV1DialingPermissionsCountryInstance{} 42 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 43 return nil, err 44 } 45 46 return ps, err 47 } 48 49 // Optional parameters for the method 'ListDialingPermissionsCountry' 50 type ListDialingPermissionsCountryParams struct { 51 // Filter to retrieve the country permissions by specifying the [ISO country code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) 52 IsoCode *string `json:"IsoCode,omitempty"` 53 // Filter to retrieve the country permissions by specifying the continent 54 Continent *string `json:"Continent,omitempty"` 55 // Filter the results by specified [country codes](https://www.itu.int/itudoc/itu-t/ob-lists/icc/e164_763.html) 56 CountryCode *string `json:"CountryCode,omitempty"` 57 // Filter to retrieve the country permissions with dialing to low-risk numbers enabled. Can be: `true` or `false`. 58 LowRiskNumbersEnabled *bool `json:"LowRiskNumbersEnabled,omitempty"` 59 // Filter to retrieve the country permissions with dialing to high-risk special service numbers enabled. Can be: `true` or `false` 60 HighRiskSpecialNumbersEnabled *bool `json:"HighRiskSpecialNumbersEnabled,omitempty"` 61 // Filter to retrieve the country permissions with dialing to high-risk [toll fraud](https://www.twilio.com/blog/how-to-protect-your-account-from-toll-fraud-with-voice-dialing-geo-permissions-html) numbers enabled. Can be: `true` or `false`. 62 HighRiskTollfraudNumbersEnabled *bool `json:"HighRiskTollfraudNumbersEnabled,omitempty"` 63 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 64 PageSize *int `json:"PageSize,omitempty"` 65 // Max number of records to return. 66 Limit *int `json:"limit,omitempty"` 67 } 68 69 func (params *ListDialingPermissionsCountryParams) SetIsoCode(IsoCode string) *ListDialingPermissionsCountryParams { 70 params.IsoCode = &IsoCode 71 return params 72 } 73 func (params *ListDialingPermissionsCountryParams) SetContinent(Continent string) *ListDialingPermissionsCountryParams { 74 params.Continent = &Continent 75 return params 76 } 77 func (params *ListDialingPermissionsCountryParams) SetCountryCode(CountryCode string) *ListDialingPermissionsCountryParams { 78 params.CountryCode = &CountryCode 79 return params 80 } 81 func (params *ListDialingPermissionsCountryParams) SetLowRiskNumbersEnabled(LowRiskNumbersEnabled bool) *ListDialingPermissionsCountryParams { 82 params.LowRiskNumbersEnabled = &LowRiskNumbersEnabled 83 return params 84 } 85 func (params *ListDialingPermissionsCountryParams) SetHighRiskSpecialNumbersEnabled(HighRiskSpecialNumbersEnabled bool) *ListDialingPermissionsCountryParams { 86 params.HighRiskSpecialNumbersEnabled = &HighRiskSpecialNumbersEnabled 87 return params 88 } 89 func (params *ListDialingPermissionsCountryParams) SetHighRiskTollfraudNumbersEnabled(HighRiskTollfraudNumbersEnabled bool) *ListDialingPermissionsCountryParams { 90 params.HighRiskTollfraudNumbersEnabled = &HighRiskTollfraudNumbersEnabled 91 return params 92 } 93 func (params *ListDialingPermissionsCountryParams) SetPageSize(PageSize int) *ListDialingPermissionsCountryParams { 94 params.PageSize = &PageSize 95 return params 96 } 97 func (params *ListDialingPermissionsCountryParams) SetLimit(Limit int) *ListDialingPermissionsCountryParams { 98 params.Limit = &Limit 99 return params 100 } 101 102 // Retrieve a single page of DialingPermissionsCountry records from the API. Request is executed immediately. 103 func (c *ApiService) PageDialingPermissionsCountry(params *ListDialingPermissionsCountryParams, pageToken, pageNumber string) (*ListDialingPermissionsCountryResponse, error) { 104 path := "/v1/DialingPermissions/Countries" 105 106 data := url.Values{} 107 headers := make(map[string]interface{}) 108 109 if params != nil && params.IsoCode != nil { 110 data.Set("IsoCode", *params.IsoCode) 111 } 112 if params != nil && params.Continent != nil { 113 data.Set("Continent", *params.Continent) 114 } 115 if params != nil && params.CountryCode != nil { 116 data.Set("CountryCode", *params.CountryCode) 117 } 118 if params != nil && params.LowRiskNumbersEnabled != nil { 119 data.Set("LowRiskNumbersEnabled", fmt.Sprint(*params.LowRiskNumbersEnabled)) 120 } 121 if params != nil && params.HighRiskSpecialNumbersEnabled != nil { 122 data.Set("HighRiskSpecialNumbersEnabled", fmt.Sprint(*params.HighRiskSpecialNumbersEnabled)) 123 } 124 if params != nil && params.HighRiskTollfraudNumbersEnabled != nil { 125 data.Set("HighRiskTollfraudNumbersEnabled", fmt.Sprint(*params.HighRiskTollfraudNumbersEnabled)) 126 } 127 if params != nil && params.PageSize != nil { 128 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 129 } 130 131 if pageToken != "" { 132 data.Set("PageToken", pageToken) 133 } 134 if pageNumber != "" { 135 data.Set("Page", pageNumber) 136 } 137 138 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 139 if err != nil { 140 return nil, err 141 } 142 143 defer resp.Body.Close() 144 145 ps := &ListDialingPermissionsCountryResponse{} 146 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 147 return nil, err 148 } 149 150 return ps, err 151 } 152 153 // Lists DialingPermissionsCountry records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 154 func (c *ApiService) ListDialingPermissionsCountry(params *ListDialingPermissionsCountryParams) ([]VoiceV1DialingPermissionsCountry, error) { 155 response, errors := c.StreamDialingPermissionsCountry(params) 156 157 records := make([]VoiceV1DialingPermissionsCountry, 0) 158 for record := range response { 159 records = append(records, record) 160 } 161 162 if err := <-errors; err != nil { 163 return nil, err 164 } 165 166 return records, nil 167 } 168 169 // Streams DialingPermissionsCountry records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 170 func (c *ApiService) StreamDialingPermissionsCountry(params *ListDialingPermissionsCountryParams) (chan VoiceV1DialingPermissionsCountry, chan error) { 171 if params == nil { 172 params = &ListDialingPermissionsCountryParams{} 173 } 174 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 175 176 recordChannel := make(chan VoiceV1DialingPermissionsCountry, 1) 177 errorChannel := make(chan error, 1) 178 179 response, err := c.PageDialingPermissionsCountry(params, "", "") 180 if err != nil { 181 errorChannel <- err 182 close(recordChannel) 183 close(errorChannel) 184 } else { 185 go c.streamDialingPermissionsCountry(response, params, recordChannel, errorChannel) 186 } 187 188 return recordChannel, errorChannel 189 } 190 191 func (c *ApiService) streamDialingPermissionsCountry(response *ListDialingPermissionsCountryResponse, params *ListDialingPermissionsCountryParams, recordChannel chan VoiceV1DialingPermissionsCountry, errorChannel chan error) { 192 curRecord := 1 193 194 for response != nil { 195 responseRecords := response.Content 196 for item := range responseRecords { 197 recordChannel <- responseRecords[item] 198 curRecord += 1 199 if params.Limit != nil && *params.Limit < curRecord { 200 close(recordChannel) 201 close(errorChannel) 202 return 203 } 204 } 205 206 record, err := client.GetNext(c.baseURL, response, c.getNextListDialingPermissionsCountryResponse) 207 if err != nil { 208 errorChannel <- err 209 break 210 } else if record == nil { 211 break 212 } 213 214 response = record.(*ListDialingPermissionsCountryResponse) 215 } 216 217 close(recordChannel) 218 close(errorChannel) 219 } 220 221 func (c *ApiService) getNextListDialingPermissionsCountryResponse(nextPageUrl string) (interface{}, error) { 222 if nextPageUrl == "" { 223 return nil, nil 224 } 225 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 226 if err != nil { 227 return nil, err 228 } 229 230 defer resp.Body.Close() 231 232 ps := &ListDialingPermissionsCountryResponse{} 233 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 234 return nil, err 235 } 236 return ps, nil 237 }