github.com/twilio/twilio-go@v1.20.1/rest/insights/v1/video_rooms_participants.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  
    23  	"github.com/twilio/twilio-go/client"
    24  )
    25  
    26  // Get Video Log Analyzer data for a Room Participant.
    27  func (c *ApiService) FetchVideoParticipantSummary(RoomSid string, ParticipantSid string) (*InsightsV1VideoParticipantSummary, error) {
    28  	path := "/v1/Video/Rooms/{RoomSid}/Participants/{ParticipantSid}"
    29  	path = strings.Replace(path, "{"+"RoomSid"+"}", RoomSid, -1)
    30  	path = strings.Replace(path, "{"+"ParticipantSid"+"}", ParticipantSid, -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 := &InsightsV1VideoParticipantSummary{}
    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 'ListVideoParticipantSummary'
    51  type ListVideoParticipantSummaryParams struct {
    52  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    53  	PageSize *int `json:"PageSize,omitempty"`
    54  	// Max number of records to return.
    55  	Limit *int `json:"limit,omitempty"`
    56  }
    57  
    58  func (params *ListVideoParticipantSummaryParams) SetPageSize(PageSize int) *ListVideoParticipantSummaryParams {
    59  	params.PageSize = &PageSize
    60  	return params
    61  }
    62  func (params *ListVideoParticipantSummaryParams) SetLimit(Limit int) *ListVideoParticipantSummaryParams {
    63  	params.Limit = &Limit
    64  	return params
    65  }
    66  
    67  // Retrieve a single page of VideoParticipantSummary records from the API. Request is executed immediately.
    68  func (c *ApiService) PageVideoParticipantSummary(RoomSid string, params *ListVideoParticipantSummaryParams, pageToken, pageNumber string) (*ListVideoParticipantSummaryResponse, error) {
    69  	path := "/v1/Video/Rooms/{RoomSid}/Participants"
    70  
    71  	path = strings.Replace(path, "{"+"RoomSid"+"}", RoomSid, -1)
    72  
    73  	data := url.Values{}
    74  	headers := make(map[string]interface{})
    75  
    76  	if params != nil && params.PageSize != nil {
    77  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
    78  	}
    79  
    80  	if pageToken != "" {
    81  		data.Set("PageToken", pageToken)
    82  	}
    83  	if pageNumber != "" {
    84  		data.Set("Page", pageNumber)
    85  	}
    86  
    87  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  
    92  	defer resp.Body.Close()
    93  
    94  	ps := &ListVideoParticipantSummaryResponse{}
    95  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    96  		return nil, err
    97  	}
    98  
    99  	return ps, err
   100  }
   101  
   102  // Lists VideoParticipantSummary records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   103  func (c *ApiService) ListVideoParticipantSummary(RoomSid string, params *ListVideoParticipantSummaryParams) ([]InsightsV1VideoParticipantSummary, error) {
   104  	response, errors := c.StreamVideoParticipantSummary(RoomSid, params)
   105  
   106  	records := make([]InsightsV1VideoParticipantSummary, 0)
   107  	for record := range response {
   108  		records = append(records, record)
   109  	}
   110  
   111  	if err := <-errors; err != nil {
   112  		return nil, err
   113  	}
   114  
   115  	return records, nil
   116  }
   117  
   118  // Streams VideoParticipantSummary records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   119  func (c *ApiService) StreamVideoParticipantSummary(RoomSid string, params *ListVideoParticipantSummaryParams) (chan InsightsV1VideoParticipantSummary, chan error) {
   120  	if params == nil {
   121  		params = &ListVideoParticipantSummaryParams{}
   122  	}
   123  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   124  
   125  	recordChannel := make(chan InsightsV1VideoParticipantSummary, 1)
   126  	errorChannel := make(chan error, 1)
   127  
   128  	response, err := c.PageVideoParticipantSummary(RoomSid, params, "", "")
   129  	if err != nil {
   130  		errorChannel <- err
   131  		close(recordChannel)
   132  		close(errorChannel)
   133  	} else {
   134  		go c.streamVideoParticipantSummary(response, params, recordChannel, errorChannel)
   135  	}
   136  
   137  	return recordChannel, errorChannel
   138  }
   139  
   140  func (c *ApiService) streamVideoParticipantSummary(response *ListVideoParticipantSummaryResponse, params *ListVideoParticipantSummaryParams, recordChannel chan InsightsV1VideoParticipantSummary, errorChannel chan error) {
   141  	curRecord := 1
   142  
   143  	for response != nil {
   144  		responseRecords := response.Participants
   145  		for item := range responseRecords {
   146  			recordChannel <- responseRecords[item]
   147  			curRecord += 1
   148  			if params.Limit != nil && *params.Limit < curRecord {
   149  				close(recordChannel)
   150  				close(errorChannel)
   151  				return
   152  			}
   153  		}
   154  
   155  		record, err := client.GetNext(c.baseURL, response, c.getNextListVideoParticipantSummaryResponse)
   156  		if err != nil {
   157  			errorChannel <- err
   158  			break
   159  		} else if record == nil {
   160  			break
   161  		}
   162  
   163  		response = record.(*ListVideoParticipantSummaryResponse)
   164  	}
   165  
   166  	close(recordChannel)
   167  	close(errorChannel)
   168  }
   169  
   170  func (c *ApiService) getNextListVideoParticipantSummaryResponse(nextPageUrl string) (interface{}, error) {
   171  	if nextPageUrl == "" {
   172  		return nil, nil
   173  	}
   174  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   175  	if err != nil {
   176  		return nil, err
   177  	}
   178  
   179  	defer resp.Body.Close()
   180  
   181  	ps := &ListVideoParticipantSummaryResponse{}
   182  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   183  		return nil, err
   184  	}
   185  	return ps, nil
   186  }