github.com/twilio/twilio-go@v1.20.1/rest/flex/v1/web_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  // Optional parameters for the method 'CreateWebChannel'
    27  type CreateWebChannelParams struct {
    28  	// The SID of the Flex Flow.
    29  	FlexFlowSid *string `json:"FlexFlowSid,omitempty"`
    30  	// The chat identity.
    31  	Identity *string `json:"Identity,omitempty"`
    32  	// The chat participant's friendly name.
    33  	CustomerFriendlyName *string `json:"CustomerFriendlyName,omitempty"`
    34  	// The chat channel's friendly name.
    35  	ChatFriendlyName *string `json:"ChatFriendlyName,omitempty"`
    36  	// The chat channel's unique name.
    37  	ChatUniqueName *string `json:"ChatUniqueName,omitempty"`
    38  	// The pre-engagement data.
    39  	PreEngagementData *string `json:"PreEngagementData,omitempty"`
    40  }
    41  
    42  func (params *CreateWebChannelParams) SetFlexFlowSid(FlexFlowSid string) *CreateWebChannelParams {
    43  	params.FlexFlowSid = &FlexFlowSid
    44  	return params
    45  }
    46  func (params *CreateWebChannelParams) SetIdentity(Identity string) *CreateWebChannelParams {
    47  	params.Identity = &Identity
    48  	return params
    49  }
    50  func (params *CreateWebChannelParams) SetCustomerFriendlyName(CustomerFriendlyName string) *CreateWebChannelParams {
    51  	params.CustomerFriendlyName = &CustomerFriendlyName
    52  	return params
    53  }
    54  func (params *CreateWebChannelParams) SetChatFriendlyName(ChatFriendlyName string) *CreateWebChannelParams {
    55  	params.ChatFriendlyName = &ChatFriendlyName
    56  	return params
    57  }
    58  func (params *CreateWebChannelParams) SetChatUniqueName(ChatUniqueName string) *CreateWebChannelParams {
    59  	params.ChatUniqueName = &ChatUniqueName
    60  	return params
    61  }
    62  func (params *CreateWebChannelParams) SetPreEngagementData(PreEngagementData string) *CreateWebChannelParams {
    63  	params.PreEngagementData = &PreEngagementData
    64  	return params
    65  }
    66  
    67  //
    68  func (c *ApiService) CreateWebChannel(params *CreateWebChannelParams) (*FlexV1WebChannel, error) {
    69  	path := "/v1/WebChannels"
    70  
    71  	data := url.Values{}
    72  	headers := make(map[string]interface{})
    73  
    74  	if params != nil && params.FlexFlowSid != nil {
    75  		data.Set("FlexFlowSid", *params.FlexFlowSid)
    76  	}
    77  	if params != nil && params.Identity != nil {
    78  		data.Set("Identity", *params.Identity)
    79  	}
    80  	if params != nil && params.CustomerFriendlyName != nil {
    81  		data.Set("CustomerFriendlyName", *params.CustomerFriendlyName)
    82  	}
    83  	if params != nil && params.ChatFriendlyName != nil {
    84  		data.Set("ChatFriendlyName", *params.ChatFriendlyName)
    85  	}
    86  	if params != nil && params.ChatUniqueName != nil {
    87  		data.Set("ChatUniqueName", *params.ChatUniqueName)
    88  	}
    89  	if params != nil && params.PreEngagementData != nil {
    90  		data.Set("PreEngagementData", *params.PreEngagementData)
    91  	}
    92  
    93  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  
    98  	defer resp.Body.Close()
    99  
   100  	ps := &FlexV1WebChannel{}
   101  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   102  		return nil, err
   103  	}
   104  
   105  	return ps, err
   106  }
   107  
   108  //
   109  func (c *ApiService) DeleteWebChannel(Sid string) error {
   110  	path := "/v1/WebChannels/{Sid}"
   111  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   112  
   113  	data := url.Values{}
   114  	headers := make(map[string]interface{})
   115  
   116  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   117  	if err != nil {
   118  		return err
   119  	}
   120  
   121  	defer resp.Body.Close()
   122  
   123  	return nil
   124  }
   125  
   126  //
   127  func (c *ApiService) FetchWebChannel(Sid string) (*FlexV1WebChannel, error) {
   128  	path := "/v1/WebChannels/{Sid}"
   129  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   130  
   131  	data := url.Values{}
   132  	headers := make(map[string]interface{})
   133  
   134  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   135  	if err != nil {
   136  		return nil, err
   137  	}
   138  
   139  	defer resp.Body.Close()
   140  
   141  	ps := &FlexV1WebChannel{}
   142  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   143  		return nil, err
   144  	}
   145  
   146  	return ps, err
   147  }
   148  
   149  // Optional parameters for the method 'ListWebChannel'
   150  type ListWebChannelParams struct {
   151  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   152  	PageSize *int `json:"PageSize,omitempty"`
   153  	// Max number of records to return.
   154  	Limit *int `json:"limit,omitempty"`
   155  }
   156  
   157  func (params *ListWebChannelParams) SetPageSize(PageSize int) *ListWebChannelParams {
   158  	params.PageSize = &PageSize
   159  	return params
   160  }
   161  func (params *ListWebChannelParams) SetLimit(Limit int) *ListWebChannelParams {
   162  	params.Limit = &Limit
   163  	return params
   164  }
   165  
   166  // Retrieve a single page of WebChannel records from the API. Request is executed immediately.
   167  func (c *ApiService) PageWebChannel(params *ListWebChannelParams, pageToken, pageNumber string) (*ListWebChannelResponse, error) {
   168  	path := "/v1/WebChannels"
   169  
   170  	data := url.Values{}
   171  	headers := make(map[string]interface{})
   172  
   173  	if params != nil && params.PageSize != nil {
   174  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   175  	}
   176  
   177  	if pageToken != "" {
   178  		data.Set("PageToken", pageToken)
   179  	}
   180  	if pageNumber != "" {
   181  		data.Set("Page", pageNumber)
   182  	}
   183  
   184  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   185  	if err != nil {
   186  		return nil, err
   187  	}
   188  
   189  	defer resp.Body.Close()
   190  
   191  	ps := &ListWebChannelResponse{}
   192  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   193  		return nil, err
   194  	}
   195  
   196  	return ps, err
   197  }
   198  
   199  // Lists WebChannel records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   200  func (c *ApiService) ListWebChannel(params *ListWebChannelParams) ([]FlexV1WebChannel, error) {
   201  	response, errors := c.StreamWebChannel(params)
   202  
   203  	records := make([]FlexV1WebChannel, 0)
   204  	for record := range response {
   205  		records = append(records, record)
   206  	}
   207  
   208  	if err := <-errors; err != nil {
   209  		return nil, err
   210  	}
   211  
   212  	return records, nil
   213  }
   214  
   215  // Streams WebChannel records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   216  func (c *ApiService) StreamWebChannel(params *ListWebChannelParams) (chan FlexV1WebChannel, chan error) {
   217  	if params == nil {
   218  		params = &ListWebChannelParams{}
   219  	}
   220  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   221  
   222  	recordChannel := make(chan FlexV1WebChannel, 1)
   223  	errorChannel := make(chan error, 1)
   224  
   225  	response, err := c.PageWebChannel(params, "", "")
   226  	if err != nil {
   227  		errorChannel <- err
   228  		close(recordChannel)
   229  		close(errorChannel)
   230  	} else {
   231  		go c.streamWebChannel(response, params, recordChannel, errorChannel)
   232  	}
   233  
   234  	return recordChannel, errorChannel
   235  }
   236  
   237  func (c *ApiService) streamWebChannel(response *ListWebChannelResponse, params *ListWebChannelParams, recordChannel chan FlexV1WebChannel, errorChannel chan error) {
   238  	curRecord := 1
   239  
   240  	for response != nil {
   241  		responseRecords := response.FlexChatChannels
   242  		for item := range responseRecords {
   243  			recordChannel <- responseRecords[item]
   244  			curRecord += 1
   245  			if params.Limit != nil && *params.Limit < curRecord {
   246  				close(recordChannel)
   247  				close(errorChannel)
   248  				return
   249  			}
   250  		}
   251  
   252  		record, err := client.GetNext(c.baseURL, response, c.getNextListWebChannelResponse)
   253  		if err != nil {
   254  			errorChannel <- err
   255  			break
   256  		} else if record == nil {
   257  			break
   258  		}
   259  
   260  		response = record.(*ListWebChannelResponse)
   261  	}
   262  
   263  	close(recordChannel)
   264  	close(errorChannel)
   265  }
   266  
   267  func (c *ApiService) getNextListWebChannelResponse(nextPageUrl string) (interface{}, error) {
   268  	if nextPageUrl == "" {
   269  		return nil, nil
   270  	}
   271  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   272  	if err != nil {
   273  		return nil, err
   274  	}
   275  
   276  	defer resp.Body.Close()
   277  
   278  	ps := &ListWebChannelResponse{}
   279  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   280  		return nil, err
   281  	}
   282  	return ps, nil
   283  }
   284  
   285  // Optional parameters for the method 'UpdateWebChannel'
   286  type UpdateWebChannelParams struct {
   287  	//
   288  	ChatStatus *string `json:"ChatStatus,omitempty"`
   289  	// The post-engagement data.
   290  	PostEngagementData *string `json:"PostEngagementData,omitempty"`
   291  }
   292  
   293  func (params *UpdateWebChannelParams) SetChatStatus(ChatStatus string) *UpdateWebChannelParams {
   294  	params.ChatStatus = &ChatStatus
   295  	return params
   296  }
   297  func (params *UpdateWebChannelParams) SetPostEngagementData(PostEngagementData string) *UpdateWebChannelParams {
   298  	params.PostEngagementData = &PostEngagementData
   299  	return params
   300  }
   301  
   302  //
   303  func (c *ApiService) UpdateWebChannel(Sid string, params *UpdateWebChannelParams) (*FlexV1WebChannel, error) {
   304  	path := "/v1/WebChannels/{Sid}"
   305  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   306  
   307  	data := url.Values{}
   308  	headers := make(map[string]interface{})
   309  
   310  	if params != nil && params.ChatStatus != nil {
   311  		data.Set("ChatStatus", *params.ChatStatus)
   312  	}
   313  	if params != nil && params.PostEngagementData != nil {
   314  		data.Set("PostEngagementData", *params.PostEngagementData)
   315  	}
   316  
   317  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   318  	if err != nil {
   319  		return nil, err
   320  	}
   321  
   322  	defer resp.Body.Close()
   323  
   324  	ps := &FlexV1WebChannel{}
   325  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   326  		return nil, err
   327  	}
   328  
   329  	return ps, err
   330  }