github.com/twilio/twilio-go@v1.20.1/rest/intelligence/v2/transcripts_sentences.go (about)

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