github.com/twilio/twilio-go@v1.20.1/rest/trunking/v1/trunks_phone_numbers.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Trunking
     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 'CreatePhoneNumber'
    27  type CreatePhoneNumberParams struct {
    28  	// The SID of the [Incoming Phone Number](https://www.twilio.com/docs/phone-numbers/api/incomingphonenumber-resource) that you want to associate with the trunk.
    29  	PhoneNumberSid *string `json:"PhoneNumberSid,omitempty"`
    30  }
    31  
    32  func (params *CreatePhoneNumberParams) SetPhoneNumberSid(PhoneNumberSid string) *CreatePhoneNumberParams {
    33  	params.PhoneNumberSid = &PhoneNumberSid
    34  	return params
    35  }
    36  
    37  //
    38  func (c *ApiService) CreatePhoneNumber(TrunkSid string, params *CreatePhoneNumberParams) (*TrunkingV1PhoneNumber, error) {
    39  	path := "/v1/Trunks/{TrunkSid}/PhoneNumbers"
    40  	path = strings.Replace(path, "{"+"TrunkSid"+"}", TrunkSid, -1)
    41  
    42  	data := url.Values{}
    43  	headers := make(map[string]interface{})
    44  
    45  	if params != nil && params.PhoneNumberSid != nil {
    46  		data.Set("PhoneNumberSid", *params.PhoneNumberSid)
    47  	}
    48  
    49  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	defer resp.Body.Close()
    55  
    56  	ps := &TrunkingV1PhoneNumber{}
    57  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	return ps, err
    62  }
    63  
    64  //
    65  func (c *ApiService) DeletePhoneNumber(TrunkSid string, Sid string) error {
    66  	path := "/v1/Trunks/{TrunkSid}/PhoneNumbers/{Sid}"
    67  	path = strings.Replace(path, "{"+"TrunkSid"+"}", TrunkSid, -1)
    68  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    69  
    70  	data := url.Values{}
    71  	headers := make(map[string]interface{})
    72  
    73  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	defer resp.Body.Close()
    79  
    80  	return nil
    81  }
    82  
    83  //
    84  func (c *ApiService) FetchPhoneNumber(TrunkSid string, Sid string) (*TrunkingV1PhoneNumber, error) {
    85  	path := "/v1/Trunks/{TrunkSid}/PhoneNumbers/{Sid}"
    86  	path = strings.Replace(path, "{"+"TrunkSid"+"}", TrunkSid, -1)
    87  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    88  
    89  	data := url.Values{}
    90  	headers := make(map[string]interface{})
    91  
    92  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  
    97  	defer resp.Body.Close()
    98  
    99  	ps := &TrunkingV1PhoneNumber{}
   100  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   101  		return nil, err
   102  	}
   103  
   104  	return ps, err
   105  }
   106  
   107  // Optional parameters for the method 'ListPhoneNumber'
   108  type ListPhoneNumberParams struct {
   109  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   110  	PageSize *int `json:"PageSize,omitempty"`
   111  	// Max number of records to return.
   112  	Limit *int `json:"limit,omitempty"`
   113  }
   114  
   115  func (params *ListPhoneNumberParams) SetPageSize(PageSize int) *ListPhoneNumberParams {
   116  	params.PageSize = &PageSize
   117  	return params
   118  }
   119  func (params *ListPhoneNumberParams) SetLimit(Limit int) *ListPhoneNumberParams {
   120  	params.Limit = &Limit
   121  	return params
   122  }
   123  
   124  // Retrieve a single page of PhoneNumber records from the API. Request is executed immediately.
   125  func (c *ApiService) PagePhoneNumber(TrunkSid string, params *ListPhoneNumberParams, pageToken, pageNumber string) (*ListPhoneNumberResponse, error) {
   126  	path := "/v1/Trunks/{TrunkSid}/PhoneNumbers"
   127  
   128  	path = strings.Replace(path, "{"+"TrunkSid"+"}", TrunkSid, -1)
   129  
   130  	data := url.Values{}
   131  	headers := make(map[string]interface{})
   132  
   133  	if params != nil && params.PageSize != nil {
   134  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   135  	}
   136  
   137  	if pageToken != "" {
   138  		data.Set("PageToken", pageToken)
   139  	}
   140  	if pageNumber != "" {
   141  		data.Set("Page", pageNumber)
   142  	}
   143  
   144  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  
   149  	defer resp.Body.Close()
   150  
   151  	ps := &ListPhoneNumberResponse{}
   152  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   153  		return nil, err
   154  	}
   155  
   156  	return ps, err
   157  }
   158  
   159  // Lists PhoneNumber records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   160  func (c *ApiService) ListPhoneNumber(TrunkSid string, params *ListPhoneNumberParams) ([]TrunkingV1PhoneNumber, error) {
   161  	response, errors := c.StreamPhoneNumber(TrunkSid, params)
   162  
   163  	records := make([]TrunkingV1PhoneNumber, 0)
   164  	for record := range response {
   165  		records = append(records, record)
   166  	}
   167  
   168  	if err := <-errors; err != nil {
   169  		return nil, err
   170  	}
   171  
   172  	return records, nil
   173  }
   174  
   175  // Streams PhoneNumber records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   176  func (c *ApiService) StreamPhoneNumber(TrunkSid string, params *ListPhoneNumberParams) (chan TrunkingV1PhoneNumber, chan error) {
   177  	if params == nil {
   178  		params = &ListPhoneNumberParams{}
   179  	}
   180  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   181  
   182  	recordChannel := make(chan TrunkingV1PhoneNumber, 1)
   183  	errorChannel := make(chan error, 1)
   184  
   185  	response, err := c.PagePhoneNumber(TrunkSid, params, "", "")
   186  	if err != nil {
   187  		errorChannel <- err
   188  		close(recordChannel)
   189  		close(errorChannel)
   190  	} else {
   191  		go c.streamPhoneNumber(response, params, recordChannel, errorChannel)
   192  	}
   193  
   194  	return recordChannel, errorChannel
   195  }
   196  
   197  func (c *ApiService) streamPhoneNumber(response *ListPhoneNumberResponse, params *ListPhoneNumberParams, recordChannel chan TrunkingV1PhoneNumber, errorChannel chan error) {
   198  	curRecord := 1
   199  
   200  	for response != nil {
   201  		responseRecords := response.PhoneNumbers
   202  		for item := range responseRecords {
   203  			recordChannel <- responseRecords[item]
   204  			curRecord += 1
   205  			if params.Limit != nil && *params.Limit < curRecord {
   206  				close(recordChannel)
   207  				close(errorChannel)
   208  				return
   209  			}
   210  		}
   211  
   212  		record, err := client.GetNext(c.baseURL, response, c.getNextListPhoneNumberResponse)
   213  		if err != nil {
   214  			errorChannel <- err
   215  			break
   216  		} else if record == nil {
   217  			break
   218  		}
   219  
   220  		response = record.(*ListPhoneNumberResponse)
   221  	}
   222  
   223  	close(recordChannel)
   224  	close(errorChannel)
   225  }
   226  
   227  func (c *ApiService) getNextListPhoneNumberResponse(nextPageUrl string) (interface{}, error) {
   228  	if nextPageUrl == "" {
   229  		return nil, nil
   230  	}
   231  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   232  	if err != nil {
   233  		return nil, err
   234  	}
   235  
   236  	defer resp.Body.Close()
   237  
   238  	ps := &ListPhoneNumberResponse{}
   239  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   240  		return nil, err
   241  	}
   242  	return ps, nil
   243  }