github.com/twilio/twilio-go@v1.20.1/rest/numbers/v2/regulatory_compliance_bundles_copies.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Numbers
     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 'CreateBundleCopy'
    27  type CreateBundleCopyParams struct {
    28  	// The string that you assigned to describe the copied bundle.
    29  	FriendlyName *string `json:"FriendlyName,omitempty"`
    30  }
    31  
    32  func (params *CreateBundleCopyParams) SetFriendlyName(FriendlyName string) *CreateBundleCopyParams {
    33  	params.FriendlyName = &FriendlyName
    34  	return params
    35  }
    36  
    37  // Creates a new copy of a Bundle. It will internally create copies of all the bundle items (identities and documents) of the original bundle
    38  func (c *ApiService) CreateBundleCopy(BundleSid string, params *CreateBundleCopyParams) (*NumbersV2BundleCopy, error) {
    39  	path := "/v2/RegulatoryCompliance/Bundles/{BundleSid}/Copies"
    40  	path = strings.Replace(path, "{"+"BundleSid"+"}", BundleSid, -1)
    41  
    42  	data := url.Values{}
    43  	headers := make(map[string]interface{})
    44  
    45  	if params != nil && params.FriendlyName != nil {
    46  		data.Set("FriendlyName", *params.FriendlyName)
    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 := &NumbersV2BundleCopy{}
    57  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	return ps, err
    62  }
    63  
    64  // Optional parameters for the method 'ListBundleCopy'
    65  type ListBundleCopyParams struct {
    66  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    67  	PageSize *int `json:"PageSize,omitempty"`
    68  	// Max number of records to return.
    69  	Limit *int `json:"limit,omitempty"`
    70  }
    71  
    72  func (params *ListBundleCopyParams) SetPageSize(PageSize int) *ListBundleCopyParams {
    73  	params.PageSize = &PageSize
    74  	return params
    75  }
    76  func (params *ListBundleCopyParams) SetLimit(Limit int) *ListBundleCopyParams {
    77  	params.Limit = &Limit
    78  	return params
    79  }
    80  
    81  // Retrieve a single page of BundleCopy records from the API. Request is executed immediately.
    82  func (c *ApiService) PageBundleCopy(BundleSid string, params *ListBundleCopyParams, pageToken, pageNumber string) (*ListBundleCopyResponse, error) {
    83  	path := "/v2/RegulatoryCompliance/Bundles/{BundleSid}/Copies"
    84  
    85  	path = strings.Replace(path, "{"+"BundleSid"+"}", BundleSid, -1)
    86  
    87  	data := url.Values{}
    88  	headers := make(map[string]interface{})
    89  
    90  	if params != nil && params.PageSize != nil {
    91  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
    92  	}
    93  
    94  	if pageToken != "" {
    95  		data.Set("PageToken", pageToken)
    96  	}
    97  	if pageNumber != "" {
    98  		data.Set("Page", pageNumber)
    99  	}
   100  
   101  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  
   106  	defer resp.Body.Close()
   107  
   108  	ps := &ListBundleCopyResponse{}
   109  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   110  		return nil, err
   111  	}
   112  
   113  	return ps, err
   114  }
   115  
   116  // Lists BundleCopy records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   117  func (c *ApiService) ListBundleCopy(BundleSid string, params *ListBundleCopyParams) ([]NumbersV2BundleCopy, error) {
   118  	response, errors := c.StreamBundleCopy(BundleSid, params)
   119  
   120  	records := make([]NumbersV2BundleCopy, 0)
   121  	for record := range response {
   122  		records = append(records, record)
   123  	}
   124  
   125  	if err := <-errors; err != nil {
   126  		return nil, err
   127  	}
   128  
   129  	return records, nil
   130  }
   131  
   132  // Streams BundleCopy records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   133  func (c *ApiService) StreamBundleCopy(BundleSid string, params *ListBundleCopyParams) (chan NumbersV2BundleCopy, chan error) {
   134  	if params == nil {
   135  		params = &ListBundleCopyParams{}
   136  	}
   137  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   138  
   139  	recordChannel := make(chan NumbersV2BundleCopy, 1)
   140  	errorChannel := make(chan error, 1)
   141  
   142  	response, err := c.PageBundleCopy(BundleSid, params, "", "")
   143  	if err != nil {
   144  		errorChannel <- err
   145  		close(recordChannel)
   146  		close(errorChannel)
   147  	} else {
   148  		go c.streamBundleCopy(response, params, recordChannel, errorChannel)
   149  	}
   150  
   151  	return recordChannel, errorChannel
   152  }
   153  
   154  func (c *ApiService) streamBundleCopy(response *ListBundleCopyResponse, params *ListBundleCopyParams, recordChannel chan NumbersV2BundleCopy, errorChannel chan error) {
   155  	curRecord := 1
   156  
   157  	for response != nil {
   158  		responseRecords := response.Results
   159  		for item := range responseRecords {
   160  			recordChannel <- responseRecords[item]
   161  			curRecord += 1
   162  			if params.Limit != nil && *params.Limit < curRecord {
   163  				close(recordChannel)
   164  				close(errorChannel)
   165  				return
   166  			}
   167  		}
   168  
   169  		record, err := client.GetNext(c.baseURL, response, c.getNextListBundleCopyResponse)
   170  		if err != nil {
   171  			errorChannel <- err
   172  			break
   173  		} else if record == nil {
   174  			break
   175  		}
   176  
   177  		response = record.(*ListBundleCopyResponse)
   178  	}
   179  
   180  	close(recordChannel)
   181  	close(errorChannel)
   182  }
   183  
   184  func (c *ApiService) getNextListBundleCopyResponse(nextPageUrl string) (interface{}, error) {
   185  	if nextPageUrl == "" {
   186  		return nil, nil
   187  	}
   188  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   189  	if err != nil {
   190  		return nil, err
   191  	}
   192  
   193  	defer resp.Body.Close()
   194  
   195  	ps := &ListBundleCopyResponse{}
   196  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   197  		return nil, err
   198  	}
   199  	return ps, nil
   200  }