github.com/twilio/twilio-go@v1.20.1/rest/video/v1/recordings.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Video
     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  // Delete a Recording resource identified by a Recording SID.
    28  func (c *ApiService) DeleteRecording(Sid string) error {
    29  	path := "/v1/Recordings/{Sid}"
    30  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    31  
    32  	data := url.Values{}
    33  	headers := make(map[string]interface{})
    34  
    35  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	defer resp.Body.Close()
    41  
    42  	return nil
    43  }
    44  
    45  // Returns a single Recording resource identified by a Recording SID.
    46  func (c *ApiService) FetchRecording(Sid string) (*VideoV1Recording, error) {
    47  	path := "/v1/Recordings/{Sid}"
    48  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    49  
    50  	data := url.Values{}
    51  	headers := make(map[string]interface{})
    52  
    53  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	defer resp.Body.Close()
    59  
    60  	ps := &VideoV1Recording{}
    61  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    62  		return nil, err
    63  	}
    64  
    65  	return ps, err
    66  }
    67  
    68  // Optional parameters for the method 'ListRecording'
    69  type ListRecordingParams struct {
    70  	// Read only the recordings that have this status. Can be: `processing`, `completed`, or `deleted`.
    71  	Status *string `json:"Status,omitempty"`
    72  	// Read only the recordings that have this `source_sid`.
    73  	SourceSid *string `json:"SourceSid,omitempty"`
    74  	// Read only recordings with this `grouping_sid`, which may include a `participant_sid` and/or a `room_sid`.
    75  	GroupingSid *[]string `json:"GroupingSid,omitempty"`
    76  	// Read only recordings that started on or after this [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date-time with time zone.
    77  	DateCreatedAfter *time.Time `json:"DateCreatedAfter,omitempty"`
    78  	// Read only recordings that started before this [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date-time with time zone, given as `YYYY-MM-DDThh:mm:ss+|-hh:mm` or `YYYY-MM-DDThh:mm:ssZ`.
    79  	DateCreatedBefore *time.Time `json:"DateCreatedBefore,omitempty"`
    80  	// Read only recordings that have this media type. Can be either `audio` or `video`.
    81  	MediaType *string `json:"MediaType,omitempty"`
    82  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    83  	PageSize *int `json:"PageSize,omitempty"`
    84  	// Max number of records to return.
    85  	Limit *int `json:"limit,omitempty"`
    86  }
    87  
    88  func (params *ListRecordingParams) SetStatus(Status string) *ListRecordingParams {
    89  	params.Status = &Status
    90  	return params
    91  }
    92  func (params *ListRecordingParams) SetSourceSid(SourceSid string) *ListRecordingParams {
    93  	params.SourceSid = &SourceSid
    94  	return params
    95  }
    96  func (params *ListRecordingParams) SetGroupingSid(GroupingSid []string) *ListRecordingParams {
    97  	params.GroupingSid = &GroupingSid
    98  	return params
    99  }
   100  func (params *ListRecordingParams) SetDateCreatedAfter(DateCreatedAfter time.Time) *ListRecordingParams {
   101  	params.DateCreatedAfter = &DateCreatedAfter
   102  	return params
   103  }
   104  func (params *ListRecordingParams) SetDateCreatedBefore(DateCreatedBefore time.Time) *ListRecordingParams {
   105  	params.DateCreatedBefore = &DateCreatedBefore
   106  	return params
   107  }
   108  func (params *ListRecordingParams) SetMediaType(MediaType string) *ListRecordingParams {
   109  	params.MediaType = &MediaType
   110  	return params
   111  }
   112  func (params *ListRecordingParams) SetPageSize(PageSize int) *ListRecordingParams {
   113  	params.PageSize = &PageSize
   114  	return params
   115  }
   116  func (params *ListRecordingParams) SetLimit(Limit int) *ListRecordingParams {
   117  	params.Limit = &Limit
   118  	return params
   119  }
   120  
   121  // Retrieve a single page of Recording records from the API. Request is executed immediately.
   122  func (c *ApiService) PageRecording(params *ListRecordingParams, pageToken, pageNumber string) (*ListRecordingResponse, error) {
   123  	path := "/v1/Recordings"
   124  
   125  	data := url.Values{}
   126  	headers := make(map[string]interface{})
   127  
   128  	if params != nil && params.Status != nil {
   129  		data.Set("Status", *params.Status)
   130  	}
   131  	if params != nil && params.SourceSid != nil {
   132  		data.Set("SourceSid", *params.SourceSid)
   133  	}
   134  	if params != nil && params.GroupingSid != nil {
   135  		for _, item := range *params.GroupingSid {
   136  			data.Add("GroupingSid", item)
   137  		}
   138  	}
   139  	if params != nil && params.DateCreatedAfter != nil {
   140  		data.Set("DateCreatedAfter", fmt.Sprint((*params.DateCreatedAfter).Format(time.RFC3339)))
   141  	}
   142  	if params != nil && params.DateCreatedBefore != nil {
   143  		data.Set("DateCreatedBefore", fmt.Sprint((*params.DateCreatedBefore).Format(time.RFC3339)))
   144  	}
   145  	if params != nil && params.MediaType != nil {
   146  		data.Set("MediaType", *params.MediaType)
   147  	}
   148  	if params != nil && params.PageSize != nil {
   149  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   150  	}
   151  
   152  	if pageToken != "" {
   153  		data.Set("PageToken", pageToken)
   154  	}
   155  	if pageNumber != "" {
   156  		data.Set("Page", pageNumber)
   157  	}
   158  
   159  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   160  	if err != nil {
   161  		return nil, err
   162  	}
   163  
   164  	defer resp.Body.Close()
   165  
   166  	ps := &ListRecordingResponse{}
   167  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   168  		return nil, err
   169  	}
   170  
   171  	return ps, err
   172  }
   173  
   174  // Lists Recording records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   175  func (c *ApiService) ListRecording(params *ListRecordingParams) ([]VideoV1Recording, error) {
   176  	response, errors := c.StreamRecording(params)
   177  
   178  	records := make([]VideoV1Recording, 0)
   179  	for record := range response {
   180  		records = append(records, record)
   181  	}
   182  
   183  	if err := <-errors; err != nil {
   184  		return nil, err
   185  	}
   186  
   187  	return records, nil
   188  }
   189  
   190  // Streams Recording records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   191  func (c *ApiService) StreamRecording(params *ListRecordingParams) (chan VideoV1Recording, chan error) {
   192  	if params == nil {
   193  		params = &ListRecordingParams{}
   194  	}
   195  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   196  
   197  	recordChannel := make(chan VideoV1Recording, 1)
   198  	errorChannel := make(chan error, 1)
   199  
   200  	response, err := c.PageRecording(params, "", "")
   201  	if err != nil {
   202  		errorChannel <- err
   203  		close(recordChannel)
   204  		close(errorChannel)
   205  	} else {
   206  		go c.streamRecording(response, params, recordChannel, errorChannel)
   207  	}
   208  
   209  	return recordChannel, errorChannel
   210  }
   211  
   212  func (c *ApiService) streamRecording(response *ListRecordingResponse, params *ListRecordingParams, recordChannel chan VideoV1Recording, errorChannel chan error) {
   213  	curRecord := 1
   214  
   215  	for response != nil {
   216  		responseRecords := response.Recordings
   217  		for item := range responseRecords {
   218  			recordChannel <- responseRecords[item]
   219  			curRecord += 1
   220  			if params.Limit != nil && *params.Limit < curRecord {
   221  				close(recordChannel)
   222  				close(errorChannel)
   223  				return
   224  			}
   225  		}
   226  
   227  		record, err := client.GetNext(c.baseURL, response, c.getNextListRecordingResponse)
   228  		if err != nil {
   229  			errorChannel <- err
   230  			break
   231  		} else if record == nil {
   232  			break
   233  		}
   234  
   235  		response = record.(*ListRecordingResponse)
   236  	}
   237  
   238  	close(recordChannel)
   239  	close(errorChannel)
   240  }
   241  
   242  func (c *ApiService) getNextListRecordingResponse(nextPageUrl string) (interface{}, error) {
   243  	if nextPageUrl == "" {
   244  		return nil, nil
   245  	}
   246  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   247  	if err != nil {
   248  		return nil, err
   249  	}
   250  
   251  	defer resp.Body.Close()
   252  
   253  	ps := &ListRecordingResponse{}
   254  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   255  		return nil, err
   256  	}
   257  	return ps, nil
   258  }