github.com/twilio/twilio-go@v1.20.1/rest/proxy/v1/services_sessions_participants.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Proxy
     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  // Optional parameters for the method 'CreateParticipant'
    27  type CreateParticipantParams struct {
    28  	// The phone number of the Participant.
    29  	Identifier *string `json:"Identifier,omitempty"`
    30  	// The string that you assigned to describe the participant. This value must be 255 characters or fewer. **This value should not have PII.**
    31  	FriendlyName *string `json:"FriendlyName,omitempty"`
    32  	// The proxy phone number to use for the Participant. If not specified, Proxy will select a number from the pool.
    33  	ProxyIdentifier *string `json:"ProxyIdentifier,omitempty"`
    34  	// The SID of the Proxy Identifier to assign to the Participant.
    35  	ProxyIdentifierSid *string `json:"ProxyIdentifierSid,omitempty"`
    36  }
    37  
    38  func (params *CreateParticipantParams) SetIdentifier(Identifier string) *CreateParticipantParams {
    39  	params.Identifier = &Identifier
    40  	return params
    41  }
    42  func (params *CreateParticipantParams) SetFriendlyName(FriendlyName string) *CreateParticipantParams {
    43  	params.FriendlyName = &FriendlyName
    44  	return params
    45  }
    46  func (params *CreateParticipantParams) SetProxyIdentifier(ProxyIdentifier string) *CreateParticipantParams {
    47  	params.ProxyIdentifier = &ProxyIdentifier
    48  	return params
    49  }
    50  func (params *CreateParticipantParams) SetProxyIdentifierSid(ProxyIdentifierSid string) *CreateParticipantParams {
    51  	params.ProxyIdentifierSid = &ProxyIdentifierSid
    52  	return params
    53  }
    54  
    55  // Add a new Participant to the Session
    56  func (c *ApiService) CreateParticipant(ServiceSid string, SessionSid string, params *CreateParticipantParams) (*ProxyV1Participant, error) {
    57  	path := "/v1/Services/{ServiceSid}/Sessions/{SessionSid}/Participants"
    58  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    59  	path = strings.Replace(path, "{"+"SessionSid"+"}", SessionSid, -1)
    60  
    61  	data := url.Values{}
    62  	headers := make(map[string]interface{})
    63  
    64  	if params != nil && params.Identifier != nil {
    65  		data.Set("Identifier", *params.Identifier)
    66  	}
    67  	if params != nil && params.FriendlyName != nil {
    68  		data.Set("FriendlyName", *params.FriendlyName)
    69  	}
    70  	if params != nil && params.ProxyIdentifier != nil {
    71  		data.Set("ProxyIdentifier", *params.ProxyIdentifier)
    72  	}
    73  	if params != nil && params.ProxyIdentifierSid != nil {
    74  		data.Set("ProxyIdentifierSid", *params.ProxyIdentifierSid)
    75  	}
    76  
    77  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  
    82  	defer resp.Body.Close()
    83  
    84  	ps := &ProxyV1Participant{}
    85  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	return ps, err
    90  }
    91  
    92  // Delete a specific Participant. This is a soft-delete. The participant remains associated with the session and cannot be re-added. Participants are only permanently deleted when the [Session](https://www.twilio.com/docs/proxy/api/session) is deleted.
    93  func (c *ApiService) DeleteParticipant(ServiceSid string, SessionSid string, Sid string) error {
    94  	path := "/v1/Services/{ServiceSid}/Sessions/{SessionSid}/Participants/{Sid}"
    95  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    96  	path = strings.Replace(path, "{"+"SessionSid"+"}", SessionSid, -1)
    97  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    98  
    99  	data := url.Values{}
   100  	headers := make(map[string]interface{})
   101  
   102  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   103  	if err != nil {
   104  		return err
   105  	}
   106  
   107  	defer resp.Body.Close()
   108  
   109  	return nil
   110  }
   111  
   112  // Fetch a specific Participant.
   113  func (c *ApiService) FetchParticipant(ServiceSid string, SessionSid string, Sid string) (*ProxyV1Participant, error) {
   114  	path := "/v1/Services/{ServiceSid}/Sessions/{SessionSid}/Participants/{Sid}"
   115  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   116  	path = strings.Replace(path, "{"+"SessionSid"+"}", SessionSid, -1)
   117  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   118  
   119  	data := url.Values{}
   120  	headers := make(map[string]interface{})
   121  
   122  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  
   127  	defer resp.Body.Close()
   128  
   129  	ps := &ProxyV1Participant{}
   130  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   131  		return nil, err
   132  	}
   133  
   134  	return ps, err
   135  }
   136  
   137  // Optional parameters for the method 'ListParticipant'
   138  type ListParticipantParams struct {
   139  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   140  	PageSize *int `json:"PageSize,omitempty"`
   141  	// Max number of records to return.
   142  	Limit *int `json:"limit,omitempty"`
   143  }
   144  
   145  func (params *ListParticipantParams) SetPageSize(PageSize int) *ListParticipantParams {
   146  	params.PageSize = &PageSize
   147  	return params
   148  }
   149  func (params *ListParticipantParams) SetLimit(Limit int) *ListParticipantParams {
   150  	params.Limit = &Limit
   151  	return params
   152  }
   153  
   154  // Retrieve a single page of Participant records from the API. Request is executed immediately.
   155  func (c *ApiService) PageParticipant(ServiceSid string, SessionSid string, params *ListParticipantParams, pageToken, pageNumber string) (*ListParticipantResponse, error) {
   156  	path := "/v1/Services/{ServiceSid}/Sessions/{SessionSid}/Participants"
   157  
   158  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   159  	path = strings.Replace(path, "{"+"SessionSid"+"}", SessionSid, -1)
   160  
   161  	data := url.Values{}
   162  	headers := make(map[string]interface{})
   163  
   164  	if params != nil && params.PageSize != nil {
   165  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   166  	}
   167  
   168  	if pageToken != "" {
   169  		data.Set("PageToken", pageToken)
   170  	}
   171  	if pageNumber != "" {
   172  		data.Set("Page", pageNumber)
   173  	}
   174  
   175  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   176  	if err != nil {
   177  		return nil, err
   178  	}
   179  
   180  	defer resp.Body.Close()
   181  
   182  	ps := &ListParticipantResponse{}
   183  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   184  		return nil, err
   185  	}
   186  
   187  	return ps, err
   188  }
   189  
   190  // Lists Participant records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   191  func (c *ApiService) ListParticipant(ServiceSid string, SessionSid string, params *ListParticipantParams) ([]ProxyV1Participant, error) {
   192  	response, errors := c.StreamParticipant(ServiceSid, SessionSid, params)
   193  
   194  	records := make([]ProxyV1Participant, 0)
   195  	for record := range response {
   196  		records = append(records, record)
   197  	}
   198  
   199  	if err := <-errors; err != nil {
   200  		return nil, err
   201  	}
   202  
   203  	return records, nil
   204  }
   205  
   206  // Streams Participant records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   207  func (c *ApiService) StreamParticipant(ServiceSid string, SessionSid string, params *ListParticipantParams) (chan ProxyV1Participant, chan error) {
   208  	if params == nil {
   209  		params = &ListParticipantParams{}
   210  	}
   211  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   212  
   213  	recordChannel := make(chan ProxyV1Participant, 1)
   214  	errorChannel := make(chan error, 1)
   215  
   216  	response, err := c.PageParticipant(ServiceSid, SessionSid, params, "", "")
   217  	if err != nil {
   218  		errorChannel <- err
   219  		close(recordChannel)
   220  		close(errorChannel)
   221  	} else {
   222  		go c.streamParticipant(response, params, recordChannel, errorChannel)
   223  	}
   224  
   225  	return recordChannel, errorChannel
   226  }
   227  
   228  func (c *ApiService) streamParticipant(response *ListParticipantResponse, params *ListParticipantParams, recordChannel chan ProxyV1Participant, errorChannel chan error) {
   229  	curRecord := 1
   230  
   231  	for response != nil {
   232  		responseRecords := response.Participants
   233  		for item := range responseRecords {
   234  			recordChannel <- responseRecords[item]
   235  			curRecord += 1
   236  			if params.Limit != nil && *params.Limit < curRecord {
   237  				close(recordChannel)
   238  				close(errorChannel)
   239  				return
   240  			}
   241  		}
   242  
   243  		record, err := client.GetNext(c.baseURL, response, c.getNextListParticipantResponse)
   244  		if err != nil {
   245  			errorChannel <- err
   246  			break
   247  		} else if record == nil {
   248  			break
   249  		}
   250  
   251  		response = record.(*ListParticipantResponse)
   252  	}
   253  
   254  	close(recordChannel)
   255  	close(errorChannel)
   256  }
   257  
   258  func (c *ApiService) getNextListParticipantResponse(nextPageUrl string) (interface{}, error) {
   259  	if nextPageUrl == "" {
   260  		return nil, nil
   261  	}
   262  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   263  	if err != nil {
   264  		return nil, err
   265  	}
   266  
   267  	defer resp.Body.Close()
   268  
   269  	ps := &ListParticipantResponse{}
   270  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   271  		return nil, err
   272  	}
   273  	return ps, nil
   274  }