github.com/twilio/twilio-go@v1.20.1/rest/insights/v1/video_rooms.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Insights
     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  // Get Video Log Analyzer data for a Room.
    28  func (c *ApiService) FetchVideoRoomSummary(RoomSid string) (*InsightsV1VideoRoomSummary, error) {
    29  	path := "/v1/Video/Rooms/{RoomSid}"
    30  	path = strings.Replace(path, "{"+"RoomSid"+"}", RoomSid, -1)
    31  
    32  	data := url.Values{}
    33  	headers := make(map[string]interface{})
    34  
    35  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	defer resp.Body.Close()
    41  
    42  	ps := &InsightsV1VideoRoomSummary{}
    43  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	return ps, err
    48  }
    49  
    50  // Optional parameters for the method 'ListVideoRoomSummary'
    51  type ListVideoRoomSummaryParams struct {
    52  	// Type of room. Can be `go`, `peer_to_peer`, `group`, or `group_small`.
    53  	RoomType *[]string `json:"RoomType,omitempty"`
    54  	// Codecs used by participants in the room. Can be `VP8`, `H264`, or `VP9`.
    55  	Codec *[]string `json:"Codec,omitempty"`
    56  	// Room friendly name.
    57  	RoomName *string `json:"RoomName,omitempty"`
    58  	// Only read rooms that started on or after this ISO 8601 timestamp.
    59  	CreatedAfter *time.Time `json:"CreatedAfter,omitempty"`
    60  	// Only read rooms that started before this ISO 8601 timestamp.
    61  	CreatedBefore *time.Time `json:"CreatedBefore,omitempty"`
    62  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    63  	PageSize *int `json:"PageSize,omitempty"`
    64  	// Max number of records to return.
    65  	Limit *int `json:"limit,omitempty"`
    66  }
    67  
    68  func (params *ListVideoRoomSummaryParams) SetRoomType(RoomType []string) *ListVideoRoomSummaryParams {
    69  	params.RoomType = &RoomType
    70  	return params
    71  }
    72  func (params *ListVideoRoomSummaryParams) SetCodec(Codec []string) *ListVideoRoomSummaryParams {
    73  	params.Codec = &Codec
    74  	return params
    75  }
    76  func (params *ListVideoRoomSummaryParams) SetRoomName(RoomName string) *ListVideoRoomSummaryParams {
    77  	params.RoomName = &RoomName
    78  	return params
    79  }
    80  func (params *ListVideoRoomSummaryParams) SetCreatedAfter(CreatedAfter time.Time) *ListVideoRoomSummaryParams {
    81  	params.CreatedAfter = &CreatedAfter
    82  	return params
    83  }
    84  func (params *ListVideoRoomSummaryParams) SetCreatedBefore(CreatedBefore time.Time) *ListVideoRoomSummaryParams {
    85  	params.CreatedBefore = &CreatedBefore
    86  	return params
    87  }
    88  func (params *ListVideoRoomSummaryParams) SetPageSize(PageSize int) *ListVideoRoomSummaryParams {
    89  	params.PageSize = &PageSize
    90  	return params
    91  }
    92  func (params *ListVideoRoomSummaryParams) SetLimit(Limit int) *ListVideoRoomSummaryParams {
    93  	params.Limit = &Limit
    94  	return params
    95  }
    96  
    97  // Retrieve a single page of VideoRoomSummary records from the API. Request is executed immediately.
    98  func (c *ApiService) PageVideoRoomSummary(params *ListVideoRoomSummaryParams, pageToken, pageNumber string) (*ListVideoRoomSummaryResponse, error) {
    99  	path := "/v1/Video/Rooms"
   100  
   101  	data := url.Values{}
   102  	headers := make(map[string]interface{})
   103  
   104  	if params != nil && params.RoomType != nil {
   105  		for _, item := range *params.RoomType {
   106  			data.Add("RoomType", item)
   107  		}
   108  	}
   109  	if params != nil && params.Codec != nil {
   110  		for _, item := range *params.Codec {
   111  			data.Add("Codec", item)
   112  		}
   113  	}
   114  	if params != nil && params.RoomName != nil {
   115  		data.Set("RoomName", *params.RoomName)
   116  	}
   117  	if params != nil && params.CreatedAfter != nil {
   118  		data.Set("CreatedAfter", fmt.Sprint((*params.CreatedAfter).Format(time.RFC3339)))
   119  	}
   120  	if params != nil && params.CreatedBefore != nil {
   121  		data.Set("CreatedBefore", fmt.Sprint((*params.CreatedBefore).Format(time.RFC3339)))
   122  	}
   123  	if params != nil && params.PageSize != nil {
   124  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   125  	}
   126  
   127  	if pageToken != "" {
   128  		data.Set("PageToken", pageToken)
   129  	}
   130  	if pageNumber != "" {
   131  		data.Set("Page", pageNumber)
   132  	}
   133  
   134  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   135  	if err != nil {
   136  		return nil, err
   137  	}
   138  
   139  	defer resp.Body.Close()
   140  
   141  	ps := &ListVideoRoomSummaryResponse{}
   142  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   143  		return nil, err
   144  	}
   145  
   146  	return ps, err
   147  }
   148  
   149  // Lists VideoRoomSummary records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   150  func (c *ApiService) ListVideoRoomSummary(params *ListVideoRoomSummaryParams) ([]InsightsV1VideoRoomSummary, error) {
   151  	response, errors := c.StreamVideoRoomSummary(params)
   152  
   153  	records := make([]InsightsV1VideoRoomSummary, 0)
   154  	for record := range response {
   155  		records = append(records, record)
   156  	}
   157  
   158  	if err := <-errors; err != nil {
   159  		return nil, err
   160  	}
   161  
   162  	return records, nil
   163  }
   164  
   165  // Streams VideoRoomSummary records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   166  func (c *ApiService) StreamVideoRoomSummary(params *ListVideoRoomSummaryParams) (chan InsightsV1VideoRoomSummary, chan error) {
   167  	if params == nil {
   168  		params = &ListVideoRoomSummaryParams{}
   169  	}
   170  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   171  
   172  	recordChannel := make(chan InsightsV1VideoRoomSummary, 1)
   173  	errorChannel := make(chan error, 1)
   174  
   175  	response, err := c.PageVideoRoomSummary(params, "", "")
   176  	if err != nil {
   177  		errorChannel <- err
   178  		close(recordChannel)
   179  		close(errorChannel)
   180  	} else {
   181  		go c.streamVideoRoomSummary(response, params, recordChannel, errorChannel)
   182  	}
   183  
   184  	return recordChannel, errorChannel
   185  }
   186  
   187  func (c *ApiService) streamVideoRoomSummary(response *ListVideoRoomSummaryResponse, params *ListVideoRoomSummaryParams, recordChannel chan InsightsV1VideoRoomSummary, errorChannel chan error) {
   188  	curRecord := 1
   189  
   190  	for response != nil {
   191  		responseRecords := response.Rooms
   192  		for item := range responseRecords {
   193  			recordChannel <- responseRecords[item]
   194  			curRecord += 1
   195  			if params.Limit != nil && *params.Limit < curRecord {
   196  				close(recordChannel)
   197  				close(errorChannel)
   198  				return
   199  			}
   200  		}
   201  
   202  		record, err := client.GetNext(c.baseURL, response, c.getNextListVideoRoomSummaryResponse)
   203  		if err != nil {
   204  			errorChannel <- err
   205  			break
   206  		} else if record == nil {
   207  			break
   208  		}
   209  
   210  		response = record.(*ListVideoRoomSummaryResponse)
   211  	}
   212  
   213  	close(recordChannel)
   214  	close(errorChannel)
   215  }
   216  
   217  func (c *ApiService) getNextListVideoRoomSummaryResponse(nextPageUrl string) (interface{}, error) {
   218  	if nextPageUrl == "" {
   219  		return nil, nil
   220  	}
   221  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   222  	if err != nil {
   223  		return nil, err
   224  	}
   225  
   226  	defer resp.Body.Close()
   227  
   228  	ps := &ListVideoRoomSummaryResponse{}
   229  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   230  		return nil, err
   231  	}
   232  	return ps, nil
   233  }