github.com/twilio/twilio-go@v1.20.1/rest/wireless/v1/sims_usage_records.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 "time" 23 24 "github.com/twilio/twilio-go/client" 25 ) 26 27 // Optional parameters for the method 'ListUsageRecord' 28 type ListUsageRecordParams struct { 29 // Only include usage that occurred on or before this date, specified in [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html). The default is the current time. 30 End *time.Time `json:"End,omitempty"` 31 // Only include usage that has occurred on or after this date, specified in [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html). The default is one month before the `end` parameter value. 32 Start *time.Time `json:"Start,omitempty"` 33 // How to summarize the usage by time. Can be: `daily`, `hourly`, or `all`. The default is `all`. A value of `all` returns one Usage Record that describes the usage for the entire period. 34 Granularity *string `json:"Granularity,omitempty"` 35 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 36 PageSize *int `json:"PageSize,omitempty"` 37 // Max number of records to return. 38 Limit *int `json:"limit,omitempty"` 39 } 40 41 func (params *ListUsageRecordParams) SetEnd(End time.Time) *ListUsageRecordParams { 42 params.End = &End 43 return params 44 } 45 func (params *ListUsageRecordParams) SetStart(Start time.Time) *ListUsageRecordParams { 46 params.Start = &Start 47 return params 48 } 49 func (params *ListUsageRecordParams) SetGranularity(Granularity string) *ListUsageRecordParams { 50 params.Granularity = &Granularity 51 return params 52 } 53 func (params *ListUsageRecordParams) SetPageSize(PageSize int) *ListUsageRecordParams { 54 params.PageSize = &PageSize 55 return params 56 } 57 func (params *ListUsageRecordParams) SetLimit(Limit int) *ListUsageRecordParams { 58 params.Limit = &Limit 59 return params 60 } 61 62 // Retrieve a single page of UsageRecord records from the API. Request is executed immediately. 63 func (c *ApiService) PageUsageRecord(SimSid string, params *ListUsageRecordParams, pageToken, pageNumber string) (*ListUsageRecordResponse, error) { 64 path := "/v1/Sims/{SimSid}/UsageRecords" 65 66 path = strings.Replace(path, "{"+"SimSid"+"}", SimSid, -1) 67 68 data := url.Values{} 69 headers := make(map[string]interface{}) 70 71 if params != nil && params.End != nil { 72 data.Set("End", fmt.Sprint((*params.End).Format(time.RFC3339))) 73 } 74 if params != nil && params.Start != nil { 75 data.Set("Start", fmt.Sprint((*params.Start).Format(time.RFC3339))) 76 } 77 if params != nil && params.Granularity != nil { 78 data.Set("Granularity", *params.Granularity) 79 } 80 if params != nil && params.PageSize != nil { 81 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 82 } 83 84 if pageToken != "" { 85 data.Set("PageToken", pageToken) 86 } 87 if pageNumber != "" { 88 data.Set("Page", pageNumber) 89 } 90 91 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 92 if err != nil { 93 return nil, err 94 } 95 96 defer resp.Body.Close() 97 98 ps := &ListUsageRecordResponse{} 99 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 100 return nil, err 101 } 102 103 return ps, err 104 } 105 106 // Lists UsageRecord records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 107 func (c *ApiService) ListUsageRecord(SimSid string, params *ListUsageRecordParams) ([]WirelessV1UsageRecord, error) { 108 response, errors := c.StreamUsageRecord(SimSid, params) 109 110 records := make([]WirelessV1UsageRecord, 0) 111 for record := range response { 112 records = append(records, record) 113 } 114 115 if err := <-errors; err != nil { 116 return nil, err 117 } 118 119 return records, nil 120 } 121 122 // Streams UsageRecord records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 123 func (c *ApiService) StreamUsageRecord(SimSid string, params *ListUsageRecordParams) (chan WirelessV1UsageRecord, chan error) { 124 if params == nil { 125 params = &ListUsageRecordParams{} 126 } 127 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 128 129 recordChannel := make(chan WirelessV1UsageRecord, 1) 130 errorChannel := make(chan error, 1) 131 132 response, err := c.PageUsageRecord(SimSid, params, "", "") 133 if err != nil { 134 errorChannel <- err 135 close(recordChannel) 136 close(errorChannel) 137 } else { 138 go c.streamUsageRecord(response, params, recordChannel, errorChannel) 139 } 140 141 return recordChannel, errorChannel 142 } 143 144 func (c *ApiService) streamUsageRecord(response *ListUsageRecordResponse, params *ListUsageRecordParams, recordChannel chan WirelessV1UsageRecord, errorChannel chan error) { 145 curRecord := 1 146 147 for response != nil { 148 responseRecords := response.UsageRecords 149 for item := range responseRecords { 150 recordChannel <- responseRecords[item] 151 curRecord += 1 152 if params.Limit != nil && *params.Limit < curRecord { 153 close(recordChannel) 154 close(errorChannel) 155 return 156 } 157 } 158 159 record, err := client.GetNext(c.baseURL, response, c.getNextListUsageRecordResponse) 160 if err != nil { 161 errorChannel <- err 162 break 163 } else if record == nil { 164 break 165 } 166 167 response = record.(*ListUsageRecordResponse) 168 } 169 170 close(recordChannel) 171 close(errorChannel) 172 } 173 174 func (c *ApiService) getNextListUsageRecordResponse(nextPageUrl string) (interface{}, error) { 175 if nextPageUrl == "" { 176 return nil, nil 177 } 178 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 179 if err != nil { 180 return nil, err 181 } 182 183 defer resp.Body.Close() 184 185 ps := &ListUsageRecordResponse{} 186 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 187 return nil, err 188 } 189 return ps, nil 190 }