github.com/twilio/twilio-go@v1.20.1/rest/supersim/v1/settings_updates.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  
    22  	"github.com/twilio/twilio-go/client"
    23  )
    24  
    25  // Optional parameters for the method 'ListSettingsUpdate'
    26  type ListSettingsUpdateParams struct {
    27  	// Filter the Settings Updates by a Super SIM's SID or UniqueName.
    28  	Sim *string `json:"Sim,omitempty"`
    29  	// Filter the Settings Updates by status. Can be `scheduled`, `in-progress`, `successful`, or `failed`.
    30  	Status *string `json:"Status,omitempty"`
    31  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    32  	PageSize *int `json:"PageSize,omitempty"`
    33  	// Max number of records to return.
    34  	Limit *int `json:"limit,omitempty"`
    35  }
    36  
    37  func (params *ListSettingsUpdateParams) SetSim(Sim string) *ListSettingsUpdateParams {
    38  	params.Sim = &Sim
    39  	return params
    40  }
    41  func (params *ListSettingsUpdateParams) SetStatus(Status string) *ListSettingsUpdateParams {
    42  	params.Status = &Status
    43  	return params
    44  }
    45  func (params *ListSettingsUpdateParams) SetPageSize(PageSize int) *ListSettingsUpdateParams {
    46  	params.PageSize = &PageSize
    47  	return params
    48  }
    49  func (params *ListSettingsUpdateParams) SetLimit(Limit int) *ListSettingsUpdateParams {
    50  	params.Limit = &Limit
    51  	return params
    52  }
    53  
    54  // Retrieve a single page of SettingsUpdate records from the API. Request is executed immediately.
    55  func (c *ApiService) PageSettingsUpdate(params *ListSettingsUpdateParams, pageToken, pageNumber string) (*ListSettingsUpdateResponse, error) {
    56  	path := "/v1/SettingsUpdates"
    57  
    58  	data := url.Values{}
    59  	headers := make(map[string]interface{})
    60  
    61  	if params != nil && params.Sim != nil {
    62  		data.Set("Sim", *params.Sim)
    63  	}
    64  	if params != nil && params.Status != nil {
    65  		data.Set("Status", *params.Status)
    66  	}
    67  	if params != nil && params.PageSize != nil {
    68  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
    69  	}
    70  
    71  	if pageToken != "" {
    72  		data.Set("PageToken", pageToken)
    73  	}
    74  	if pageNumber != "" {
    75  		data.Set("Page", pageNumber)
    76  	}
    77  
    78  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	defer resp.Body.Close()
    84  
    85  	ps := &ListSettingsUpdateResponse{}
    86  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    87  		return nil, err
    88  	}
    89  
    90  	return ps, err
    91  }
    92  
    93  // Lists SettingsUpdate records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
    94  func (c *ApiService) ListSettingsUpdate(params *ListSettingsUpdateParams) ([]SupersimV1SettingsUpdate, error) {
    95  	response, errors := c.StreamSettingsUpdate(params)
    96  
    97  	records := make([]SupersimV1SettingsUpdate, 0)
    98  	for record := range response {
    99  		records = append(records, record)
   100  	}
   101  
   102  	if err := <-errors; err != nil {
   103  		return nil, err
   104  	}
   105  
   106  	return records, nil
   107  }
   108  
   109  // Streams SettingsUpdate records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   110  func (c *ApiService) StreamSettingsUpdate(params *ListSettingsUpdateParams) (chan SupersimV1SettingsUpdate, chan error) {
   111  	if params == nil {
   112  		params = &ListSettingsUpdateParams{}
   113  	}
   114  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   115  
   116  	recordChannel := make(chan SupersimV1SettingsUpdate, 1)
   117  	errorChannel := make(chan error, 1)
   118  
   119  	response, err := c.PageSettingsUpdate(params, "", "")
   120  	if err != nil {
   121  		errorChannel <- err
   122  		close(recordChannel)
   123  		close(errorChannel)
   124  	} else {
   125  		go c.streamSettingsUpdate(response, params, recordChannel, errorChannel)
   126  	}
   127  
   128  	return recordChannel, errorChannel
   129  }
   130  
   131  func (c *ApiService) streamSettingsUpdate(response *ListSettingsUpdateResponse, params *ListSettingsUpdateParams, recordChannel chan SupersimV1SettingsUpdate, errorChannel chan error) {
   132  	curRecord := 1
   133  
   134  	for response != nil {
   135  		responseRecords := response.SettingsUpdates
   136  		for item := range responseRecords {
   137  			recordChannel <- responseRecords[item]
   138  			curRecord += 1
   139  			if params.Limit != nil && *params.Limit < curRecord {
   140  				close(recordChannel)
   141  				close(errorChannel)
   142  				return
   143  			}
   144  		}
   145  
   146  		record, err := client.GetNext(c.baseURL, response, c.getNextListSettingsUpdateResponse)
   147  		if err != nil {
   148  			errorChannel <- err
   149  			break
   150  		} else if record == nil {
   151  			break
   152  		}
   153  
   154  		response = record.(*ListSettingsUpdateResponse)
   155  	}
   156  
   157  	close(recordChannel)
   158  	close(errorChannel)
   159  }
   160  
   161  func (c *ApiService) getNextListSettingsUpdateResponse(nextPageUrl string) (interface{}, error) {
   162  	if nextPageUrl == "" {
   163  		return nil, nil
   164  	}
   165  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   166  	if err != nil {
   167  		return nil, err
   168  	}
   169  
   170  	defer resp.Body.Close()
   171  
   172  	ps := &ListSettingsUpdateResponse{}
   173  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   174  		return nil, err
   175  	}
   176  	return ps, nil
   177  }