github.com/twilio/twilio-go@v1.20.1/rest/pricing/v2/trunking_countries.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Pricing 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 // Fetch a specific Country. 27 func (c *ApiService) FetchTrunkingCountry(IsoCountry string) (*PricingV2TrunkingCountryInstance, error) { 28 path := "/v2/Trunking/Countries/{IsoCountry}" 29 path = strings.Replace(path, "{"+"IsoCountry"+"}", IsoCountry, -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 := &PricingV2TrunkingCountryInstance{} 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 'ListTrunkingCountry' 50 type ListTrunkingCountryParams struct { 51 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 52 PageSize *int `json:"PageSize,omitempty"` 53 // Max number of records to return. 54 Limit *int `json:"limit,omitempty"` 55 } 56 57 func (params *ListTrunkingCountryParams) SetPageSize(PageSize int) *ListTrunkingCountryParams { 58 params.PageSize = &PageSize 59 return params 60 } 61 func (params *ListTrunkingCountryParams) SetLimit(Limit int) *ListTrunkingCountryParams { 62 params.Limit = &Limit 63 return params 64 } 65 66 // Retrieve a single page of TrunkingCountry records from the API. Request is executed immediately. 67 func (c *ApiService) PageTrunkingCountry(params *ListTrunkingCountryParams, pageToken, pageNumber string) (*ListTrunkingCountryResponse, error) { 68 path := "/v2/Trunking/Countries" 69 70 data := url.Values{} 71 headers := make(map[string]interface{}) 72 73 if params != nil && params.PageSize != nil { 74 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 75 } 76 77 if pageToken != "" { 78 data.Set("PageToken", pageToken) 79 } 80 if pageNumber != "" { 81 data.Set("Page", pageNumber) 82 } 83 84 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 85 if err != nil { 86 return nil, err 87 } 88 89 defer resp.Body.Close() 90 91 ps := &ListTrunkingCountryResponse{} 92 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 93 return nil, err 94 } 95 96 return ps, err 97 } 98 99 // Lists TrunkingCountry records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 100 func (c *ApiService) ListTrunkingCountry(params *ListTrunkingCountryParams) ([]PricingV2TrunkingCountry, error) { 101 response, errors := c.StreamTrunkingCountry(params) 102 103 records := make([]PricingV2TrunkingCountry, 0) 104 for record := range response { 105 records = append(records, record) 106 } 107 108 if err := <-errors; err != nil { 109 return nil, err 110 } 111 112 return records, nil 113 } 114 115 // Streams TrunkingCountry records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 116 func (c *ApiService) StreamTrunkingCountry(params *ListTrunkingCountryParams) (chan PricingV2TrunkingCountry, chan error) { 117 if params == nil { 118 params = &ListTrunkingCountryParams{} 119 } 120 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 121 122 recordChannel := make(chan PricingV2TrunkingCountry, 1) 123 errorChannel := make(chan error, 1) 124 125 response, err := c.PageTrunkingCountry(params, "", "") 126 if err != nil { 127 errorChannel <- err 128 close(recordChannel) 129 close(errorChannel) 130 } else { 131 go c.streamTrunkingCountry(response, params, recordChannel, errorChannel) 132 } 133 134 return recordChannel, errorChannel 135 } 136 137 func (c *ApiService) streamTrunkingCountry(response *ListTrunkingCountryResponse, params *ListTrunkingCountryParams, recordChannel chan PricingV2TrunkingCountry, errorChannel chan error) { 138 curRecord := 1 139 140 for response != nil { 141 responseRecords := response.Countries 142 for item := range responseRecords { 143 recordChannel <- responseRecords[item] 144 curRecord += 1 145 if params.Limit != nil && *params.Limit < curRecord { 146 close(recordChannel) 147 close(errorChannel) 148 return 149 } 150 } 151 152 record, err := client.GetNext(c.baseURL, response, c.getNextListTrunkingCountryResponse) 153 if err != nil { 154 errorChannel <- err 155 break 156 } else if record == nil { 157 break 158 } 159 160 response = record.(*ListTrunkingCountryResponse) 161 } 162 163 close(recordChannel) 164 close(errorChannel) 165 } 166 167 func (c *ApiService) getNextListTrunkingCountryResponse(nextPageUrl string) (interface{}, error) { 168 if nextPageUrl == "" { 169 return nil, nil 170 } 171 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 172 if err != nil { 173 return nil, err 174 } 175 176 defer resp.Body.Close() 177 178 ps := &ListTrunkingCountryResponse{} 179 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 180 return nil, err 181 } 182 return ps, nil 183 }