github.com/twilio/twilio-go@v1.20.1/rest/verify/v2/templates.go (about)

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