github.com/twilio/twilio-go@v1.20.1/rest/api/v2010/accounts_addresses_dependent_phone_numbers.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Api
     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 'ListDependentPhoneNumber'
    27  type ListDependentPhoneNumberParams struct {
    28  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the DependentPhoneNumber resources to read.
    29  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
    30  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    31  	PageSize *int `json:"PageSize,omitempty"`
    32  	// Max number of records to return.
    33  	Limit *int `json:"limit,omitempty"`
    34  }
    35  
    36  func (params *ListDependentPhoneNumberParams) SetPathAccountSid(PathAccountSid string) *ListDependentPhoneNumberParams {
    37  	params.PathAccountSid = &PathAccountSid
    38  	return params
    39  }
    40  func (params *ListDependentPhoneNumberParams) SetPageSize(PageSize int) *ListDependentPhoneNumberParams {
    41  	params.PageSize = &PageSize
    42  	return params
    43  }
    44  func (params *ListDependentPhoneNumberParams) SetLimit(Limit int) *ListDependentPhoneNumberParams {
    45  	params.Limit = &Limit
    46  	return params
    47  }
    48  
    49  // Retrieve a single page of DependentPhoneNumber records from the API. Request is executed immediately.
    50  func (c *ApiService) PageDependentPhoneNumber(AddressSid string, params *ListDependentPhoneNumberParams, pageToken, pageNumber string) (*ListDependentPhoneNumberResponse, error) {
    51  	path := "/2010-04-01/Accounts/{AccountSid}/Addresses/{AddressSid}/DependentPhoneNumbers.json"
    52  
    53  	if params != nil && params.PathAccountSid != nil {
    54  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
    55  	} else {
    56  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
    57  	}
    58  	path = strings.Replace(path, "{"+"AddressSid"+"}", AddressSid, -1)
    59  
    60  	data := url.Values{}
    61  	headers := make(map[string]interface{})
    62  
    63  	if params != nil && params.PageSize != nil {
    64  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
    65  	}
    66  
    67  	if pageToken != "" {
    68  		data.Set("PageToken", pageToken)
    69  	}
    70  	if pageNumber != "" {
    71  		data.Set("Page", pageNumber)
    72  	}
    73  
    74  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	defer resp.Body.Close()
    80  
    81  	ps := &ListDependentPhoneNumberResponse{}
    82  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	return ps, err
    87  }
    88  
    89  // Lists DependentPhoneNumber records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
    90  func (c *ApiService) ListDependentPhoneNumber(AddressSid string, params *ListDependentPhoneNumberParams) ([]ApiV2010DependentPhoneNumber, error) {
    91  	response, errors := c.StreamDependentPhoneNumber(AddressSid, params)
    92  
    93  	records := make([]ApiV2010DependentPhoneNumber, 0)
    94  	for record := range response {
    95  		records = append(records, record)
    96  	}
    97  
    98  	if err := <-errors; err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	return records, nil
   103  }
   104  
   105  // Streams DependentPhoneNumber records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   106  func (c *ApiService) StreamDependentPhoneNumber(AddressSid string, params *ListDependentPhoneNumberParams) (chan ApiV2010DependentPhoneNumber, chan error) {
   107  	if params == nil {
   108  		params = &ListDependentPhoneNumberParams{}
   109  	}
   110  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   111  
   112  	recordChannel := make(chan ApiV2010DependentPhoneNumber, 1)
   113  	errorChannel := make(chan error, 1)
   114  
   115  	response, err := c.PageDependentPhoneNumber(AddressSid, params, "", "")
   116  	if err != nil {
   117  		errorChannel <- err
   118  		close(recordChannel)
   119  		close(errorChannel)
   120  	} else {
   121  		go c.streamDependentPhoneNumber(response, params, recordChannel, errorChannel)
   122  	}
   123  
   124  	return recordChannel, errorChannel
   125  }
   126  
   127  func (c *ApiService) streamDependentPhoneNumber(response *ListDependentPhoneNumberResponse, params *ListDependentPhoneNumberParams, recordChannel chan ApiV2010DependentPhoneNumber, errorChannel chan error) {
   128  	curRecord := 1
   129  
   130  	for response != nil {
   131  		responseRecords := response.DependentPhoneNumbers
   132  		for item := range responseRecords {
   133  			recordChannel <- responseRecords[item]
   134  			curRecord += 1
   135  			if params.Limit != nil && *params.Limit < curRecord {
   136  				close(recordChannel)
   137  				close(errorChannel)
   138  				return
   139  			}
   140  		}
   141  
   142  		record, err := client.GetNext(c.baseURL, response, c.getNextListDependentPhoneNumberResponse)
   143  		if err != nil {
   144  			errorChannel <- err
   145  			break
   146  		} else if record == nil {
   147  			break
   148  		}
   149  
   150  		response = record.(*ListDependentPhoneNumberResponse)
   151  	}
   152  
   153  	close(recordChannel)
   154  	close(errorChannel)
   155  }
   156  
   157  func (c *ApiService) getNextListDependentPhoneNumberResponse(nextPageUrl string) (interface{}, error) {
   158  	if nextPageUrl == "" {
   159  		return nil, nil
   160  	}
   161  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   162  	if err != nil {
   163  		return nil, err
   164  	}
   165  
   166  	defer resp.Body.Close()
   167  
   168  	ps := &ListDependentPhoneNumberResponse{}
   169  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   170  		return nil, err
   171  	}
   172  	return ps, nil
   173  }