github.com/twilio/twilio-go@v1.20.1/rest/supersim/v1/usage_records.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Supersim
     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 'ListUsageRecord'
    27  type ListUsageRecordParams struct {
    28  	// SID or unique name of a Sim resource. Only show UsageRecords representing usage incurred by this Super SIM.
    29  	Sim *string `json:"Sim,omitempty"`
    30  	// SID or unique name of a Fleet resource. Only show UsageRecords representing usage for Super SIMs belonging to this Fleet resource at the time the usage occurred.
    31  	Fleet *string `json:"Fleet,omitempty"`
    32  	// SID of a Network resource. Only show UsageRecords representing usage on this network.
    33  	Network *string `json:"Network,omitempty"`
    34  	// Alpha-2 ISO Country Code. Only show UsageRecords representing usage in this country.
    35  	IsoCountry *string `json:"IsoCountry,omitempty"`
    36  	// Dimension over which to aggregate usage records. Can be: `sim`, `fleet`, `network`, `isoCountry`. Default is to not aggregate across any of these dimensions, UsageRecords will be aggregated into the time buckets described by the `Granularity` parameter.
    37  	Group *string `json:"Group,omitempty"`
    38  	// Time-based grouping that UsageRecords should be aggregated by. Can be: `hour`, `day`, or `all`. Default is `all`. `all` returns one UsageRecord that describes the usage for the entire period.
    39  	Granularity *string `json:"Granularity,omitempty"`
    40  	// Only include usage that occurred at or after this time, specified in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format. Default is one month before the `end_time`.
    41  	StartTime *time.Time `json:"StartTime,omitempty"`
    42  	// Only include usage that occurred before this time (exclusive), specified in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format. Default is the current time.
    43  	EndTime *time.Time `json:"EndTime,omitempty"`
    44  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    45  	PageSize *int `json:"PageSize,omitempty"`
    46  	// Max number of records to return.
    47  	Limit *int `json:"limit,omitempty"`
    48  }
    49  
    50  func (params *ListUsageRecordParams) SetSim(Sim string) *ListUsageRecordParams {
    51  	params.Sim = &Sim
    52  	return params
    53  }
    54  func (params *ListUsageRecordParams) SetFleet(Fleet string) *ListUsageRecordParams {
    55  	params.Fleet = &Fleet
    56  	return params
    57  }
    58  func (params *ListUsageRecordParams) SetNetwork(Network string) *ListUsageRecordParams {
    59  	params.Network = &Network
    60  	return params
    61  }
    62  func (params *ListUsageRecordParams) SetIsoCountry(IsoCountry string) *ListUsageRecordParams {
    63  	params.IsoCountry = &IsoCountry
    64  	return params
    65  }
    66  func (params *ListUsageRecordParams) SetGroup(Group string) *ListUsageRecordParams {
    67  	params.Group = &Group
    68  	return params
    69  }
    70  func (params *ListUsageRecordParams) SetGranularity(Granularity string) *ListUsageRecordParams {
    71  	params.Granularity = &Granularity
    72  	return params
    73  }
    74  func (params *ListUsageRecordParams) SetStartTime(StartTime time.Time) *ListUsageRecordParams {
    75  	params.StartTime = &StartTime
    76  	return params
    77  }
    78  func (params *ListUsageRecordParams) SetEndTime(EndTime time.Time) *ListUsageRecordParams {
    79  	params.EndTime = &EndTime
    80  	return params
    81  }
    82  func (params *ListUsageRecordParams) SetPageSize(PageSize int) *ListUsageRecordParams {
    83  	params.PageSize = &PageSize
    84  	return params
    85  }
    86  func (params *ListUsageRecordParams) SetLimit(Limit int) *ListUsageRecordParams {
    87  	params.Limit = &Limit
    88  	return params
    89  }
    90  
    91  // Retrieve a single page of UsageRecord records from the API. Request is executed immediately.
    92  func (c *ApiService) PageUsageRecord(params *ListUsageRecordParams, pageToken, pageNumber string) (*ListUsageRecordResponse, error) {
    93  	path := "/v1/UsageRecords"
    94  
    95  	data := url.Values{}
    96  	headers := make(map[string]interface{})
    97  
    98  	if params != nil && params.Sim != nil {
    99  		data.Set("Sim", *params.Sim)
   100  	}
   101  	if params != nil && params.Fleet != nil {
   102  		data.Set("Fleet", *params.Fleet)
   103  	}
   104  	if params != nil && params.Network != nil {
   105  		data.Set("Network", *params.Network)
   106  	}
   107  	if params != nil && params.IsoCountry != nil {
   108  		data.Set("IsoCountry", *params.IsoCountry)
   109  	}
   110  	if params != nil && params.Group != nil {
   111  		data.Set("Group", *params.Group)
   112  	}
   113  	if params != nil && params.Granularity != nil {
   114  		data.Set("Granularity", *params.Granularity)
   115  	}
   116  	if params != nil && params.StartTime != nil {
   117  		data.Set("StartTime", fmt.Sprint((*params.StartTime).Format(time.RFC3339)))
   118  	}
   119  	if params != nil && params.EndTime != nil {
   120  		data.Set("EndTime", fmt.Sprint((*params.EndTime).Format(time.RFC3339)))
   121  	}
   122  	if params != nil && params.PageSize != nil {
   123  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   124  	}
   125  
   126  	if pageToken != "" {
   127  		data.Set("PageToken", pageToken)
   128  	}
   129  	if pageNumber != "" {
   130  		data.Set("Page", pageNumber)
   131  	}
   132  
   133  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  
   138  	defer resp.Body.Close()
   139  
   140  	ps := &ListUsageRecordResponse{}
   141  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   142  		return nil, err
   143  	}
   144  
   145  	return ps, err
   146  }
   147  
   148  // Lists UsageRecord records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   149  func (c *ApiService) ListUsageRecord(params *ListUsageRecordParams) ([]SupersimV1UsageRecord, error) {
   150  	response, errors := c.StreamUsageRecord(params)
   151  
   152  	records := make([]SupersimV1UsageRecord, 0)
   153  	for record := range response {
   154  		records = append(records, record)
   155  	}
   156  
   157  	if err := <-errors; err != nil {
   158  		return nil, err
   159  	}
   160  
   161  	return records, nil
   162  }
   163  
   164  // Streams UsageRecord records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   165  func (c *ApiService) StreamUsageRecord(params *ListUsageRecordParams) (chan SupersimV1UsageRecord, chan error) {
   166  	if params == nil {
   167  		params = &ListUsageRecordParams{}
   168  	}
   169  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   170  
   171  	recordChannel := make(chan SupersimV1UsageRecord, 1)
   172  	errorChannel := make(chan error, 1)
   173  
   174  	response, err := c.PageUsageRecord(params, "", "")
   175  	if err != nil {
   176  		errorChannel <- err
   177  		close(recordChannel)
   178  		close(errorChannel)
   179  	} else {
   180  		go c.streamUsageRecord(response, params, recordChannel, errorChannel)
   181  	}
   182  
   183  	return recordChannel, errorChannel
   184  }
   185  
   186  func (c *ApiService) streamUsageRecord(response *ListUsageRecordResponse, params *ListUsageRecordParams, recordChannel chan SupersimV1UsageRecord, errorChannel chan error) {
   187  	curRecord := 1
   188  
   189  	for response != nil {
   190  		responseRecords := response.UsageRecords
   191  		for item := range responseRecords {
   192  			recordChannel <- responseRecords[item]
   193  			curRecord += 1
   194  			if params.Limit != nil && *params.Limit < curRecord {
   195  				close(recordChannel)
   196  				close(errorChannel)
   197  				return
   198  			}
   199  		}
   200  
   201  		record, err := client.GetNext(c.baseURL, response, c.getNextListUsageRecordResponse)
   202  		if err != nil {
   203  			errorChannel <- err
   204  			break
   205  		} else if record == nil {
   206  			break
   207  		}
   208  
   209  		response = record.(*ListUsageRecordResponse)
   210  	}
   211  
   212  	close(recordChannel)
   213  	close(errorChannel)
   214  }
   215  
   216  func (c *ApiService) getNextListUsageRecordResponse(nextPageUrl string) (interface{}, error) {
   217  	if nextPageUrl == "" {
   218  		return nil, nil
   219  	}
   220  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   221  	if err != nil {
   222  		return nil, err
   223  	}
   224  
   225  	defer resp.Body.Close()
   226  
   227  	ps := &ListUsageRecordResponse{}
   228  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   229  		return nil, err
   230  	}
   231  	return ps, nil
   232  }