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