github.com/twilio/twilio-go@v1.20.1/rest/flex/v1/insights_segments.go (about)

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