github.com/twilio/twilio-go@v1.20.1/rest/video/v1/compositions.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  // Optional parameters for the method 'CreateComposition'
    28  type CreateCompositionParams struct {
    29  	// The SID of the Group Room with the media tracks to be used as composition sources.
    30  	RoomSid *string `json:"RoomSid,omitempty"`
    31  	// An object that describes the video layout of the composition in terms of regions. See [Specifying Video Layouts](https://www.twilio.com/docs/video/api/compositions-resource#specifying-video-layouts) for more info. Please, be aware that either video_layout or audio_sources have to be provided to get a valid creation request
    32  	VideoLayout *interface{} `json:"VideoLayout,omitempty"`
    33  	// An array of track names from the same group room to merge into the new composition. Can include zero or more track names. The new composition includes all audio sources specified in `audio_sources` except for those specified in `audio_sources_excluded`. The track names in this parameter can include an asterisk as a wild card character, which will match zero or more characters in a track name. For example, `student*` includes `student` as well as `studentTeam`. Please, be aware that either video_layout or audio_sources have to be provided to get a valid creation request
    34  	AudioSources *[]string `json:"AudioSources,omitempty"`
    35  	// An array of track names to exclude. The new composition includes all audio sources specified in `audio_sources` except for those specified in `audio_sources_excluded`. The track names in this parameter can include an asterisk as a wild card character, which will match zero or more characters in a track name. For example, `student*` excludes `student` as well as `studentTeam`. This parameter can also be empty.
    36  	AudioSourcesExcluded *[]string `json:"AudioSourcesExcluded,omitempty"`
    37  	// A string that describes the columns (width) and rows (height) of the generated composed video in pixels. Defaults to `640x480`.  The string's format is `{width}x{height}` where:   * 16 <= `{width}` <= 1280 * 16 <= `{height}` <= 1280 * `{width}` * `{height}` <= 921,600  Typical values are:   * HD = `1280x720` * PAL = `1024x576` * VGA = `640x480` * CIF = `320x240`  Note that the `resolution` imposes an aspect ratio to the resulting composition. When the original video tracks are constrained by the aspect ratio, they are scaled to fit. See [Specifying Video Layouts](https://www.twilio.com/docs/video/api/compositions-resource#specifying-video-layouts) for more info.
    38  	Resolution *string `json:"Resolution,omitempty"`
    39  	//
    40  	Format *string `json:"Format,omitempty"`
    41  	// The URL we should call using the `status_callback_method` to send status information to your application on every composition event. If not provided, status callback events will not be dispatched.
    42  	StatusCallback *string `json:"StatusCallback,omitempty"`
    43  	// The HTTP method we should use to call `status_callback`. Can be: `POST` or `GET` and the default is `POST`.
    44  	StatusCallbackMethod *string `json:"StatusCallbackMethod,omitempty"`
    45  	// Whether to clip the intervals where there is no active media in the composition. The default is `true`. Compositions with `trim` enabled are shorter when the Room is created and no Participant joins for a while as well as if all the Participants leave the room and join later, because those gaps will be removed. See [Specifying Video Layouts](https://www.twilio.com/docs/video/api/compositions-resource#specifying-video-layouts) for more info.
    46  	Trim *bool `json:"Trim,omitempty"`
    47  }
    48  
    49  func (params *CreateCompositionParams) SetRoomSid(RoomSid string) *CreateCompositionParams {
    50  	params.RoomSid = &RoomSid
    51  	return params
    52  }
    53  func (params *CreateCompositionParams) SetVideoLayout(VideoLayout interface{}) *CreateCompositionParams {
    54  	params.VideoLayout = &VideoLayout
    55  	return params
    56  }
    57  func (params *CreateCompositionParams) SetAudioSources(AudioSources []string) *CreateCompositionParams {
    58  	params.AudioSources = &AudioSources
    59  	return params
    60  }
    61  func (params *CreateCompositionParams) SetAudioSourcesExcluded(AudioSourcesExcluded []string) *CreateCompositionParams {
    62  	params.AudioSourcesExcluded = &AudioSourcesExcluded
    63  	return params
    64  }
    65  func (params *CreateCompositionParams) SetResolution(Resolution string) *CreateCompositionParams {
    66  	params.Resolution = &Resolution
    67  	return params
    68  }
    69  func (params *CreateCompositionParams) SetFormat(Format string) *CreateCompositionParams {
    70  	params.Format = &Format
    71  	return params
    72  }
    73  func (params *CreateCompositionParams) SetStatusCallback(StatusCallback string) *CreateCompositionParams {
    74  	params.StatusCallback = &StatusCallback
    75  	return params
    76  }
    77  func (params *CreateCompositionParams) SetStatusCallbackMethod(StatusCallbackMethod string) *CreateCompositionParams {
    78  	params.StatusCallbackMethod = &StatusCallbackMethod
    79  	return params
    80  }
    81  func (params *CreateCompositionParams) SetTrim(Trim bool) *CreateCompositionParams {
    82  	params.Trim = &Trim
    83  	return params
    84  }
    85  
    86  //
    87  func (c *ApiService) CreateComposition(params *CreateCompositionParams) (*VideoV1Composition, error) {
    88  	path := "/v1/Compositions"
    89  
    90  	data := url.Values{}
    91  	headers := make(map[string]interface{})
    92  
    93  	if params != nil && params.RoomSid != nil {
    94  		data.Set("RoomSid", *params.RoomSid)
    95  	}
    96  	if params != nil && params.VideoLayout != nil {
    97  		v, err := json.Marshal(params.VideoLayout)
    98  
    99  		if err != nil {
   100  			return nil, err
   101  		}
   102  
   103  		data.Set("VideoLayout", string(v))
   104  	}
   105  	if params != nil && params.AudioSources != nil {
   106  		for _, item := range *params.AudioSources {
   107  			data.Add("AudioSources", item)
   108  		}
   109  	}
   110  	if params != nil && params.AudioSourcesExcluded != nil {
   111  		for _, item := range *params.AudioSourcesExcluded {
   112  			data.Add("AudioSourcesExcluded", item)
   113  		}
   114  	}
   115  	if params != nil && params.Resolution != nil {
   116  		data.Set("Resolution", *params.Resolution)
   117  	}
   118  	if params != nil && params.Format != nil {
   119  		data.Set("Format", *params.Format)
   120  	}
   121  	if params != nil && params.StatusCallback != nil {
   122  		data.Set("StatusCallback", *params.StatusCallback)
   123  	}
   124  	if params != nil && params.StatusCallbackMethod != nil {
   125  		data.Set("StatusCallbackMethod", *params.StatusCallbackMethod)
   126  	}
   127  	if params != nil && params.Trim != nil {
   128  		data.Set("Trim", fmt.Sprint(*params.Trim))
   129  	}
   130  
   131  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  
   136  	defer resp.Body.Close()
   137  
   138  	ps := &VideoV1Composition{}
   139  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   140  		return nil, err
   141  	}
   142  
   143  	return ps, err
   144  }
   145  
   146  // Delete a Recording Composition resource identified by a Composition SID.
   147  func (c *ApiService) DeleteComposition(Sid string) error {
   148  	path := "/v1/Compositions/{Sid}"
   149  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   150  
   151  	data := url.Values{}
   152  	headers := make(map[string]interface{})
   153  
   154  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   155  	if err != nil {
   156  		return err
   157  	}
   158  
   159  	defer resp.Body.Close()
   160  
   161  	return nil
   162  }
   163  
   164  // Returns a single Composition resource identified by a Composition SID.
   165  func (c *ApiService) FetchComposition(Sid string) (*VideoV1Composition, error) {
   166  	path := "/v1/Compositions/{Sid}"
   167  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   168  
   169  	data := url.Values{}
   170  	headers := make(map[string]interface{})
   171  
   172  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   173  	if err != nil {
   174  		return nil, err
   175  	}
   176  
   177  	defer resp.Body.Close()
   178  
   179  	ps := &VideoV1Composition{}
   180  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   181  		return nil, err
   182  	}
   183  
   184  	return ps, err
   185  }
   186  
   187  // Optional parameters for the method 'ListComposition'
   188  type ListCompositionParams struct {
   189  	// Read only Composition resources with this status. Can be: `enqueued`, `processing`, `completed`, `deleted`, or `failed`.
   190  	Status *string `json:"Status,omitempty"`
   191  	// Read only Composition resources created on or after this [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date-time with time zone.
   192  	DateCreatedAfter *time.Time `json:"DateCreatedAfter,omitempty"`
   193  	// Read only Composition resources created before this ISO 8601 date-time with time zone.
   194  	DateCreatedBefore *time.Time `json:"DateCreatedBefore,omitempty"`
   195  	// Read only Composition resources with this Room SID.
   196  	RoomSid *string `json:"RoomSid,omitempty"`
   197  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   198  	PageSize *int `json:"PageSize,omitempty"`
   199  	// Max number of records to return.
   200  	Limit *int `json:"limit,omitempty"`
   201  }
   202  
   203  func (params *ListCompositionParams) SetStatus(Status string) *ListCompositionParams {
   204  	params.Status = &Status
   205  	return params
   206  }
   207  func (params *ListCompositionParams) SetDateCreatedAfter(DateCreatedAfter time.Time) *ListCompositionParams {
   208  	params.DateCreatedAfter = &DateCreatedAfter
   209  	return params
   210  }
   211  func (params *ListCompositionParams) SetDateCreatedBefore(DateCreatedBefore time.Time) *ListCompositionParams {
   212  	params.DateCreatedBefore = &DateCreatedBefore
   213  	return params
   214  }
   215  func (params *ListCompositionParams) SetRoomSid(RoomSid string) *ListCompositionParams {
   216  	params.RoomSid = &RoomSid
   217  	return params
   218  }
   219  func (params *ListCompositionParams) SetPageSize(PageSize int) *ListCompositionParams {
   220  	params.PageSize = &PageSize
   221  	return params
   222  }
   223  func (params *ListCompositionParams) SetLimit(Limit int) *ListCompositionParams {
   224  	params.Limit = &Limit
   225  	return params
   226  }
   227  
   228  // Retrieve a single page of Composition records from the API. Request is executed immediately.
   229  func (c *ApiService) PageComposition(params *ListCompositionParams, pageToken, pageNumber string) (*ListCompositionResponse, error) {
   230  	path := "/v1/Compositions"
   231  
   232  	data := url.Values{}
   233  	headers := make(map[string]interface{})
   234  
   235  	if params != nil && params.Status != nil {
   236  		data.Set("Status", *params.Status)
   237  	}
   238  	if params != nil && params.DateCreatedAfter != nil {
   239  		data.Set("DateCreatedAfter", fmt.Sprint((*params.DateCreatedAfter).Format(time.RFC3339)))
   240  	}
   241  	if params != nil && params.DateCreatedBefore != nil {
   242  		data.Set("DateCreatedBefore", fmt.Sprint((*params.DateCreatedBefore).Format(time.RFC3339)))
   243  	}
   244  	if params != nil && params.RoomSid != nil {
   245  		data.Set("RoomSid", *params.RoomSid)
   246  	}
   247  	if params != nil && params.PageSize != nil {
   248  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   249  	}
   250  
   251  	if pageToken != "" {
   252  		data.Set("PageToken", pageToken)
   253  	}
   254  	if pageNumber != "" {
   255  		data.Set("Page", pageNumber)
   256  	}
   257  
   258  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   259  	if err != nil {
   260  		return nil, err
   261  	}
   262  
   263  	defer resp.Body.Close()
   264  
   265  	ps := &ListCompositionResponse{}
   266  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   267  		return nil, err
   268  	}
   269  
   270  	return ps, err
   271  }
   272  
   273  // Lists Composition records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   274  func (c *ApiService) ListComposition(params *ListCompositionParams) ([]VideoV1Composition, error) {
   275  	response, errors := c.StreamComposition(params)
   276  
   277  	records := make([]VideoV1Composition, 0)
   278  	for record := range response {
   279  		records = append(records, record)
   280  	}
   281  
   282  	if err := <-errors; err != nil {
   283  		return nil, err
   284  	}
   285  
   286  	return records, nil
   287  }
   288  
   289  // Streams Composition records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   290  func (c *ApiService) StreamComposition(params *ListCompositionParams) (chan VideoV1Composition, chan error) {
   291  	if params == nil {
   292  		params = &ListCompositionParams{}
   293  	}
   294  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   295  
   296  	recordChannel := make(chan VideoV1Composition, 1)
   297  	errorChannel := make(chan error, 1)
   298  
   299  	response, err := c.PageComposition(params, "", "")
   300  	if err != nil {
   301  		errorChannel <- err
   302  		close(recordChannel)
   303  		close(errorChannel)
   304  	} else {
   305  		go c.streamComposition(response, params, recordChannel, errorChannel)
   306  	}
   307  
   308  	return recordChannel, errorChannel
   309  }
   310  
   311  func (c *ApiService) streamComposition(response *ListCompositionResponse, params *ListCompositionParams, recordChannel chan VideoV1Composition, errorChannel chan error) {
   312  	curRecord := 1
   313  
   314  	for response != nil {
   315  		responseRecords := response.Compositions
   316  		for item := range responseRecords {
   317  			recordChannel <- responseRecords[item]
   318  			curRecord += 1
   319  			if params.Limit != nil && *params.Limit < curRecord {
   320  				close(recordChannel)
   321  				close(errorChannel)
   322  				return
   323  			}
   324  		}
   325  
   326  		record, err := client.GetNext(c.baseURL, response, c.getNextListCompositionResponse)
   327  		if err != nil {
   328  			errorChannel <- err
   329  			break
   330  		} else if record == nil {
   331  			break
   332  		}
   333  
   334  		response = record.(*ListCompositionResponse)
   335  	}
   336  
   337  	close(recordChannel)
   338  	close(errorChannel)
   339  }
   340  
   341  func (c *ApiService) getNextListCompositionResponse(nextPageUrl string) (interface{}, error) {
   342  	if nextPageUrl == "" {
   343  		return nil, nil
   344  	}
   345  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   346  	if err != nil {
   347  		return nil, err
   348  	}
   349  
   350  	defer resp.Body.Close()
   351  
   352  	ps := &ListCompositionResponse{}
   353  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   354  		return nil, err
   355  	}
   356  	return ps, nil
   357  }