github.com/twilio/twilio-go@v1.20.1/rest/intelligence/v2/transcripts.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  	"time"
    23  
    24  	"github.com/twilio/twilio-go/client"
    25  )
    26  
    27  // Optional parameters for the method 'CreateTranscript'
    28  type CreateTranscriptParams struct {
    29  	// The unique SID identifier of the Service.
    30  	ServiceSid *string `json:"ServiceSid,omitempty"`
    31  	// JSON object describing Media Channel including Source and Participants
    32  	Channel *interface{} `json:"Channel,omitempty"`
    33  	// Used to store client provided metadata. Maximum of 64 double-byte UTF8 characters.
    34  	CustomerKey *string `json:"CustomerKey,omitempty"`
    35  	// The date that this Transcript's media was started, given in ISO 8601 format.
    36  	MediaStartTime *time.Time `json:"MediaStartTime,omitempty"`
    37  }
    38  
    39  func (params *CreateTranscriptParams) SetServiceSid(ServiceSid string) *CreateTranscriptParams {
    40  	params.ServiceSid = &ServiceSid
    41  	return params
    42  }
    43  func (params *CreateTranscriptParams) SetChannel(Channel interface{}) *CreateTranscriptParams {
    44  	params.Channel = &Channel
    45  	return params
    46  }
    47  func (params *CreateTranscriptParams) SetCustomerKey(CustomerKey string) *CreateTranscriptParams {
    48  	params.CustomerKey = &CustomerKey
    49  	return params
    50  }
    51  func (params *CreateTranscriptParams) SetMediaStartTime(MediaStartTime time.Time) *CreateTranscriptParams {
    52  	params.MediaStartTime = &MediaStartTime
    53  	return params
    54  }
    55  
    56  // Create a new Transcript for the service
    57  func (c *ApiService) CreateTranscript(params *CreateTranscriptParams) (*IntelligenceV2Transcript, error) {
    58  	path := "/v2/Transcripts"
    59  
    60  	data := url.Values{}
    61  	headers := make(map[string]interface{})
    62  
    63  	if params != nil && params.ServiceSid != nil {
    64  		data.Set("ServiceSid", *params.ServiceSid)
    65  	}
    66  	if params != nil && params.Channel != nil {
    67  		v, err := json.Marshal(params.Channel)
    68  
    69  		if err != nil {
    70  			return nil, err
    71  		}
    72  
    73  		data.Set("Channel", string(v))
    74  	}
    75  	if params != nil && params.CustomerKey != nil {
    76  		data.Set("CustomerKey", *params.CustomerKey)
    77  	}
    78  	if params != nil && params.MediaStartTime != nil {
    79  		data.Set("MediaStartTime", fmt.Sprint((*params.MediaStartTime).Format(time.RFC3339)))
    80  	}
    81  
    82  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  
    87  	defer resp.Body.Close()
    88  
    89  	ps := &IntelligenceV2Transcript{}
    90  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    91  		return nil, err
    92  	}
    93  
    94  	return ps, err
    95  }
    96  
    97  // Delete a specific Transcript.
    98  func (c *ApiService) DeleteTranscript(Sid string) error {
    99  	path := "/v2/Transcripts/{Sid}"
   100  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   101  
   102  	data := url.Values{}
   103  	headers := make(map[string]interface{})
   104  
   105  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   106  	if err != nil {
   107  		return err
   108  	}
   109  
   110  	defer resp.Body.Close()
   111  
   112  	return nil
   113  }
   114  
   115  // Fetch a specific Transcript.
   116  func (c *ApiService) FetchTranscript(Sid string) (*IntelligenceV2Transcript, error) {
   117  	path := "/v2/Transcripts/{Sid}"
   118  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   119  
   120  	data := url.Values{}
   121  	headers := make(map[string]interface{})
   122  
   123  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   124  	if err != nil {
   125  		return nil, err
   126  	}
   127  
   128  	defer resp.Body.Close()
   129  
   130  	ps := &IntelligenceV2Transcript{}
   131  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   132  		return nil, err
   133  	}
   134  
   135  	return ps, err
   136  }
   137  
   138  // Optional parameters for the method 'ListTranscript'
   139  type ListTranscriptParams struct {
   140  	// The unique SID identifier of the Service.
   141  	ServiceSid *string `json:"ServiceSid,omitempty"`
   142  	// Filter by before StartTime.
   143  	BeforeStartTime *string `json:"BeforeStartTime,omitempty"`
   144  	// Filter by after StartTime.
   145  	AfterStartTime *string `json:"AfterStartTime,omitempty"`
   146  	// Filter by before DateCreated.
   147  	BeforeDateCreated *string `json:"BeforeDateCreated,omitempty"`
   148  	// Filter by after DateCreated.
   149  	AfterDateCreated *string `json:"AfterDateCreated,omitempty"`
   150  	// Filter by status.
   151  	Status *string `json:"Status,omitempty"`
   152  	// Filter by Language Code.
   153  	LanguageCode *string `json:"LanguageCode,omitempty"`
   154  	// Filter by SourceSid.
   155  	SourceSid *string `json:"SourceSid,omitempty"`
   156  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   157  	PageSize *int `json:"PageSize,omitempty"`
   158  	// Max number of records to return.
   159  	Limit *int `json:"limit,omitempty"`
   160  }
   161  
   162  func (params *ListTranscriptParams) SetServiceSid(ServiceSid string) *ListTranscriptParams {
   163  	params.ServiceSid = &ServiceSid
   164  	return params
   165  }
   166  func (params *ListTranscriptParams) SetBeforeStartTime(BeforeStartTime string) *ListTranscriptParams {
   167  	params.BeforeStartTime = &BeforeStartTime
   168  	return params
   169  }
   170  func (params *ListTranscriptParams) SetAfterStartTime(AfterStartTime string) *ListTranscriptParams {
   171  	params.AfterStartTime = &AfterStartTime
   172  	return params
   173  }
   174  func (params *ListTranscriptParams) SetBeforeDateCreated(BeforeDateCreated string) *ListTranscriptParams {
   175  	params.BeforeDateCreated = &BeforeDateCreated
   176  	return params
   177  }
   178  func (params *ListTranscriptParams) SetAfterDateCreated(AfterDateCreated string) *ListTranscriptParams {
   179  	params.AfterDateCreated = &AfterDateCreated
   180  	return params
   181  }
   182  func (params *ListTranscriptParams) SetStatus(Status string) *ListTranscriptParams {
   183  	params.Status = &Status
   184  	return params
   185  }
   186  func (params *ListTranscriptParams) SetLanguageCode(LanguageCode string) *ListTranscriptParams {
   187  	params.LanguageCode = &LanguageCode
   188  	return params
   189  }
   190  func (params *ListTranscriptParams) SetSourceSid(SourceSid string) *ListTranscriptParams {
   191  	params.SourceSid = &SourceSid
   192  	return params
   193  }
   194  func (params *ListTranscriptParams) SetPageSize(PageSize int) *ListTranscriptParams {
   195  	params.PageSize = &PageSize
   196  	return params
   197  }
   198  func (params *ListTranscriptParams) SetLimit(Limit int) *ListTranscriptParams {
   199  	params.Limit = &Limit
   200  	return params
   201  }
   202  
   203  // Retrieve a single page of Transcript records from the API. Request is executed immediately.
   204  func (c *ApiService) PageTranscript(params *ListTranscriptParams, pageToken, pageNumber string) (*ListTranscriptResponse, error) {
   205  	path := "/v2/Transcripts"
   206  
   207  	data := url.Values{}
   208  	headers := make(map[string]interface{})
   209  
   210  	if params != nil && params.ServiceSid != nil {
   211  		data.Set("ServiceSid", *params.ServiceSid)
   212  	}
   213  	if params != nil && params.BeforeStartTime != nil {
   214  		data.Set("BeforeStartTime", *params.BeforeStartTime)
   215  	}
   216  	if params != nil && params.AfterStartTime != nil {
   217  		data.Set("AfterStartTime", *params.AfterStartTime)
   218  	}
   219  	if params != nil && params.BeforeDateCreated != nil {
   220  		data.Set("BeforeDateCreated", *params.BeforeDateCreated)
   221  	}
   222  	if params != nil && params.AfterDateCreated != nil {
   223  		data.Set("AfterDateCreated", *params.AfterDateCreated)
   224  	}
   225  	if params != nil && params.Status != nil {
   226  		data.Set("Status", *params.Status)
   227  	}
   228  	if params != nil && params.LanguageCode != nil {
   229  		data.Set("LanguageCode", *params.LanguageCode)
   230  	}
   231  	if params != nil && params.SourceSid != nil {
   232  		data.Set("SourceSid", *params.SourceSid)
   233  	}
   234  	if params != nil && params.PageSize != nil {
   235  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   236  	}
   237  
   238  	if pageToken != "" {
   239  		data.Set("PageToken", pageToken)
   240  	}
   241  	if pageNumber != "" {
   242  		data.Set("Page", pageNumber)
   243  	}
   244  
   245  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   246  	if err != nil {
   247  		return nil, err
   248  	}
   249  
   250  	defer resp.Body.Close()
   251  
   252  	ps := &ListTranscriptResponse{}
   253  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   254  		return nil, err
   255  	}
   256  
   257  	return ps, err
   258  }
   259  
   260  // Lists Transcript records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   261  func (c *ApiService) ListTranscript(params *ListTranscriptParams) ([]IntelligenceV2Transcript, error) {
   262  	response, errors := c.StreamTranscript(params)
   263  
   264  	records := make([]IntelligenceV2Transcript, 0)
   265  	for record := range response {
   266  		records = append(records, record)
   267  	}
   268  
   269  	if err := <-errors; err != nil {
   270  		return nil, err
   271  	}
   272  
   273  	return records, nil
   274  }
   275  
   276  // Streams Transcript records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   277  func (c *ApiService) StreamTranscript(params *ListTranscriptParams) (chan IntelligenceV2Transcript, chan error) {
   278  	if params == nil {
   279  		params = &ListTranscriptParams{}
   280  	}
   281  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   282  
   283  	recordChannel := make(chan IntelligenceV2Transcript, 1)
   284  	errorChannel := make(chan error, 1)
   285  
   286  	response, err := c.PageTranscript(params, "", "")
   287  	if err != nil {
   288  		errorChannel <- err
   289  		close(recordChannel)
   290  		close(errorChannel)
   291  	} else {
   292  		go c.streamTranscript(response, params, recordChannel, errorChannel)
   293  	}
   294  
   295  	return recordChannel, errorChannel
   296  }
   297  
   298  func (c *ApiService) streamTranscript(response *ListTranscriptResponse, params *ListTranscriptParams, recordChannel chan IntelligenceV2Transcript, errorChannel chan error) {
   299  	curRecord := 1
   300  
   301  	for response != nil {
   302  		responseRecords := response.Transcripts
   303  		for item := range responseRecords {
   304  			recordChannel <- responseRecords[item]
   305  			curRecord += 1
   306  			if params.Limit != nil && *params.Limit < curRecord {
   307  				close(recordChannel)
   308  				close(errorChannel)
   309  				return
   310  			}
   311  		}
   312  
   313  		record, err := client.GetNext(c.baseURL, response, c.getNextListTranscriptResponse)
   314  		if err != nil {
   315  			errorChannel <- err
   316  			break
   317  		} else if record == nil {
   318  			break
   319  		}
   320  
   321  		response = record.(*ListTranscriptResponse)
   322  	}
   323  
   324  	close(recordChannel)
   325  	close(errorChannel)
   326  }
   327  
   328  func (c *ApiService) getNextListTranscriptResponse(nextPageUrl string) (interface{}, error) {
   329  	if nextPageUrl == "" {
   330  		return nil, nil
   331  	}
   332  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   333  	if err != nil {
   334  		return nil, err
   335  	}
   336  
   337  	defer resp.Body.Close()
   338  
   339  	ps := &ListTranscriptResponse{}
   340  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   341  		return nil, err
   342  	}
   343  	return ps, nil
   344  }