github.com/twilio/twilio-go@v1.20.1/rest/api/v2010/accounts_available_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 'FetchAvailablePhoneNumberCountry'
    27  type FetchAvailablePhoneNumberCountryParams struct {
    28  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) requesting the available phone number Country resource.
    29  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
    30  }
    31  
    32  func (params *FetchAvailablePhoneNumberCountryParams) SetPathAccountSid(PathAccountSid string) *FetchAvailablePhoneNumberCountryParams {
    33  	params.PathAccountSid = &PathAccountSid
    34  	return params
    35  }
    36  
    37  //
    38  func (c *ApiService) FetchAvailablePhoneNumberCountry(CountryCode string, params *FetchAvailablePhoneNumberCountryParams) (*ApiV2010AvailablePhoneNumberCountry, error) {
    39  	path := "/2010-04-01/Accounts/{AccountSid}/AvailablePhoneNumbers/{CountryCode}.json"
    40  	if params != nil && params.PathAccountSid != nil {
    41  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
    42  	} else {
    43  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
    44  	}
    45  	path = strings.Replace(path, "{"+"CountryCode"+"}", CountryCode, -1)
    46  
    47  	data := url.Values{}
    48  	headers := make(map[string]interface{})
    49  
    50  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	defer resp.Body.Close()
    56  
    57  	ps := &ApiV2010AvailablePhoneNumberCountry{}
    58  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	return ps, err
    63  }
    64  
    65  // Optional parameters for the method 'ListAvailablePhoneNumberCountry'
    66  type ListAvailablePhoneNumberCountryParams struct {
    67  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) requesting the available phone number Country resources.
    68  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
    69  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    70  	PageSize *int `json:"PageSize,omitempty"`
    71  	// Max number of records to return.
    72  	Limit *int `json:"limit,omitempty"`
    73  }
    74  
    75  func (params *ListAvailablePhoneNumberCountryParams) SetPathAccountSid(PathAccountSid string) *ListAvailablePhoneNumberCountryParams {
    76  	params.PathAccountSid = &PathAccountSid
    77  	return params
    78  }
    79  func (params *ListAvailablePhoneNumberCountryParams) SetPageSize(PageSize int) *ListAvailablePhoneNumberCountryParams {
    80  	params.PageSize = &PageSize
    81  	return params
    82  }
    83  func (params *ListAvailablePhoneNumberCountryParams) SetLimit(Limit int) *ListAvailablePhoneNumberCountryParams {
    84  	params.Limit = &Limit
    85  	return params
    86  }
    87  
    88  // Retrieve a single page of AvailablePhoneNumberCountry records from the API. Request is executed immediately.
    89  func (c *ApiService) PageAvailablePhoneNumberCountry(params *ListAvailablePhoneNumberCountryParams, pageToken, pageNumber string) (*ListAvailablePhoneNumberCountryResponse, error) {
    90  	path := "/2010-04-01/Accounts/{AccountSid}/AvailablePhoneNumbers.json"
    91  
    92  	if params != nil && params.PathAccountSid != nil {
    93  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
    94  	} else {
    95  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
    96  	}
    97  
    98  	data := url.Values{}
    99  	headers := make(map[string]interface{})
   100  
   101  	if params != nil && params.PageSize != nil {
   102  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   103  	}
   104  
   105  	if pageToken != "" {
   106  		data.Set("PageToken", pageToken)
   107  	}
   108  	if pageNumber != "" {
   109  		data.Set("Page", pageNumber)
   110  	}
   111  
   112  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	defer resp.Body.Close()
   118  
   119  	ps := &ListAvailablePhoneNumberCountryResponse{}
   120  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   121  		return nil, err
   122  	}
   123  
   124  	return ps, err
   125  }
   126  
   127  // Lists AvailablePhoneNumberCountry records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   128  func (c *ApiService) ListAvailablePhoneNumberCountry(params *ListAvailablePhoneNumberCountryParams) ([]ApiV2010AvailablePhoneNumberCountry, error) {
   129  	response, errors := c.StreamAvailablePhoneNumberCountry(params)
   130  
   131  	records := make([]ApiV2010AvailablePhoneNumberCountry, 0)
   132  	for record := range response {
   133  		records = append(records, record)
   134  	}
   135  
   136  	if err := <-errors; err != nil {
   137  		return nil, err
   138  	}
   139  
   140  	return records, nil
   141  }
   142  
   143  // Streams AvailablePhoneNumberCountry records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   144  func (c *ApiService) StreamAvailablePhoneNumberCountry(params *ListAvailablePhoneNumberCountryParams) (chan ApiV2010AvailablePhoneNumberCountry, chan error) {
   145  	if params == nil {
   146  		params = &ListAvailablePhoneNumberCountryParams{}
   147  	}
   148  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   149  
   150  	recordChannel := make(chan ApiV2010AvailablePhoneNumberCountry, 1)
   151  	errorChannel := make(chan error, 1)
   152  
   153  	response, err := c.PageAvailablePhoneNumberCountry(params, "", "")
   154  	if err != nil {
   155  		errorChannel <- err
   156  		close(recordChannel)
   157  		close(errorChannel)
   158  	} else {
   159  		go c.streamAvailablePhoneNumberCountry(response, params, recordChannel, errorChannel)
   160  	}
   161  
   162  	return recordChannel, errorChannel
   163  }
   164  
   165  func (c *ApiService) streamAvailablePhoneNumberCountry(response *ListAvailablePhoneNumberCountryResponse, params *ListAvailablePhoneNumberCountryParams, recordChannel chan ApiV2010AvailablePhoneNumberCountry, errorChannel chan error) {
   166  	curRecord := 1
   167  
   168  	for response != nil {
   169  		responseRecords := response.Countries
   170  		for item := range responseRecords {
   171  			recordChannel <- responseRecords[item]
   172  			curRecord += 1
   173  			if params.Limit != nil && *params.Limit < curRecord {
   174  				close(recordChannel)
   175  				close(errorChannel)
   176  				return
   177  			}
   178  		}
   179  
   180  		record, err := client.GetNext(c.baseURL, response, c.getNextListAvailablePhoneNumberCountryResponse)
   181  		if err != nil {
   182  			errorChannel <- err
   183  			break
   184  		} else if record == nil {
   185  			break
   186  		}
   187  
   188  		response = record.(*ListAvailablePhoneNumberCountryResponse)
   189  	}
   190  
   191  	close(recordChannel)
   192  	close(errorChannel)
   193  }
   194  
   195  func (c *ApiService) getNextListAvailablePhoneNumberCountryResponse(nextPageUrl string) (interface{}, error) {
   196  	if nextPageUrl == "" {
   197  		return nil, nil
   198  	}
   199  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   200  	if err != nil {
   201  		return nil, err
   202  	}
   203  
   204  	defer resp.Body.Close()
   205  
   206  	ps := &ListAvailablePhoneNumberCountryResponse{}
   207  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   208  		return nil, err
   209  	}
   210  	return ps, nil
   211  }