github.com/twilio/twilio-go@v1.20.1/rest/content/v1/content_and_approvals.go (about)

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