github.com/twilio/twilio-go@v1.20.1/rest/intelligence/v2/transcripts_operator_results.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 'FetchOperatorResult'
    27  type FetchOperatorResultParams struct {
    28  	// Grant access to PII redacted/unredacted Language Understanding operator. If redaction is enabled, the default is True.
    29  	Redacted *bool `json:"Redacted,omitempty"`
    30  }
    31  
    32  func (params *FetchOperatorResultParams) SetRedacted(Redacted bool) *FetchOperatorResultParams {
    33  	params.Redacted = &Redacted
    34  	return params
    35  }
    36  
    37  // Fetch a specific Operator Result for the given Transcript.
    38  func (c *ApiService) FetchOperatorResult(TranscriptSid string, OperatorSid string, params *FetchOperatorResultParams) (*IntelligenceV2OperatorResult, error) {
    39  	path := "/v2/Transcripts/{TranscriptSid}/OperatorResults/{OperatorSid}"
    40  	path = strings.Replace(path, "{"+"TranscriptSid"+"}", TranscriptSid, -1)
    41  	path = strings.Replace(path, "{"+"OperatorSid"+"}", OperatorSid, -1)
    42  
    43  	data := url.Values{}
    44  	headers := make(map[string]interface{})
    45  
    46  	if params != nil && params.Redacted != nil {
    47  		data.Set("Redacted", fmt.Sprint(*params.Redacted))
    48  	}
    49  
    50  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	defer resp.Body.Close()
    56  
    57  	ps := &IntelligenceV2OperatorResult{}
    58  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	return ps, err
    63  }
    64  
    65  // Optional parameters for the method 'ListOperatorResult'
    66  type ListOperatorResultParams struct {
    67  	// Grant access to PII redacted/unredacted Language Understanding operator. If redaction is enabled, the default is True.
    68  	Redacted *bool `json:"Redacted,omitempty"`
    69  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    70  	PageSize *int `json:"PageSize,omitempty"`
    71  	// Max number of records to return.
    72  	Limit *int `json:"limit,omitempty"`
    73  }
    74  
    75  func (params *ListOperatorResultParams) SetRedacted(Redacted bool) *ListOperatorResultParams {
    76  	params.Redacted = &Redacted
    77  	return params
    78  }
    79  func (params *ListOperatorResultParams) SetPageSize(PageSize int) *ListOperatorResultParams {
    80  	params.PageSize = &PageSize
    81  	return params
    82  }
    83  func (params *ListOperatorResultParams) SetLimit(Limit int) *ListOperatorResultParams {
    84  	params.Limit = &Limit
    85  	return params
    86  }
    87  
    88  // Retrieve a single page of OperatorResult records from the API. Request is executed immediately.
    89  func (c *ApiService) PageOperatorResult(TranscriptSid string, params *ListOperatorResultParams, pageToken, pageNumber string) (*ListOperatorResultResponse, error) {
    90  	path := "/v2/Transcripts/{TranscriptSid}/OperatorResults"
    91  
    92  	path = strings.Replace(path, "{"+"TranscriptSid"+"}", TranscriptSid, -1)
    93  
    94  	data := url.Values{}
    95  	headers := make(map[string]interface{})
    96  
    97  	if params != nil && params.Redacted != nil {
    98  		data.Set("Redacted", fmt.Sprint(*params.Redacted))
    99  	}
   100  	if params != nil && params.PageSize != nil {
   101  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   102  	}
   103  
   104  	if pageToken != "" {
   105  		data.Set("PageToken", pageToken)
   106  	}
   107  	if pageNumber != "" {
   108  		data.Set("Page", pageNumber)
   109  	}
   110  
   111  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  
   116  	defer resp.Body.Close()
   117  
   118  	ps := &ListOperatorResultResponse{}
   119  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   120  		return nil, err
   121  	}
   122  
   123  	return ps, err
   124  }
   125  
   126  // Lists OperatorResult records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   127  func (c *ApiService) ListOperatorResult(TranscriptSid string, params *ListOperatorResultParams) ([]IntelligenceV2OperatorResult, error) {
   128  	response, errors := c.StreamOperatorResult(TranscriptSid, params)
   129  
   130  	records := make([]IntelligenceV2OperatorResult, 0)
   131  	for record := range response {
   132  		records = append(records, record)
   133  	}
   134  
   135  	if err := <-errors; err != nil {
   136  		return nil, err
   137  	}
   138  
   139  	return records, nil
   140  }
   141  
   142  // Streams OperatorResult records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   143  func (c *ApiService) StreamOperatorResult(TranscriptSid string, params *ListOperatorResultParams) (chan IntelligenceV2OperatorResult, chan error) {
   144  	if params == nil {
   145  		params = &ListOperatorResultParams{}
   146  	}
   147  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   148  
   149  	recordChannel := make(chan IntelligenceV2OperatorResult, 1)
   150  	errorChannel := make(chan error, 1)
   151  
   152  	response, err := c.PageOperatorResult(TranscriptSid, params, "", "")
   153  	if err != nil {
   154  		errorChannel <- err
   155  		close(recordChannel)
   156  		close(errorChannel)
   157  	} else {
   158  		go c.streamOperatorResult(response, params, recordChannel, errorChannel)
   159  	}
   160  
   161  	return recordChannel, errorChannel
   162  }
   163  
   164  func (c *ApiService) streamOperatorResult(response *ListOperatorResultResponse, params *ListOperatorResultParams, recordChannel chan IntelligenceV2OperatorResult, errorChannel chan error) {
   165  	curRecord := 1
   166  
   167  	for response != nil {
   168  		responseRecords := response.OperatorResults
   169  		for item := range responseRecords {
   170  			recordChannel <- responseRecords[item]
   171  			curRecord += 1
   172  			if params.Limit != nil && *params.Limit < curRecord {
   173  				close(recordChannel)
   174  				close(errorChannel)
   175  				return
   176  			}
   177  		}
   178  
   179  		record, err := client.GetNext(c.baseURL, response, c.getNextListOperatorResultResponse)
   180  		if err != nil {
   181  			errorChannel <- err
   182  			break
   183  		} else if record == nil {
   184  			break
   185  		}
   186  
   187  		response = record.(*ListOperatorResultResponse)
   188  	}
   189  
   190  	close(recordChannel)
   191  	close(errorChannel)
   192  }
   193  
   194  func (c *ApiService) getNextListOperatorResultResponse(nextPageUrl string) (interface{}, error) {
   195  	if nextPageUrl == "" {
   196  		return nil, nil
   197  	}
   198  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   199  	if err != nil {
   200  		return nil, err
   201  	}
   202  
   203  	defer resp.Body.Close()
   204  
   205  	ps := &ListOperatorResultResponse{}
   206  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   207  		return nil, err
   208  	}
   209  	return ps, nil
   210  }