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