github.com/twilio/twilio-go@v1.20.1/rest/api/v2010/accounts_usage_triggers.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Api 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 'CreateUsageTrigger' 27 type CreateUsageTriggerParams struct { 28 // The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that will create the resource. 29 PathAccountSid *string `json:"PathAccountSid,omitempty"` 30 // The URL we should call using `callback_method` when the trigger fires. 31 CallbackUrl *string `json:"CallbackUrl,omitempty"` 32 // The usage value at which the trigger should fire. For convenience, you can use an offset value such as `+30` to specify a trigger_value that is 30 units more than the current usage value. Be sure to urlencode a `+` as `%2B`. 33 TriggerValue *string `json:"TriggerValue,omitempty"` 34 // 35 UsageCategory *string `json:"UsageCategory,omitempty"` 36 // The HTTP method we should use to call `callback_url`. Can be: `GET` or `POST` and the default is `POST`. 37 CallbackMethod *string `json:"CallbackMethod,omitempty"` 38 // A descriptive string that you create to describe the resource. It can be up to 64 characters long. 39 FriendlyName *string `json:"FriendlyName,omitempty"` 40 // 41 Recurring *string `json:"Recurring,omitempty"` 42 // 43 TriggerBy *string `json:"TriggerBy,omitempty"` 44 } 45 46 func (params *CreateUsageTriggerParams) SetPathAccountSid(PathAccountSid string) *CreateUsageTriggerParams { 47 params.PathAccountSid = &PathAccountSid 48 return params 49 } 50 func (params *CreateUsageTriggerParams) SetCallbackUrl(CallbackUrl string) *CreateUsageTriggerParams { 51 params.CallbackUrl = &CallbackUrl 52 return params 53 } 54 func (params *CreateUsageTriggerParams) SetTriggerValue(TriggerValue string) *CreateUsageTriggerParams { 55 params.TriggerValue = &TriggerValue 56 return params 57 } 58 func (params *CreateUsageTriggerParams) SetUsageCategory(UsageCategory string) *CreateUsageTriggerParams { 59 params.UsageCategory = &UsageCategory 60 return params 61 } 62 func (params *CreateUsageTriggerParams) SetCallbackMethod(CallbackMethod string) *CreateUsageTriggerParams { 63 params.CallbackMethod = &CallbackMethod 64 return params 65 } 66 func (params *CreateUsageTriggerParams) SetFriendlyName(FriendlyName string) *CreateUsageTriggerParams { 67 params.FriendlyName = &FriendlyName 68 return params 69 } 70 func (params *CreateUsageTriggerParams) SetRecurring(Recurring string) *CreateUsageTriggerParams { 71 params.Recurring = &Recurring 72 return params 73 } 74 func (params *CreateUsageTriggerParams) SetTriggerBy(TriggerBy string) *CreateUsageTriggerParams { 75 params.TriggerBy = &TriggerBy 76 return params 77 } 78 79 // Create a new UsageTrigger 80 func (c *ApiService) CreateUsageTrigger(params *CreateUsageTriggerParams) (*ApiV2010UsageTrigger, error) { 81 path := "/2010-04-01/Accounts/{AccountSid}/Usage/Triggers.json" 82 if params != nil && params.PathAccountSid != nil { 83 path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) 84 } else { 85 path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1) 86 } 87 88 data := url.Values{} 89 headers := make(map[string]interface{}) 90 91 if params != nil && params.CallbackUrl != nil { 92 data.Set("CallbackUrl", *params.CallbackUrl) 93 } 94 if params != nil && params.TriggerValue != nil { 95 data.Set("TriggerValue", *params.TriggerValue) 96 } 97 if params != nil && params.UsageCategory != nil { 98 data.Set("UsageCategory", *params.UsageCategory) 99 } 100 if params != nil && params.CallbackMethod != nil { 101 data.Set("CallbackMethod", *params.CallbackMethod) 102 } 103 if params != nil && params.FriendlyName != nil { 104 data.Set("FriendlyName", *params.FriendlyName) 105 } 106 if params != nil && params.Recurring != nil { 107 data.Set("Recurring", *params.Recurring) 108 } 109 if params != nil && params.TriggerBy != nil { 110 data.Set("TriggerBy", *params.TriggerBy) 111 } 112 113 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 114 if err != nil { 115 return nil, err 116 } 117 118 defer resp.Body.Close() 119 120 ps := &ApiV2010UsageTrigger{} 121 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 122 return nil, err 123 } 124 125 return ps, err 126 } 127 128 // Optional parameters for the method 'DeleteUsageTrigger' 129 type DeleteUsageTriggerParams struct { 130 // The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the UsageTrigger resources to delete. 131 PathAccountSid *string `json:"PathAccountSid,omitempty"` 132 } 133 134 func (params *DeleteUsageTriggerParams) SetPathAccountSid(PathAccountSid string) *DeleteUsageTriggerParams { 135 params.PathAccountSid = &PathAccountSid 136 return params 137 } 138 139 // 140 func (c *ApiService) DeleteUsageTrigger(Sid string, params *DeleteUsageTriggerParams) error { 141 path := "/2010-04-01/Accounts/{AccountSid}/Usage/Triggers/{Sid}.json" 142 if params != nil && params.PathAccountSid != nil { 143 path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) 144 } else { 145 path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1) 146 } 147 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 148 149 data := url.Values{} 150 headers := make(map[string]interface{}) 151 152 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 153 if err != nil { 154 return err 155 } 156 157 defer resp.Body.Close() 158 159 return nil 160 } 161 162 // Optional parameters for the method 'FetchUsageTrigger' 163 type FetchUsageTriggerParams struct { 164 // The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the UsageTrigger resource to fetch. 165 PathAccountSid *string `json:"PathAccountSid,omitempty"` 166 } 167 168 func (params *FetchUsageTriggerParams) SetPathAccountSid(PathAccountSid string) *FetchUsageTriggerParams { 169 params.PathAccountSid = &PathAccountSid 170 return params 171 } 172 173 // Fetch and instance of a usage-trigger 174 func (c *ApiService) FetchUsageTrigger(Sid string, params *FetchUsageTriggerParams) (*ApiV2010UsageTrigger, error) { 175 path := "/2010-04-01/Accounts/{AccountSid}/Usage/Triggers/{Sid}.json" 176 if params != nil && params.PathAccountSid != nil { 177 path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) 178 } else { 179 path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1) 180 } 181 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 182 183 data := url.Values{} 184 headers := make(map[string]interface{}) 185 186 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 187 if err != nil { 188 return nil, err 189 } 190 191 defer resp.Body.Close() 192 193 ps := &ApiV2010UsageTrigger{} 194 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 195 return nil, err 196 } 197 198 return ps, err 199 } 200 201 // Optional parameters for the method 'ListUsageTrigger' 202 type ListUsageTriggerParams struct { 203 // The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the UsageTrigger resources to read. 204 PathAccountSid *string `json:"PathAccountSid,omitempty"` 205 // The frequency of recurring UsageTriggers to read. Can be: `daily`, `monthly`, or `yearly` to read recurring UsageTriggers. An empty value or a value of `alltime` reads non-recurring UsageTriggers. 206 Recurring *string `json:"Recurring,omitempty"` 207 // The trigger field of the UsageTriggers to read. Can be: `count`, `usage`, or `price` as described in the [UsageRecords documentation](https://www.twilio.com/docs/usage/api/usage-record#usage-count-price). 208 TriggerBy *string `json:"TriggerBy,omitempty"` 209 // The usage category of the UsageTriggers to read. Must be a supported [usage categories](https://www.twilio.com/docs/usage/api/usage-record#usage-categories). 210 UsageCategory *string `json:"UsageCategory,omitempty"` 211 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 212 PageSize *int `json:"PageSize,omitempty"` 213 // Max number of records to return. 214 Limit *int `json:"limit,omitempty"` 215 } 216 217 func (params *ListUsageTriggerParams) SetPathAccountSid(PathAccountSid string) *ListUsageTriggerParams { 218 params.PathAccountSid = &PathAccountSid 219 return params 220 } 221 func (params *ListUsageTriggerParams) SetRecurring(Recurring string) *ListUsageTriggerParams { 222 params.Recurring = &Recurring 223 return params 224 } 225 func (params *ListUsageTriggerParams) SetTriggerBy(TriggerBy string) *ListUsageTriggerParams { 226 params.TriggerBy = &TriggerBy 227 return params 228 } 229 func (params *ListUsageTriggerParams) SetUsageCategory(UsageCategory string) *ListUsageTriggerParams { 230 params.UsageCategory = &UsageCategory 231 return params 232 } 233 func (params *ListUsageTriggerParams) SetPageSize(PageSize int) *ListUsageTriggerParams { 234 params.PageSize = &PageSize 235 return params 236 } 237 func (params *ListUsageTriggerParams) SetLimit(Limit int) *ListUsageTriggerParams { 238 params.Limit = &Limit 239 return params 240 } 241 242 // Retrieve a single page of UsageTrigger records from the API. Request is executed immediately. 243 func (c *ApiService) PageUsageTrigger(params *ListUsageTriggerParams, pageToken, pageNumber string) (*ListUsageTriggerResponse, error) { 244 path := "/2010-04-01/Accounts/{AccountSid}/Usage/Triggers.json" 245 246 if params != nil && params.PathAccountSid != nil { 247 path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) 248 } else { 249 path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1) 250 } 251 252 data := url.Values{} 253 headers := make(map[string]interface{}) 254 255 if params != nil && params.Recurring != nil { 256 data.Set("Recurring", *params.Recurring) 257 } 258 if params != nil && params.TriggerBy != nil { 259 data.Set("TriggerBy", *params.TriggerBy) 260 } 261 if params != nil && params.UsageCategory != nil { 262 data.Set("UsageCategory", *params.UsageCategory) 263 } 264 if params != nil && params.PageSize != nil { 265 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 266 } 267 268 if pageToken != "" { 269 data.Set("PageToken", pageToken) 270 } 271 if pageNumber != "" { 272 data.Set("Page", pageNumber) 273 } 274 275 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 276 if err != nil { 277 return nil, err 278 } 279 280 defer resp.Body.Close() 281 282 ps := &ListUsageTriggerResponse{} 283 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 284 return nil, err 285 } 286 287 return ps, err 288 } 289 290 // Lists UsageTrigger records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 291 func (c *ApiService) ListUsageTrigger(params *ListUsageTriggerParams) ([]ApiV2010UsageTrigger, error) { 292 response, errors := c.StreamUsageTrigger(params) 293 294 records := make([]ApiV2010UsageTrigger, 0) 295 for record := range response { 296 records = append(records, record) 297 } 298 299 if err := <-errors; err != nil { 300 return nil, err 301 } 302 303 return records, nil 304 } 305 306 // Streams UsageTrigger records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 307 func (c *ApiService) StreamUsageTrigger(params *ListUsageTriggerParams) (chan ApiV2010UsageTrigger, chan error) { 308 if params == nil { 309 params = &ListUsageTriggerParams{} 310 } 311 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 312 313 recordChannel := make(chan ApiV2010UsageTrigger, 1) 314 errorChannel := make(chan error, 1) 315 316 response, err := c.PageUsageTrigger(params, "", "") 317 if err != nil { 318 errorChannel <- err 319 close(recordChannel) 320 close(errorChannel) 321 } else { 322 go c.streamUsageTrigger(response, params, recordChannel, errorChannel) 323 } 324 325 return recordChannel, errorChannel 326 } 327 328 func (c *ApiService) streamUsageTrigger(response *ListUsageTriggerResponse, params *ListUsageTriggerParams, recordChannel chan ApiV2010UsageTrigger, errorChannel chan error) { 329 curRecord := 1 330 331 for response != nil { 332 responseRecords := response.UsageTriggers 333 for item := range responseRecords { 334 recordChannel <- responseRecords[item] 335 curRecord += 1 336 if params.Limit != nil && *params.Limit < curRecord { 337 close(recordChannel) 338 close(errorChannel) 339 return 340 } 341 } 342 343 record, err := client.GetNext(c.baseURL, response, c.getNextListUsageTriggerResponse) 344 if err != nil { 345 errorChannel <- err 346 break 347 } else if record == nil { 348 break 349 } 350 351 response = record.(*ListUsageTriggerResponse) 352 } 353 354 close(recordChannel) 355 close(errorChannel) 356 } 357 358 func (c *ApiService) getNextListUsageTriggerResponse(nextPageUrl string) (interface{}, error) { 359 if nextPageUrl == "" { 360 return nil, nil 361 } 362 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 363 if err != nil { 364 return nil, err 365 } 366 367 defer resp.Body.Close() 368 369 ps := &ListUsageTriggerResponse{} 370 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 371 return nil, err 372 } 373 return ps, nil 374 } 375 376 // Optional parameters for the method 'UpdateUsageTrigger' 377 type UpdateUsageTriggerParams struct { 378 // The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the UsageTrigger resources to update. 379 PathAccountSid *string `json:"PathAccountSid,omitempty"` 380 // The HTTP method we should use to call `callback_url`. Can be: `GET` or `POST` and the default is `POST`. 381 CallbackMethod *string `json:"CallbackMethod,omitempty"` 382 // The URL we should call using `callback_method` when the trigger fires. 383 CallbackUrl *string `json:"CallbackUrl,omitempty"` 384 // A descriptive string that you create to describe the resource. It can be up to 64 characters long. 385 FriendlyName *string `json:"FriendlyName,omitempty"` 386 } 387 388 func (params *UpdateUsageTriggerParams) SetPathAccountSid(PathAccountSid string) *UpdateUsageTriggerParams { 389 params.PathAccountSid = &PathAccountSid 390 return params 391 } 392 func (params *UpdateUsageTriggerParams) SetCallbackMethod(CallbackMethod string) *UpdateUsageTriggerParams { 393 params.CallbackMethod = &CallbackMethod 394 return params 395 } 396 func (params *UpdateUsageTriggerParams) SetCallbackUrl(CallbackUrl string) *UpdateUsageTriggerParams { 397 params.CallbackUrl = &CallbackUrl 398 return params 399 } 400 func (params *UpdateUsageTriggerParams) SetFriendlyName(FriendlyName string) *UpdateUsageTriggerParams { 401 params.FriendlyName = &FriendlyName 402 return params 403 } 404 405 // Update an instance of a usage trigger 406 func (c *ApiService) UpdateUsageTrigger(Sid string, params *UpdateUsageTriggerParams) (*ApiV2010UsageTrigger, error) { 407 path := "/2010-04-01/Accounts/{AccountSid}/Usage/Triggers/{Sid}.json" 408 if params != nil && params.PathAccountSid != nil { 409 path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) 410 } else { 411 path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1) 412 } 413 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 414 415 data := url.Values{} 416 headers := make(map[string]interface{}) 417 418 if params != nil && params.CallbackMethod != nil { 419 data.Set("CallbackMethod", *params.CallbackMethod) 420 } 421 if params != nil && params.CallbackUrl != nil { 422 data.Set("CallbackUrl", *params.CallbackUrl) 423 } 424 if params != nil && params.FriendlyName != nil { 425 data.Set("FriendlyName", *params.FriendlyName) 426 } 427 428 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 429 if err != nil { 430 return nil, err 431 } 432 433 defer resp.Body.Close() 434 435 ps := &ApiV2010UsageTrigger{} 436 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 437 return nil, err 438 } 439 440 return ps, err 441 }