github.com/twilio/twilio-go@v1.20.1/rest/video/v1/rooms_participants.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  //
    28  func (c *ApiService) FetchRoomParticipant(RoomSid string, Sid string) (*VideoV1RoomParticipant, error) {
    29  	path := "/v1/Rooms/{RoomSid}/Participants/{Sid}"
    30  	path = strings.Replace(path, "{"+"RoomSid"+"}", RoomSid, -1)
    31  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    32  
    33  	data := url.Values{}
    34  	headers := make(map[string]interface{})
    35  
    36  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	defer resp.Body.Close()
    42  
    43  	ps := &VideoV1RoomParticipant{}
    44  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	return ps, err
    49  }
    50  
    51  // Optional parameters for the method 'ListRoomParticipant'
    52  type ListRoomParticipantParams struct {
    53  	// Read only the participants with this status. Can be: `connected` or `disconnected`. For `in-progress` Rooms the default Status is `connected`, for `completed` Rooms only `disconnected` Participants are returned.
    54  	Status *string `json:"Status,omitempty"`
    55  	// Read only the Participants with this [User](https://www.twilio.com/docs/chat/rest/user-resource) `identity` value.
    56  	Identity *string `json:"Identity,omitempty"`
    57  	// Read only Participants that started after this date in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#UTC) format.
    58  	DateCreatedAfter *time.Time `json:"DateCreatedAfter,omitempty"`
    59  	// Read only Participants that started before this date in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#UTC) format.
    60  	DateCreatedBefore *time.Time `json:"DateCreatedBefore,omitempty"`
    61  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    62  	PageSize *int `json:"PageSize,omitempty"`
    63  	// Max number of records to return.
    64  	Limit *int `json:"limit,omitempty"`
    65  }
    66  
    67  func (params *ListRoomParticipantParams) SetStatus(Status string) *ListRoomParticipantParams {
    68  	params.Status = &Status
    69  	return params
    70  }
    71  func (params *ListRoomParticipantParams) SetIdentity(Identity string) *ListRoomParticipantParams {
    72  	params.Identity = &Identity
    73  	return params
    74  }
    75  func (params *ListRoomParticipantParams) SetDateCreatedAfter(DateCreatedAfter time.Time) *ListRoomParticipantParams {
    76  	params.DateCreatedAfter = &DateCreatedAfter
    77  	return params
    78  }
    79  func (params *ListRoomParticipantParams) SetDateCreatedBefore(DateCreatedBefore time.Time) *ListRoomParticipantParams {
    80  	params.DateCreatedBefore = &DateCreatedBefore
    81  	return params
    82  }
    83  func (params *ListRoomParticipantParams) SetPageSize(PageSize int) *ListRoomParticipantParams {
    84  	params.PageSize = &PageSize
    85  	return params
    86  }
    87  func (params *ListRoomParticipantParams) SetLimit(Limit int) *ListRoomParticipantParams {
    88  	params.Limit = &Limit
    89  	return params
    90  }
    91  
    92  // Retrieve a single page of RoomParticipant records from the API. Request is executed immediately.
    93  func (c *ApiService) PageRoomParticipant(RoomSid string, params *ListRoomParticipantParams, pageToken, pageNumber string) (*ListRoomParticipantResponse, error) {
    94  	path := "/v1/Rooms/{RoomSid}/Participants"
    95  
    96  	path = strings.Replace(path, "{"+"RoomSid"+"}", RoomSid, -1)
    97  
    98  	data := url.Values{}
    99  	headers := make(map[string]interface{})
   100  
   101  	if params != nil && params.Status != nil {
   102  		data.Set("Status", *params.Status)
   103  	}
   104  	if params != nil && params.Identity != nil {
   105  		data.Set("Identity", *params.Identity)
   106  	}
   107  	if params != nil && params.DateCreatedAfter != nil {
   108  		data.Set("DateCreatedAfter", fmt.Sprint((*params.DateCreatedAfter).Format(time.RFC3339)))
   109  	}
   110  	if params != nil && params.DateCreatedBefore != nil {
   111  		data.Set("DateCreatedBefore", fmt.Sprint((*params.DateCreatedBefore).Format(time.RFC3339)))
   112  	}
   113  	if params != nil && params.PageSize != nil {
   114  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   115  	}
   116  
   117  	if pageToken != "" {
   118  		data.Set("PageToken", pageToken)
   119  	}
   120  	if pageNumber != "" {
   121  		data.Set("Page", pageNumber)
   122  	}
   123  
   124  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  
   129  	defer resp.Body.Close()
   130  
   131  	ps := &ListRoomParticipantResponse{}
   132  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   133  		return nil, err
   134  	}
   135  
   136  	return ps, err
   137  }
   138  
   139  // Lists RoomParticipant records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   140  func (c *ApiService) ListRoomParticipant(RoomSid string, params *ListRoomParticipantParams) ([]VideoV1RoomParticipant, error) {
   141  	response, errors := c.StreamRoomParticipant(RoomSid, params)
   142  
   143  	records := make([]VideoV1RoomParticipant, 0)
   144  	for record := range response {
   145  		records = append(records, record)
   146  	}
   147  
   148  	if err := <-errors; err != nil {
   149  		return nil, err
   150  	}
   151  
   152  	return records, nil
   153  }
   154  
   155  // Streams RoomParticipant records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   156  func (c *ApiService) StreamRoomParticipant(RoomSid string, params *ListRoomParticipantParams) (chan VideoV1RoomParticipant, chan error) {
   157  	if params == nil {
   158  		params = &ListRoomParticipantParams{}
   159  	}
   160  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   161  
   162  	recordChannel := make(chan VideoV1RoomParticipant, 1)
   163  	errorChannel := make(chan error, 1)
   164  
   165  	response, err := c.PageRoomParticipant(RoomSid, params, "", "")
   166  	if err != nil {
   167  		errorChannel <- err
   168  		close(recordChannel)
   169  		close(errorChannel)
   170  	} else {
   171  		go c.streamRoomParticipant(response, params, recordChannel, errorChannel)
   172  	}
   173  
   174  	return recordChannel, errorChannel
   175  }
   176  
   177  func (c *ApiService) streamRoomParticipant(response *ListRoomParticipantResponse, params *ListRoomParticipantParams, recordChannel chan VideoV1RoomParticipant, errorChannel chan error) {
   178  	curRecord := 1
   179  
   180  	for response != nil {
   181  		responseRecords := response.Participants
   182  		for item := range responseRecords {
   183  			recordChannel <- responseRecords[item]
   184  			curRecord += 1
   185  			if params.Limit != nil && *params.Limit < curRecord {
   186  				close(recordChannel)
   187  				close(errorChannel)
   188  				return
   189  			}
   190  		}
   191  
   192  		record, err := client.GetNext(c.baseURL, response, c.getNextListRoomParticipantResponse)
   193  		if err != nil {
   194  			errorChannel <- err
   195  			break
   196  		} else if record == nil {
   197  			break
   198  		}
   199  
   200  		response = record.(*ListRoomParticipantResponse)
   201  	}
   202  
   203  	close(recordChannel)
   204  	close(errorChannel)
   205  }
   206  
   207  func (c *ApiService) getNextListRoomParticipantResponse(nextPageUrl string) (interface{}, error) {
   208  	if nextPageUrl == "" {
   209  		return nil, nil
   210  	}
   211  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   212  	if err != nil {
   213  		return nil, err
   214  	}
   215  
   216  	defer resp.Body.Close()
   217  
   218  	ps := &ListRoomParticipantResponse{}
   219  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   220  		return nil, err
   221  	}
   222  	return ps, nil
   223  }
   224  
   225  // Optional parameters for the method 'UpdateRoomParticipant'
   226  type UpdateRoomParticipantParams struct {
   227  	//
   228  	Status *string `json:"Status,omitempty"`
   229  }
   230  
   231  func (params *UpdateRoomParticipantParams) SetStatus(Status string) *UpdateRoomParticipantParams {
   232  	params.Status = &Status
   233  	return params
   234  }
   235  
   236  //
   237  func (c *ApiService) UpdateRoomParticipant(RoomSid string, Sid string, params *UpdateRoomParticipantParams) (*VideoV1RoomParticipant, error) {
   238  	path := "/v1/Rooms/{RoomSid}/Participants/{Sid}"
   239  	path = strings.Replace(path, "{"+"RoomSid"+"}", RoomSid, -1)
   240  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   241  
   242  	data := url.Values{}
   243  	headers := make(map[string]interface{})
   244  
   245  	if params != nil && params.Status != nil {
   246  		data.Set("Status", *params.Status)
   247  	}
   248  
   249  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   250  	if err != nil {
   251  		return nil, err
   252  	}
   253  
   254  	defer resp.Body.Close()
   255  
   256  	ps := &VideoV1RoomParticipant{}
   257  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   258  		return nil, err
   259  	}
   260  
   261  	return ps, err
   262  }