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