github.com/twilio/twilio-go@v1.20.1/rest/wireless/v1/rate_plans.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Wireless 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 'CreateRatePlan' 27 type CreateRatePlanParams struct { 28 // An application-defined string that uniquely identifies the resource. It can be used in place of the resource's `sid` in the URL to address the resource. 29 UniqueName *string `json:"UniqueName,omitempty"` 30 // A descriptive string that you create to describe the resource. It does not have to be unique. 31 FriendlyName *string `json:"FriendlyName,omitempty"` 32 // Whether SIMs can use GPRS/3G/4G/LTE data connectivity. 33 DataEnabled *bool `json:"DataEnabled,omitempty"` 34 // The total data usage (download and upload combined) in Megabytes that the Network allows during one month on the home network (T-Mobile USA). The metering period begins the day of activation and ends on the same day in the following month. Can be up to 2TB and the default value is `1000`. 35 DataLimit *int `json:"DataLimit,omitempty"` 36 // The model used to meter data usage. Can be: `payg` and `quota-1`, `quota-10`, and `quota-50`. Learn more about the available [data metering models](https://www.twilio.com/docs/iot/wireless/api/rateplan-resource#payg-vs-quota-data-plans). 37 DataMetering *string `json:"DataMetering,omitempty"` 38 // Whether SIMs can make, send, and receive SMS using [Commands](https://www.twilio.com/docs/iot/wireless/api/command-resource). 39 MessagingEnabled *bool `json:"MessagingEnabled,omitempty"` 40 // Deprecated. 41 VoiceEnabled *bool `json:"VoiceEnabled,omitempty"` 42 // Whether SIMs can roam on networks other than the home network (T-Mobile USA) in the United States. See [national roaming](https://www.twilio.com/docs/iot/wireless/api/rateplan-resource#national-roaming). 43 NationalRoamingEnabled *bool `json:"NationalRoamingEnabled,omitempty"` 44 // The list of services that SIMs capable of using GPRS/3G/4G/LTE data connectivity can use outside of the United States. Can contain: `data` and `messaging`. 45 InternationalRoaming *[]string `json:"InternationalRoaming,omitempty"` 46 // The total data usage (download and upload combined) in Megabytes that the Network allows during one month on non-home networks in the United States. The metering period begins the day of activation and ends on the same day in the following month. Can be up to 2TB. See [national roaming](https://www.twilio.com/docs/iot/wireless/api/rateplan-resource#national-roaming) for more info. 47 NationalRoamingDataLimit *int `json:"NationalRoamingDataLimit,omitempty"` 48 // The total data usage (download and upload combined) in Megabytes that the Network allows during one month when roaming outside the United States. Can be up to 2TB. 49 InternationalRoamingDataLimit *int `json:"InternationalRoamingDataLimit,omitempty"` 50 } 51 52 func (params *CreateRatePlanParams) SetUniqueName(UniqueName string) *CreateRatePlanParams { 53 params.UniqueName = &UniqueName 54 return params 55 } 56 func (params *CreateRatePlanParams) SetFriendlyName(FriendlyName string) *CreateRatePlanParams { 57 params.FriendlyName = &FriendlyName 58 return params 59 } 60 func (params *CreateRatePlanParams) SetDataEnabled(DataEnabled bool) *CreateRatePlanParams { 61 params.DataEnabled = &DataEnabled 62 return params 63 } 64 func (params *CreateRatePlanParams) SetDataLimit(DataLimit int) *CreateRatePlanParams { 65 params.DataLimit = &DataLimit 66 return params 67 } 68 func (params *CreateRatePlanParams) SetDataMetering(DataMetering string) *CreateRatePlanParams { 69 params.DataMetering = &DataMetering 70 return params 71 } 72 func (params *CreateRatePlanParams) SetMessagingEnabled(MessagingEnabled bool) *CreateRatePlanParams { 73 params.MessagingEnabled = &MessagingEnabled 74 return params 75 } 76 func (params *CreateRatePlanParams) SetVoiceEnabled(VoiceEnabled bool) *CreateRatePlanParams { 77 params.VoiceEnabled = &VoiceEnabled 78 return params 79 } 80 func (params *CreateRatePlanParams) SetNationalRoamingEnabled(NationalRoamingEnabled bool) *CreateRatePlanParams { 81 params.NationalRoamingEnabled = &NationalRoamingEnabled 82 return params 83 } 84 func (params *CreateRatePlanParams) SetInternationalRoaming(InternationalRoaming []string) *CreateRatePlanParams { 85 params.InternationalRoaming = &InternationalRoaming 86 return params 87 } 88 func (params *CreateRatePlanParams) SetNationalRoamingDataLimit(NationalRoamingDataLimit int) *CreateRatePlanParams { 89 params.NationalRoamingDataLimit = &NationalRoamingDataLimit 90 return params 91 } 92 func (params *CreateRatePlanParams) SetInternationalRoamingDataLimit(InternationalRoamingDataLimit int) *CreateRatePlanParams { 93 params.InternationalRoamingDataLimit = &InternationalRoamingDataLimit 94 return params 95 } 96 97 // 98 func (c *ApiService) CreateRatePlan(params *CreateRatePlanParams) (*WirelessV1RatePlan, error) { 99 path := "/v1/RatePlans" 100 101 data := url.Values{} 102 headers := make(map[string]interface{}) 103 104 if params != nil && params.UniqueName != nil { 105 data.Set("UniqueName", *params.UniqueName) 106 } 107 if params != nil && params.FriendlyName != nil { 108 data.Set("FriendlyName", *params.FriendlyName) 109 } 110 if params != nil && params.DataEnabled != nil { 111 data.Set("DataEnabled", fmt.Sprint(*params.DataEnabled)) 112 } 113 if params != nil && params.DataLimit != nil { 114 data.Set("DataLimit", fmt.Sprint(*params.DataLimit)) 115 } 116 if params != nil && params.DataMetering != nil { 117 data.Set("DataMetering", *params.DataMetering) 118 } 119 if params != nil && params.MessagingEnabled != nil { 120 data.Set("MessagingEnabled", fmt.Sprint(*params.MessagingEnabled)) 121 } 122 if params != nil && params.VoiceEnabled != nil { 123 data.Set("VoiceEnabled", fmt.Sprint(*params.VoiceEnabled)) 124 } 125 if params != nil && params.NationalRoamingEnabled != nil { 126 data.Set("NationalRoamingEnabled", fmt.Sprint(*params.NationalRoamingEnabled)) 127 } 128 if params != nil && params.InternationalRoaming != nil { 129 for _, item := range *params.InternationalRoaming { 130 data.Add("InternationalRoaming", item) 131 } 132 } 133 if params != nil && params.NationalRoamingDataLimit != nil { 134 data.Set("NationalRoamingDataLimit", fmt.Sprint(*params.NationalRoamingDataLimit)) 135 } 136 if params != nil && params.InternationalRoamingDataLimit != nil { 137 data.Set("InternationalRoamingDataLimit", fmt.Sprint(*params.InternationalRoamingDataLimit)) 138 } 139 140 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 141 if err != nil { 142 return nil, err 143 } 144 145 defer resp.Body.Close() 146 147 ps := &WirelessV1RatePlan{} 148 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 149 return nil, err 150 } 151 152 return ps, err 153 } 154 155 // 156 func (c *ApiService) DeleteRatePlan(Sid string) error { 157 path := "/v1/RatePlans/{Sid}" 158 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 159 160 data := url.Values{} 161 headers := make(map[string]interface{}) 162 163 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 164 if err != nil { 165 return err 166 } 167 168 defer resp.Body.Close() 169 170 return nil 171 } 172 173 // 174 func (c *ApiService) FetchRatePlan(Sid string) (*WirelessV1RatePlan, error) { 175 path := "/v1/RatePlans/{Sid}" 176 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 177 178 data := url.Values{} 179 headers := make(map[string]interface{}) 180 181 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 182 if err != nil { 183 return nil, err 184 } 185 186 defer resp.Body.Close() 187 188 ps := &WirelessV1RatePlan{} 189 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 190 return nil, err 191 } 192 193 return ps, err 194 } 195 196 // Optional parameters for the method 'ListRatePlan' 197 type ListRatePlanParams struct { 198 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 199 PageSize *int `json:"PageSize,omitempty"` 200 // Max number of records to return. 201 Limit *int `json:"limit,omitempty"` 202 } 203 204 func (params *ListRatePlanParams) SetPageSize(PageSize int) *ListRatePlanParams { 205 params.PageSize = &PageSize 206 return params 207 } 208 func (params *ListRatePlanParams) SetLimit(Limit int) *ListRatePlanParams { 209 params.Limit = &Limit 210 return params 211 } 212 213 // Retrieve a single page of RatePlan records from the API. Request is executed immediately. 214 func (c *ApiService) PageRatePlan(params *ListRatePlanParams, pageToken, pageNumber string) (*ListRatePlanResponse, error) { 215 path := "/v1/RatePlans" 216 217 data := url.Values{} 218 headers := make(map[string]interface{}) 219 220 if params != nil && params.PageSize != nil { 221 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 222 } 223 224 if pageToken != "" { 225 data.Set("PageToken", pageToken) 226 } 227 if pageNumber != "" { 228 data.Set("Page", pageNumber) 229 } 230 231 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 232 if err != nil { 233 return nil, err 234 } 235 236 defer resp.Body.Close() 237 238 ps := &ListRatePlanResponse{} 239 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 240 return nil, err 241 } 242 243 return ps, err 244 } 245 246 // Lists RatePlan records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 247 func (c *ApiService) ListRatePlan(params *ListRatePlanParams) ([]WirelessV1RatePlan, error) { 248 response, errors := c.StreamRatePlan(params) 249 250 records := make([]WirelessV1RatePlan, 0) 251 for record := range response { 252 records = append(records, record) 253 } 254 255 if err := <-errors; err != nil { 256 return nil, err 257 } 258 259 return records, nil 260 } 261 262 // Streams RatePlan records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 263 func (c *ApiService) StreamRatePlan(params *ListRatePlanParams) (chan WirelessV1RatePlan, chan error) { 264 if params == nil { 265 params = &ListRatePlanParams{} 266 } 267 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 268 269 recordChannel := make(chan WirelessV1RatePlan, 1) 270 errorChannel := make(chan error, 1) 271 272 response, err := c.PageRatePlan(params, "", "") 273 if err != nil { 274 errorChannel <- err 275 close(recordChannel) 276 close(errorChannel) 277 } else { 278 go c.streamRatePlan(response, params, recordChannel, errorChannel) 279 } 280 281 return recordChannel, errorChannel 282 } 283 284 func (c *ApiService) streamRatePlan(response *ListRatePlanResponse, params *ListRatePlanParams, recordChannel chan WirelessV1RatePlan, errorChannel chan error) { 285 curRecord := 1 286 287 for response != nil { 288 responseRecords := response.RatePlans 289 for item := range responseRecords { 290 recordChannel <- responseRecords[item] 291 curRecord += 1 292 if params.Limit != nil && *params.Limit < curRecord { 293 close(recordChannel) 294 close(errorChannel) 295 return 296 } 297 } 298 299 record, err := client.GetNext(c.baseURL, response, c.getNextListRatePlanResponse) 300 if err != nil { 301 errorChannel <- err 302 break 303 } else if record == nil { 304 break 305 } 306 307 response = record.(*ListRatePlanResponse) 308 } 309 310 close(recordChannel) 311 close(errorChannel) 312 } 313 314 func (c *ApiService) getNextListRatePlanResponse(nextPageUrl string) (interface{}, error) { 315 if nextPageUrl == "" { 316 return nil, nil 317 } 318 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 319 if err != nil { 320 return nil, err 321 } 322 323 defer resp.Body.Close() 324 325 ps := &ListRatePlanResponse{} 326 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 327 return nil, err 328 } 329 return ps, nil 330 } 331 332 // Optional parameters for the method 'UpdateRatePlan' 333 type UpdateRatePlanParams struct { 334 // An application-defined string that uniquely identifies the resource. It can be used in place of the resource's `sid` in the URL to address the resource. 335 UniqueName *string `json:"UniqueName,omitempty"` 336 // A descriptive string that you create to describe the resource. It does not have to be unique. 337 FriendlyName *string `json:"FriendlyName,omitempty"` 338 } 339 340 func (params *UpdateRatePlanParams) SetUniqueName(UniqueName string) *UpdateRatePlanParams { 341 params.UniqueName = &UniqueName 342 return params 343 } 344 func (params *UpdateRatePlanParams) SetFriendlyName(FriendlyName string) *UpdateRatePlanParams { 345 params.FriendlyName = &FriendlyName 346 return params 347 } 348 349 // 350 func (c *ApiService) UpdateRatePlan(Sid string, params *UpdateRatePlanParams) (*WirelessV1RatePlan, error) { 351 path := "/v1/RatePlans/{Sid}" 352 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 353 354 data := url.Values{} 355 headers := make(map[string]interface{}) 356 357 if params != nil && params.UniqueName != nil { 358 data.Set("UniqueName", *params.UniqueName) 359 } 360 if params != nil && params.FriendlyName != nil { 361 data.Set("FriendlyName", *params.FriendlyName) 362 } 363 364 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 365 if err != nil { 366 return nil, err 367 } 368 369 defer resp.Body.Close() 370 371 ps := &WirelessV1RatePlan{} 372 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 373 return nil, err 374 } 375 376 return ps, err 377 }