github.com/twilio/twilio-go@v1.20.1/rest/flex/v1/interactions_channels.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  // Fetch a Channel for an Interaction.
    27  func (c *ApiService) FetchInteractionChannel(InteractionSid string, Sid string) (*FlexV1InteractionChannel, error) {
    28  	path := "/v1/Interactions/{InteractionSid}/Channels/{Sid}"
    29  	path = strings.Replace(path, "{"+"InteractionSid"+"}", InteractionSid, -1)
    30  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -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 := &FlexV1InteractionChannel{}
    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 'ListInteractionChannel'
    51  type ListInteractionChannelParams 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 *ListInteractionChannelParams) SetPageSize(PageSize int) *ListInteractionChannelParams {
    59  	params.PageSize = &PageSize
    60  	return params
    61  }
    62  func (params *ListInteractionChannelParams) SetLimit(Limit int) *ListInteractionChannelParams {
    63  	params.Limit = &Limit
    64  	return params
    65  }
    66  
    67  // Retrieve a single page of InteractionChannel records from the API. Request is executed immediately.
    68  func (c *ApiService) PageInteractionChannel(InteractionSid string, params *ListInteractionChannelParams, pageToken, pageNumber string) (*ListInteractionChannelResponse, error) {
    69  	path := "/v1/Interactions/{InteractionSid}/Channels"
    70  
    71  	path = strings.Replace(path, "{"+"InteractionSid"+"}", InteractionSid, -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 := &ListInteractionChannelResponse{}
    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 InteractionChannel 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) ListInteractionChannel(InteractionSid string, params *ListInteractionChannelParams) ([]FlexV1InteractionChannel, error) {
   104  	response, errors := c.StreamInteractionChannel(InteractionSid, params)
   105  
   106  	records := make([]FlexV1InteractionChannel, 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 InteractionChannel 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) StreamInteractionChannel(InteractionSid string, params *ListInteractionChannelParams) (chan FlexV1InteractionChannel, chan error) {
   120  	if params == nil {
   121  		params = &ListInteractionChannelParams{}
   122  	}
   123  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   124  
   125  	recordChannel := make(chan FlexV1InteractionChannel, 1)
   126  	errorChannel := make(chan error, 1)
   127  
   128  	response, err := c.PageInteractionChannel(InteractionSid, params, "", "")
   129  	if err != nil {
   130  		errorChannel <- err
   131  		close(recordChannel)
   132  		close(errorChannel)
   133  	} else {
   134  		go c.streamInteractionChannel(response, params, recordChannel, errorChannel)
   135  	}
   136  
   137  	return recordChannel, errorChannel
   138  }
   139  
   140  func (c *ApiService) streamInteractionChannel(response *ListInteractionChannelResponse, params *ListInteractionChannelParams, recordChannel chan FlexV1InteractionChannel, errorChannel chan error) {
   141  	curRecord := 1
   142  
   143  	for response != nil {
   144  		responseRecords := response.Channels
   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.getNextListInteractionChannelResponse)
   156  		if err != nil {
   157  			errorChannel <- err
   158  			break
   159  		} else if record == nil {
   160  			break
   161  		}
   162  
   163  		response = record.(*ListInteractionChannelResponse)
   164  	}
   165  
   166  	close(recordChannel)
   167  	close(errorChannel)
   168  }
   169  
   170  func (c *ApiService) getNextListInteractionChannelResponse(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 := &ListInteractionChannelResponse{}
   182  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   183  		return nil, err
   184  	}
   185  	return ps, nil
   186  }
   187  
   188  // Optional parameters for the method 'UpdateInteractionChannel'
   189  type UpdateInteractionChannelParams struct {
   190  	//
   191  	Status *string `json:"Status,omitempty"`
   192  	// It changes the state of associated tasks. Routing status is required, When the channel status is set to `inactive`. Allowed Value for routing status is `closed`. Otherwise Optional, if not specified, all tasks will be set to `wrapping`.
   193  	Routing *interface{} `json:"Routing,omitempty"`
   194  }
   195  
   196  func (params *UpdateInteractionChannelParams) SetStatus(Status string) *UpdateInteractionChannelParams {
   197  	params.Status = &Status
   198  	return params
   199  }
   200  func (params *UpdateInteractionChannelParams) SetRouting(Routing interface{}) *UpdateInteractionChannelParams {
   201  	params.Routing = &Routing
   202  	return params
   203  }
   204  
   205  // Update an existing Interaction Channel.
   206  func (c *ApiService) UpdateInteractionChannel(InteractionSid string, Sid string, params *UpdateInteractionChannelParams) (*FlexV1InteractionChannel, error) {
   207  	path := "/v1/Interactions/{InteractionSid}/Channels/{Sid}"
   208  	path = strings.Replace(path, "{"+"InteractionSid"+"}", InteractionSid, -1)
   209  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   210  
   211  	data := url.Values{}
   212  	headers := make(map[string]interface{})
   213  
   214  	if params != nil && params.Status != nil {
   215  		data.Set("Status", *params.Status)
   216  	}
   217  	if params != nil && params.Routing != nil {
   218  		v, err := json.Marshal(params.Routing)
   219  
   220  		if err != nil {
   221  			return nil, err
   222  		}
   223  
   224  		data.Set("Routing", string(v))
   225  	}
   226  
   227  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   228  	if err != nil {
   229  		return nil, err
   230  	}
   231  
   232  	defer resp.Body.Close()
   233  
   234  	ps := &FlexV1InteractionChannel{}
   235  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   236  		return nil, err
   237  	}
   238  
   239  	return ps, err
   240  }