github.com/twilio/twilio-go@v1.20.1/rest/api/v2010/accounts_calls_recordings.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Api
     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 'CreateCallRecording'
    27  type CreateCallRecordingParams struct {
    28  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that will create the resource.
    29  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
    30  	// The recording status events on which we should call the `recording_status_callback` URL. Can be: `in-progress`, `completed` and `absent` and the default is `completed`. Separate multiple event values with a space.
    31  	RecordingStatusCallbackEvent *[]string `json:"RecordingStatusCallbackEvent,omitempty"`
    32  	// The URL we should call using the `recording_status_callback_method` on each recording event specified in  `recording_status_callback_event`. For more information, see [RecordingStatusCallback parameters](https://www.twilio.com/docs/voice/api/recording#recordingstatuscallback).
    33  	RecordingStatusCallback *string `json:"RecordingStatusCallback,omitempty"`
    34  	// The HTTP method we should use to call `recording_status_callback`. Can be: `GET` or `POST` and the default is `POST`.
    35  	RecordingStatusCallbackMethod *string `json:"RecordingStatusCallbackMethod,omitempty"`
    36  	// Whether to trim any leading and trailing silence in the recording. Can be: `trim-silence` or `do-not-trim` and the default is `do-not-trim`. `trim-silence` trims the silence from the beginning and end of the recording and `do-not-trim` does not.
    37  	Trim *string `json:"Trim,omitempty"`
    38  	// The number of channels used in the recording. Can be: `mono` or `dual` and the default is `mono`. `mono` records all parties of the call into one channel. `dual` records each party of a 2-party call into separate channels.
    39  	RecordingChannels *string `json:"RecordingChannels,omitempty"`
    40  	// The audio track to record for the call. Can be: `inbound`, `outbound` or `both`. The default is `both`. `inbound` records the audio that is received by Twilio. `outbound` records the audio that is generated from Twilio. `both` records the audio that is received and generated by Twilio.
    41  	RecordingTrack *string `json:"RecordingTrack,omitempty"`
    42  }
    43  
    44  func (params *CreateCallRecordingParams) SetPathAccountSid(PathAccountSid string) *CreateCallRecordingParams {
    45  	params.PathAccountSid = &PathAccountSid
    46  	return params
    47  }
    48  func (params *CreateCallRecordingParams) SetRecordingStatusCallbackEvent(RecordingStatusCallbackEvent []string) *CreateCallRecordingParams {
    49  	params.RecordingStatusCallbackEvent = &RecordingStatusCallbackEvent
    50  	return params
    51  }
    52  func (params *CreateCallRecordingParams) SetRecordingStatusCallback(RecordingStatusCallback string) *CreateCallRecordingParams {
    53  	params.RecordingStatusCallback = &RecordingStatusCallback
    54  	return params
    55  }
    56  func (params *CreateCallRecordingParams) SetRecordingStatusCallbackMethod(RecordingStatusCallbackMethod string) *CreateCallRecordingParams {
    57  	params.RecordingStatusCallbackMethod = &RecordingStatusCallbackMethod
    58  	return params
    59  }
    60  func (params *CreateCallRecordingParams) SetTrim(Trim string) *CreateCallRecordingParams {
    61  	params.Trim = &Trim
    62  	return params
    63  }
    64  func (params *CreateCallRecordingParams) SetRecordingChannels(RecordingChannels string) *CreateCallRecordingParams {
    65  	params.RecordingChannels = &RecordingChannels
    66  	return params
    67  }
    68  func (params *CreateCallRecordingParams) SetRecordingTrack(RecordingTrack string) *CreateCallRecordingParams {
    69  	params.RecordingTrack = &RecordingTrack
    70  	return params
    71  }
    72  
    73  // Create a recording for the call
    74  func (c *ApiService) CreateCallRecording(CallSid string, params *CreateCallRecordingParams) (*ApiV2010CallRecording, error) {
    75  	path := "/2010-04-01/Accounts/{AccountSid}/Calls/{CallSid}/Recordings.json"
    76  	if params != nil && params.PathAccountSid != nil {
    77  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
    78  	} else {
    79  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
    80  	}
    81  	path = strings.Replace(path, "{"+"CallSid"+"}", CallSid, -1)
    82  
    83  	data := url.Values{}
    84  	headers := make(map[string]interface{})
    85  
    86  	if params != nil && params.RecordingStatusCallbackEvent != nil {
    87  		for _, item := range *params.RecordingStatusCallbackEvent {
    88  			data.Add("RecordingStatusCallbackEvent", item)
    89  		}
    90  	}
    91  	if params != nil && params.RecordingStatusCallback != nil {
    92  		data.Set("RecordingStatusCallback", *params.RecordingStatusCallback)
    93  	}
    94  	if params != nil && params.RecordingStatusCallbackMethod != nil {
    95  		data.Set("RecordingStatusCallbackMethod", *params.RecordingStatusCallbackMethod)
    96  	}
    97  	if params != nil && params.Trim != nil {
    98  		data.Set("Trim", *params.Trim)
    99  	}
   100  	if params != nil && params.RecordingChannels != nil {
   101  		data.Set("RecordingChannels", *params.RecordingChannels)
   102  	}
   103  	if params != nil && params.RecordingTrack != nil {
   104  		data.Set("RecordingTrack", *params.RecordingTrack)
   105  	}
   106  
   107  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  
   112  	defer resp.Body.Close()
   113  
   114  	ps := &ApiV2010CallRecording{}
   115  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   116  		return nil, err
   117  	}
   118  
   119  	return ps, err
   120  }
   121  
   122  // Optional parameters for the method 'DeleteCallRecording'
   123  type DeleteCallRecordingParams struct {
   124  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the Recording resources to delete.
   125  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
   126  }
   127  
   128  func (params *DeleteCallRecordingParams) SetPathAccountSid(PathAccountSid string) *DeleteCallRecordingParams {
   129  	params.PathAccountSid = &PathAccountSid
   130  	return params
   131  }
   132  
   133  // Delete a recording from your account
   134  func (c *ApiService) DeleteCallRecording(CallSid string, Sid string, params *DeleteCallRecordingParams) error {
   135  	path := "/2010-04-01/Accounts/{AccountSid}/Calls/{CallSid}/Recordings/{Sid}.json"
   136  	if params != nil && params.PathAccountSid != nil {
   137  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
   138  	} else {
   139  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
   140  	}
   141  	path = strings.Replace(path, "{"+"CallSid"+"}", CallSid, -1)
   142  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   143  
   144  	data := url.Values{}
   145  	headers := make(map[string]interface{})
   146  
   147  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   148  	if err != nil {
   149  		return err
   150  	}
   151  
   152  	defer resp.Body.Close()
   153  
   154  	return nil
   155  }
   156  
   157  // Optional parameters for the method 'FetchCallRecording'
   158  type FetchCallRecordingParams struct {
   159  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the Recording resource to fetch.
   160  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
   161  }
   162  
   163  func (params *FetchCallRecordingParams) SetPathAccountSid(PathAccountSid string) *FetchCallRecordingParams {
   164  	params.PathAccountSid = &PathAccountSid
   165  	return params
   166  }
   167  
   168  // Fetch an instance of a recording for a call
   169  func (c *ApiService) FetchCallRecording(CallSid string, Sid string, params *FetchCallRecordingParams) (*ApiV2010CallRecording, error) {
   170  	path := "/2010-04-01/Accounts/{AccountSid}/Calls/{CallSid}/Recordings/{Sid}.json"
   171  	if params != nil && params.PathAccountSid != nil {
   172  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
   173  	} else {
   174  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
   175  	}
   176  	path = strings.Replace(path, "{"+"CallSid"+"}", CallSid, -1)
   177  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   178  
   179  	data := url.Values{}
   180  	headers := make(map[string]interface{})
   181  
   182  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   183  	if err != nil {
   184  		return nil, err
   185  	}
   186  
   187  	defer resp.Body.Close()
   188  
   189  	ps := &ApiV2010CallRecording{}
   190  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   191  		return nil, err
   192  	}
   193  
   194  	return ps, err
   195  }
   196  
   197  // Optional parameters for the method 'ListCallRecording'
   198  type ListCallRecordingParams struct {
   199  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the Recording resources to read.
   200  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
   201  	// The `date_created` value, specified as `YYYY-MM-DD`, of the resources to read. You can also specify inequality: `DateCreated<=YYYY-MM-DD` will return recordings generated at or before midnight on a given date, and `DateCreated>=YYYY-MM-DD` returns recordings generated at or after midnight on a date.
   202  	DateCreated *string `json:"DateCreated,omitempty"`
   203  	// The `date_created` value, specified as `YYYY-MM-DD`, of the resources to read. You can also specify inequality: `DateCreated<=YYYY-MM-DD` will return recordings generated at or before midnight on a given date, and `DateCreated>=YYYY-MM-DD` returns recordings generated at or after midnight on a date.
   204  	DateCreatedBefore *string `json:"DateCreated&lt;,omitempty"`
   205  	// The `date_created` value, specified as `YYYY-MM-DD`, of the resources to read. You can also specify inequality: `DateCreated<=YYYY-MM-DD` will return recordings generated at or before midnight on a given date, and `DateCreated>=YYYY-MM-DD` returns recordings generated at or after midnight on a date.
   206  	DateCreatedAfter *string `json:"DateCreated&gt;,omitempty"`
   207  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   208  	PageSize *int `json:"PageSize,omitempty"`
   209  	// Max number of records to return.
   210  	Limit *int `json:"limit,omitempty"`
   211  }
   212  
   213  func (params *ListCallRecordingParams) SetPathAccountSid(PathAccountSid string) *ListCallRecordingParams {
   214  	params.PathAccountSid = &PathAccountSid
   215  	return params
   216  }
   217  func (params *ListCallRecordingParams) SetDateCreated(DateCreated string) *ListCallRecordingParams {
   218  	params.DateCreated = &DateCreated
   219  	return params
   220  }
   221  func (params *ListCallRecordingParams) SetDateCreatedBefore(DateCreatedBefore string) *ListCallRecordingParams {
   222  	params.DateCreatedBefore = &DateCreatedBefore
   223  	return params
   224  }
   225  func (params *ListCallRecordingParams) SetDateCreatedAfter(DateCreatedAfter string) *ListCallRecordingParams {
   226  	params.DateCreatedAfter = &DateCreatedAfter
   227  	return params
   228  }
   229  func (params *ListCallRecordingParams) SetPageSize(PageSize int) *ListCallRecordingParams {
   230  	params.PageSize = &PageSize
   231  	return params
   232  }
   233  func (params *ListCallRecordingParams) SetLimit(Limit int) *ListCallRecordingParams {
   234  	params.Limit = &Limit
   235  	return params
   236  }
   237  
   238  // Retrieve a single page of CallRecording records from the API. Request is executed immediately.
   239  func (c *ApiService) PageCallRecording(CallSid string, params *ListCallRecordingParams, pageToken, pageNumber string) (*ListCallRecordingResponse, error) {
   240  	path := "/2010-04-01/Accounts/{AccountSid}/Calls/{CallSid}/Recordings.json"
   241  
   242  	if params != nil && params.PathAccountSid != nil {
   243  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
   244  	} else {
   245  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
   246  	}
   247  	path = strings.Replace(path, "{"+"CallSid"+"}", CallSid, -1)
   248  
   249  	data := url.Values{}
   250  	headers := make(map[string]interface{})
   251  
   252  	if params != nil && params.DateCreated != nil {
   253  		data.Set("DateCreated", fmt.Sprint(*params.DateCreated))
   254  	}
   255  	if params != nil && params.DateCreatedBefore != nil {
   256  		data.Set("DateCreated<", fmt.Sprint(*params.DateCreatedBefore))
   257  	}
   258  	if params != nil && params.DateCreatedAfter != nil {
   259  		data.Set("DateCreated>", fmt.Sprint(*params.DateCreatedAfter))
   260  	}
   261  	if params != nil && params.PageSize != nil {
   262  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   263  	}
   264  
   265  	if pageToken != "" {
   266  		data.Set("PageToken", pageToken)
   267  	}
   268  	if pageNumber != "" {
   269  		data.Set("Page", pageNumber)
   270  	}
   271  
   272  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   273  	if err != nil {
   274  		return nil, err
   275  	}
   276  
   277  	defer resp.Body.Close()
   278  
   279  	ps := &ListCallRecordingResponse{}
   280  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   281  		return nil, err
   282  	}
   283  
   284  	return ps, err
   285  }
   286  
   287  // Lists CallRecording records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   288  func (c *ApiService) ListCallRecording(CallSid string, params *ListCallRecordingParams) ([]ApiV2010CallRecording, error) {
   289  	response, errors := c.StreamCallRecording(CallSid, params)
   290  
   291  	records := make([]ApiV2010CallRecording, 0)
   292  	for record := range response {
   293  		records = append(records, record)
   294  	}
   295  
   296  	if err := <-errors; err != nil {
   297  		return nil, err
   298  	}
   299  
   300  	return records, nil
   301  }
   302  
   303  // Streams CallRecording records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   304  func (c *ApiService) StreamCallRecording(CallSid string, params *ListCallRecordingParams) (chan ApiV2010CallRecording, chan error) {
   305  	if params == nil {
   306  		params = &ListCallRecordingParams{}
   307  	}
   308  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   309  
   310  	recordChannel := make(chan ApiV2010CallRecording, 1)
   311  	errorChannel := make(chan error, 1)
   312  
   313  	response, err := c.PageCallRecording(CallSid, params, "", "")
   314  	if err != nil {
   315  		errorChannel <- err
   316  		close(recordChannel)
   317  		close(errorChannel)
   318  	} else {
   319  		go c.streamCallRecording(response, params, recordChannel, errorChannel)
   320  	}
   321  
   322  	return recordChannel, errorChannel
   323  }
   324  
   325  func (c *ApiService) streamCallRecording(response *ListCallRecordingResponse, params *ListCallRecordingParams, recordChannel chan ApiV2010CallRecording, errorChannel chan error) {
   326  	curRecord := 1
   327  
   328  	for response != nil {
   329  		responseRecords := response.Recordings
   330  		for item := range responseRecords {
   331  			recordChannel <- responseRecords[item]
   332  			curRecord += 1
   333  			if params.Limit != nil && *params.Limit < curRecord {
   334  				close(recordChannel)
   335  				close(errorChannel)
   336  				return
   337  			}
   338  		}
   339  
   340  		record, err := client.GetNext(c.baseURL, response, c.getNextListCallRecordingResponse)
   341  		if err != nil {
   342  			errorChannel <- err
   343  			break
   344  		} else if record == nil {
   345  			break
   346  		}
   347  
   348  		response = record.(*ListCallRecordingResponse)
   349  	}
   350  
   351  	close(recordChannel)
   352  	close(errorChannel)
   353  }
   354  
   355  func (c *ApiService) getNextListCallRecordingResponse(nextPageUrl string) (interface{}, error) {
   356  	if nextPageUrl == "" {
   357  		return nil, nil
   358  	}
   359  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   360  	if err != nil {
   361  		return nil, err
   362  	}
   363  
   364  	defer resp.Body.Close()
   365  
   366  	ps := &ListCallRecordingResponse{}
   367  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   368  		return nil, err
   369  	}
   370  	return ps, nil
   371  }
   372  
   373  // Optional parameters for the method 'UpdateCallRecording'
   374  type UpdateCallRecordingParams struct {
   375  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the Recording resource to update.
   376  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
   377  	//
   378  	Status *string `json:"Status,omitempty"`
   379  	// Whether to record during a pause. Can be: `skip` or `silence` and the default is `silence`. `skip` does not record during the pause period, while `silence` will replace the actual audio of the call with silence during the pause period. This parameter only applies when setting `status` is set to `paused`.
   380  	PauseBehavior *string `json:"PauseBehavior,omitempty"`
   381  }
   382  
   383  func (params *UpdateCallRecordingParams) SetPathAccountSid(PathAccountSid string) *UpdateCallRecordingParams {
   384  	params.PathAccountSid = &PathAccountSid
   385  	return params
   386  }
   387  func (params *UpdateCallRecordingParams) SetStatus(Status string) *UpdateCallRecordingParams {
   388  	params.Status = &Status
   389  	return params
   390  }
   391  func (params *UpdateCallRecordingParams) SetPauseBehavior(PauseBehavior string) *UpdateCallRecordingParams {
   392  	params.PauseBehavior = &PauseBehavior
   393  	return params
   394  }
   395  
   396  // Changes the status of the recording to paused, stopped, or in-progress. Note: Pass `Twilio.CURRENT` instead of recording sid to reference current active recording.
   397  func (c *ApiService) UpdateCallRecording(CallSid string, Sid string, params *UpdateCallRecordingParams) (*ApiV2010CallRecording, error) {
   398  	path := "/2010-04-01/Accounts/{AccountSid}/Calls/{CallSid}/Recordings/{Sid}.json"
   399  	if params != nil && params.PathAccountSid != nil {
   400  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
   401  	} else {
   402  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
   403  	}
   404  	path = strings.Replace(path, "{"+"CallSid"+"}", CallSid, -1)
   405  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   406  
   407  	data := url.Values{}
   408  	headers := make(map[string]interface{})
   409  
   410  	if params != nil && params.Status != nil {
   411  		data.Set("Status", *params.Status)
   412  	}
   413  	if params != nil && params.PauseBehavior != nil {
   414  		data.Set("PauseBehavior", *params.PauseBehavior)
   415  	}
   416  
   417  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   418  	if err != nil {
   419  		return nil, err
   420  	}
   421  
   422  	defer resp.Body.Close()
   423  
   424  	ps := &ApiV2010CallRecording{}
   425  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   426  		return nil, err
   427  	}
   428  
   429  	return ps, err
   430  }