github.com/twilio/twilio-go@v1.20.1/rest/flex/v1/interactions_channels_participants.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Flex
     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 'CreateInteractionChannelParticipant'
    27  type CreateInteractionChannelParticipantParams struct {
    28  	//
    29  	Type *string `json:"Type,omitempty"`
    30  	// JSON representing the Media Properties for the new Participant.
    31  	MediaProperties *interface{} `json:"MediaProperties,omitempty"`
    32  	// Object representing the Routing Properties for the new Participant.
    33  	RoutingProperties *interface{} `json:"RoutingProperties,omitempty"`
    34  }
    35  
    36  func (params *CreateInteractionChannelParticipantParams) SetType(Type string) *CreateInteractionChannelParticipantParams {
    37  	params.Type = &Type
    38  	return params
    39  }
    40  func (params *CreateInteractionChannelParticipantParams) SetMediaProperties(MediaProperties interface{}) *CreateInteractionChannelParticipantParams {
    41  	params.MediaProperties = &MediaProperties
    42  	return params
    43  }
    44  func (params *CreateInteractionChannelParticipantParams) SetRoutingProperties(RoutingProperties interface{}) *CreateInteractionChannelParticipantParams {
    45  	params.RoutingProperties = &RoutingProperties
    46  	return params
    47  }
    48  
    49  // Add a Participant to a Channel.
    50  func (c *ApiService) CreateInteractionChannelParticipant(InteractionSid string, ChannelSid string, params *CreateInteractionChannelParticipantParams) (*FlexV1InteractionChannelParticipant, error) {
    51  	path := "/v1/Interactions/{InteractionSid}/Channels/{ChannelSid}/Participants"
    52  	path = strings.Replace(path, "{"+"InteractionSid"+"}", InteractionSid, -1)
    53  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
    54  
    55  	data := url.Values{}
    56  	headers := make(map[string]interface{})
    57  
    58  	if params != nil && params.Type != nil {
    59  		data.Set("Type", *params.Type)
    60  	}
    61  	if params != nil && params.MediaProperties != nil {
    62  		v, err := json.Marshal(params.MediaProperties)
    63  
    64  		if err != nil {
    65  			return nil, err
    66  		}
    67  
    68  		data.Set("MediaProperties", string(v))
    69  	}
    70  	if params != nil && params.RoutingProperties != nil {
    71  		v, err := json.Marshal(params.RoutingProperties)
    72  
    73  		if err != nil {
    74  			return nil, err
    75  		}
    76  
    77  		data.Set("RoutingProperties", string(v))
    78  	}
    79  
    80  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	defer resp.Body.Close()
    86  
    87  	ps := &FlexV1InteractionChannelParticipant{}
    88  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    89  		return nil, err
    90  	}
    91  
    92  	return ps, err
    93  }
    94  
    95  // Optional parameters for the method 'ListInteractionChannelParticipant'
    96  type ListInteractionChannelParticipantParams struct {
    97  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    98  	PageSize *int `json:"PageSize,omitempty"`
    99  	// Max number of records to return.
   100  	Limit *int `json:"limit,omitempty"`
   101  }
   102  
   103  func (params *ListInteractionChannelParticipantParams) SetPageSize(PageSize int) *ListInteractionChannelParticipantParams {
   104  	params.PageSize = &PageSize
   105  	return params
   106  }
   107  func (params *ListInteractionChannelParticipantParams) SetLimit(Limit int) *ListInteractionChannelParticipantParams {
   108  	params.Limit = &Limit
   109  	return params
   110  }
   111  
   112  // Retrieve a single page of InteractionChannelParticipant records from the API. Request is executed immediately.
   113  func (c *ApiService) PageInteractionChannelParticipant(InteractionSid string, ChannelSid string, params *ListInteractionChannelParticipantParams, pageToken, pageNumber string) (*ListInteractionChannelParticipantResponse, error) {
   114  	path := "/v1/Interactions/{InteractionSid}/Channels/{ChannelSid}/Participants"
   115  
   116  	path = strings.Replace(path, "{"+"InteractionSid"+"}", InteractionSid, -1)
   117  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
   118  
   119  	data := url.Values{}
   120  	headers := make(map[string]interface{})
   121  
   122  	if params != nil && params.PageSize != nil {
   123  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   124  	}
   125  
   126  	if pageToken != "" {
   127  		data.Set("PageToken", pageToken)
   128  	}
   129  	if pageNumber != "" {
   130  		data.Set("Page", pageNumber)
   131  	}
   132  
   133  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  
   138  	defer resp.Body.Close()
   139  
   140  	ps := &ListInteractionChannelParticipantResponse{}
   141  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   142  		return nil, err
   143  	}
   144  
   145  	return ps, err
   146  }
   147  
   148  // Lists InteractionChannelParticipant records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   149  func (c *ApiService) ListInteractionChannelParticipant(InteractionSid string, ChannelSid string, params *ListInteractionChannelParticipantParams) ([]FlexV1InteractionChannelParticipant, error) {
   150  	response, errors := c.StreamInteractionChannelParticipant(InteractionSid, ChannelSid, params)
   151  
   152  	records := make([]FlexV1InteractionChannelParticipant, 0)
   153  	for record := range response {
   154  		records = append(records, record)
   155  	}
   156  
   157  	if err := <-errors; err != nil {
   158  		return nil, err
   159  	}
   160  
   161  	return records, nil
   162  }
   163  
   164  // Streams InteractionChannelParticipant records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   165  func (c *ApiService) StreamInteractionChannelParticipant(InteractionSid string, ChannelSid string, params *ListInteractionChannelParticipantParams) (chan FlexV1InteractionChannelParticipant, chan error) {
   166  	if params == nil {
   167  		params = &ListInteractionChannelParticipantParams{}
   168  	}
   169  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   170  
   171  	recordChannel := make(chan FlexV1InteractionChannelParticipant, 1)
   172  	errorChannel := make(chan error, 1)
   173  
   174  	response, err := c.PageInteractionChannelParticipant(InteractionSid, ChannelSid, params, "", "")
   175  	if err != nil {
   176  		errorChannel <- err
   177  		close(recordChannel)
   178  		close(errorChannel)
   179  	} else {
   180  		go c.streamInteractionChannelParticipant(response, params, recordChannel, errorChannel)
   181  	}
   182  
   183  	return recordChannel, errorChannel
   184  }
   185  
   186  func (c *ApiService) streamInteractionChannelParticipant(response *ListInteractionChannelParticipantResponse, params *ListInteractionChannelParticipantParams, recordChannel chan FlexV1InteractionChannelParticipant, errorChannel chan error) {
   187  	curRecord := 1
   188  
   189  	for response != nil {
   190  		responseRecords := response.Participants
   191  		for item := range responseRecords {
   192  			recordChannel <- responseRecords[item]
   193  			curRecord += 1
   194  			if params.Limit != nil && *params.Limit < curRecord {
   195  				close(recordChannel)
   196  				close(errorChannel)
   197  				return
   198  			}
   199  		}
   200  
   201  		record, err := client.GetNext(c.baseURL, response, c.getNextListInteractionChannelParticipantResponse)
   202  		if err != nil {
   203  			errorChannel <- err
   204  			break
   205  		} else if record == nil {
   206  			break
   207  		}
   208  
   209  		response = record.(*ListInteractionChannelParticipantResponse)
   210  	}
   211  
   212  	close(recordChannel)
   213  	close(errorChannel)
   214  }
   215  
   216  func (c *ApiService) getNextListInteractionChannelParticipantResponse(nextPageUrl string) (interface{}, error) {
   217  	if nextPageUrl == "" {
   218  		return nil, nil
   219  	}
   220  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   221  	if err != nil {
   222  		return nil, err
   223  	}
   224  
   225  	defer resp.Body.Close()
   226  
   227  	ps := &ListInteractionChannelParticipantResponse{}
   228  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   229  		return nil, err
   230  	}
   231  	return ps, nil
   232  }
   233  
   234  // Optional parameters for the method 'UpdateInteractionChannelParticipant'
   235  type UpdateInteractionChannelParticipantParams struct {
   236  	//
   237  	Status *string `json:"Status,omitempty"`
   238  }
   239  
   240  func (params *UpdateInteractionChannelParticipantParams) SetStatus(Status string) *UpdateInteractionChannelParticipantParams {
   241  	params.Status = &Status
   242  	return params
   243  }
   244  
   245  // Update an existing Channel Participant.
   246  func (c *ApiService) UpdateInteractionChannelParticipant(InteractionSid string, ChannelSid string, Sid string, params *UpdateInteractionChannelParticipantParams) (*FlexV1InteractionChannelParticipant, error) {
   247  	path := "/v1/Interactions/{InteractionSid}/Channels/{ChannelSid}/Participants/{Sid}"
   248  	path = strings.Replace(path, "{"+"InteractionSid"+"}", InteractionSid, -1)
   249  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
   250  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   251  
   252  	data := url.Values{}
   253  	headers := make(map[string]interface{})
   254  
   255  	if params != nil && params.Status != nil {
   256  		data.Set("Status", *params.Status)
   257  	}
   258  
   259  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   260  	if err != nil {
   261  		return nil, err
   262  	}
   263  
   264  	defer resp.Body.Close()
   265  
   266  	ps := &FlexV1InteractionChannelParticipant{}
   267  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   268  		return nil, err
   269  	}
   270  
   271  	return ps, err
   272  }