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