github.com/twilio/twilio-go@v1.20.1/rest/api/v2010/accounts_usage_records_daily.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 'ListUsageRecordDaily' 27 type ListUsageRecordDailyParams struct { 28 // The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the UsageRecord resources to read. 29 PathAccountSid *string `json:"PathAccountSid,omitempty"` 30 // The [usage category](https://www.twilio.com/docs/usage/api/usage-record#usage-categories) of the UsageRecord resources to read. Only UsageRecord resources in the specified category are retrieved. 31 Category *string `json:"Category,omitempty"` 32 // Only include usage that has occurred on or after this date. Specify the date in GMT and format as `YYYY-MM-DD`. You can also specify offsets from the current date, such as: `-30days`, which will set the start date to be 30 days before the current date. 33 StartDate *string `json:"StartDate,omitempty"` 34 // Only include usage that occurred on or before this date. Specify the date in GMT and format as `YYYY-MM-DD`. You can also specify offsets from the current date, such as: `+30days`, which will set the end date to 30 days from the current date. 35 EndDate *string `json:"EndDate,omitempty"` 36 // Whether to include usage from the master account and all its subaccounts. Can be: `true` (the default) to include usage from the master account and all subaccounts or `false` to retrieve usage from only the specified account. 37 IncludeSubaccounts *bool `json:"IncludeSubaccounts,omitempty"` 38 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 39 PageSize *int `json:"PageSize,omitempty"` 40 // Max number of records to return. 41 Limit *int `json:"limit,omitempty"` 42 } 43 44 func (params *ListUsageRecordDailyParams) SetPathAccountSid(PathAccountSid string) *ListUsageRecordDailyParams { 45 params.PathAccountSid = &PathAccountSid 46 return params 47 } 48 func (params *ListUsageRecordDailyParams) SetCategory(Category string) *ListUsageRecordDailyParams { 49 params.Category = &Category 50 return params 51 } 52 func (params *ListUsageRecordDailyParams) SetStartDate(StartDate string) *ListUsageRecordDailyParams { 53 params.StartDate = &StartDate 54 return params 55 } 56 func (params *ListUsageRecordDailyParams) SetEndDate(EndDate string) *ListUsageRecordDailyParams { 57 params.EndDate = &EndDate 58 return params 59 } 60 func (params *ListUsageRecordDailyParams) SetIncludeSubaccounts(IncludeSubaccounts bool) *ListUsageRecordDailyParams { 61 params.IncludeSubaccounts = &IncludeSubaccounts 62 return params 63 } 64 func (params *ListUsageRecordDailyParams) SetPageSize(PageSize int) *ListUsageRecordDailyParams { 65 params.PageSize = &PageSize 66 return params 67 } 68 func (params *ListUsageRecordDailyParams) SetLimit(Limit int) *ListUsageRecordDailyParams { 69 params.Limit = &Limit 70 return params 71 } 72 73 // Retrieve a single page of UsageRecordDaily records from the API. Request is executed immediately. 74 func (c *ApiService) PageUsageRecordDaily(params *ListUsageRecordDailyParams, pageToken, pageNumber string) (*ListUsageRecordDailyResponse, error) { 75 path := "/2010-04-01/Accounts/{AccountSid}/Usage/Records/Daily.json" 76 77 if params != nil && params.PathAccountSid != nil { 78 path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) 79 } else { 80 path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1) 81 } 82 83 data := url.Values{} 84 headers := make(map[string]interface{}) 85 86 if params != nil && params.Category != nil { 87 data.Set("Category", *params.Category) 88 } 89 if params != nil && params.StartDate != nil { 90 data.Set("StartDate", fmt.Sprint(*params.StartDate)) 91 } 92 if params != nil && params.EndDate != nil { 93 data.Set("EndDate", fmt.Sprint(*params.EndDate)) 94 } 95 if params != nil && params.IncludeSubaccounts != nil { 96 data.Set("IncludeSubaccounts", fmt.Sprint(*params.IncludeSubaccounts)) 97 } 98 if params != nil && params.PageSize != nil { 99 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 100 } 101 102 if pageToken != "" { 103 data.Set("PageToken", pageToken) 104 } 105 if pageNumber != "" { 106 data.Set("Page", pageNumber) 107 } 108 109 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 110 if err != nil { 111 return nil, err 112 } 113 114 defer resp.Body.Close() 115 116 ps := &ListUsageRecordDailyResponse{} 117 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 118 return nil, err 119 } 120 121 return ps, err 122 } 123 124 // Lists UsageRecordDaily records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 125 func (c *ApiService) ListUsageRecordDaily(params *ListUsageRecordDailyParams) ([]ApiV2010UsageRecordDaily, error) { 126 response, errors := c.StreamUsageRecordDaily(params) 127 128 records := make([]ApiV2010UsageRecordDaily, 0) 129 for record := range response { 130 records = append(records, record) 131 } 132 133 if err := <-errors; err != nil { 134 return nil, err 135 } 136 137 return records, nil 138 } 139 140 // Streams UsageRecordDaily records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 141 func (c *ApiService) StreamUsageRecordDaily(params *ListUsageRecordDailyParams) (chan ApiV2010UsageRecordDaily, chan error) { 142 if params == nil { 143 params = &ListUsageRecordDailyParams{} 144 } 145 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 146 147 recordChannel := make(chan ApiV2010UsageRecordDaily, 1) 148 errorChannel := make(chan error, 1) 149 150 response, err := c.PageUsageRecordDaily(params, "", "") 151 if err != nil { 152 errorChannel <- err 153 close(recordChannel) 154 close(errorChannel) 155 } else { 156 go c.streamUsageRecordDaily(response, params, recordChannel, errorChannel) 157 } 158 159 return recordChannel, errorChannel 160 } 161 162 func (c *ApiService) streamUsageRecordDaily(response *ListUsageRecordDailyResponse, params *ListUsageRecordDailyParams, recordChannel chan ApiV2010UsageRecordDaily, errorChannel chan error) { 163 curRecord := 1 164 165 for response != nil { 166 responseRecords := response.UsageRecords 167 for item := range responseRecords { 168 recordChannel <- responseRecords[item] 169 curRecord += 1 170 if params.Limit != nil && *params.Limit < curRecord { 171 close(recordChannel) 172 close(errorChannel) 173 return 174 } 175 } 176 177 record, err := client.GetNext(c.baseURL, response, c.getNextListUsageRecordDailyResponse) 178 if err != nil { 179 errorChannel <- err 180 break 181 } else if record == nil { 182 break 183 } 184 185 response = record.(*ListUsageRecordDailyResponse) 186 } 187 188 close(recordChannel) 189 close(errorChannel) 190 } 191 192 func (c *ApiService) getNextListUsageRecordDailyResponse(nextPageUrl string) (interface{}, error) { 193 if nextPageUrl == "" { 194 return nil, nil 195 } 196 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 197 if err != nil { 198 return nil, err 199 } 200 201 defer resp.Body.Close() 202 203 ps := &ListUsageRecordDailyResponse{} 204 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 205 return nil, err 206 } 207 return ps, nil 208 }