github.com/twilio/twilio-go@v1.20.1/rest/studio/v1/flows_engagements.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Studio
     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 'CreateEngagement'
    27  type CreateEngagementParams struct {
    28  	// The Contact phone number to start a Studio Flow Engagement, available as variable `{{contact.channel.address}}`.
    29  	To *string `json:"To,omitempty"`
    30  	// The Twilio phone number to send messages or initiate calls from during the Flow Engagement. Available as variable `{{flow.channel.address}}`
    31  	From *string `json:"From,omitempty"`
    32  	// A JSON string we will add to your flow's context and that you can access as variables inside your flow. For example, if you pass in `Parameters={'name':'Zeke'}` then inside a widget you can reference the variable `{{flow.data.name}}` which will return the string 'Zeke'. Note: the JSON value must explicitly be passed as a string, not as a hash object. Depending on your particular HTTP library, you may need to add quotes or URL encode your JSON string.
    33  	Parameters *interface{} `json:"Parameters,omitempty"`
    34  }
    35  
    36  func (params *CreateEngagementParams) SetTo(To string) *CreateEngagementParams {
    37  	params.To = &To
    38  	return params
    39  }
    40  func (params *CreateEngagementParams) SetFrom(From string) *CreateEngagementParams {
    41  	params.From = &From
    42  	return params
    43  }
    44  func (params *CreateEngagementParams) SetParameters(Parameters interface{}) *CreateEngagementParams {
    45  	params.Parameters = &Parameters
    46  	return params
    47  }
    48  
    49  // Triggers a new Engagement for the Flow
    50  func (c *ApiService) CreateEngagement(FlowSid string, params *CreateEngagementParams) (*StudioV1Engagement, error) {
    51  	path := "/v1/Flows/{FlowSid}/Engagements"
    52  	path = strings.Replace(path, "{"+"FlowSid"+"}", FlowSid, -1)
    53  
    54  	data := url.Values{}
    55  	headers := make(map[string]interface{})
    56  
    57  	if params != nil && params.To != nil {
    58  		data.Set("To", *params.To)
    59  	}
    60  	if params != nil && params.From != nil {
    61  		data.Set("From", *params.From)
    62  	}
    63  	if params != nil && params.Parameters != nil {
    64  		v, err := json.Marshal(params.Parameters)
    65  
    66  		if err != nil {
    67  			return nil, err
    68  		}
    69  
    70  		data.Set("Parameters", string(v))
    71  	}
    72  
    73  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	defer resp.Body.Close()
    79  
    80  	ps := &StudioV1Engagement{}
    81  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	return ps, err
    86  }
    87  
    88  // Delete this Engagement and all Steps relating to it.
    89  func (c *ApiService) DeleteEngagement(FlowSid string, Sid string) error {
    90  	path := "/v1/Flows/{FlowSid}/Engagements/{Sid}"
    91  	path = strings.Replace(path, "{"+"FlowSid"+"}", FlowSid, -1)
    92  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    93  
    94  	data := url.Values{}
    95  	headers := make(map[string]interface{})
    96  
    97  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	defer resp.Body.Close()
   103  
   104  	return nil
   105  }
   106  
   107  // Retrieve an Engagement
   108  func (c *ApiService) FetchEngagement(FlowSid string, Sid string) (*StudioV1Engagement, error) {
   109  	path := "/v1/Flows/{FlowSid}/Engagements/{Sid}"
   110  	path = strings.Replace(path, "{"+"FlowSid"+"}", FlowSid, -1)
   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.Get(c.baseURL+path, data, headers)
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  
   121  	defer resp.Body.Close()
   122  
   123  	ps := &StudioV1Engagement{}
   124  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   125  		return nil, err
   126  	}
   127  
   128  	return ps, err
   129  }
   130  
   131  // Optional parameters for the method 'ListEngagement'
   132  type ListEngagementParams struct {
   133  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   134  	PageSize *int `json:"PageSize,omitempty"`
   135  	// Max number of records to return.
   136  	Limit *int `json:"limit,omitempty"`
   137  }
   138  
   139  func (params *ListEngagementParams) SetPageSize(PageSize int) *ListEngagementParams {
   140  	params.PageSize = &PageSize
   141  	return params
   142  }
   143  func (params *ListEngagementParams) SetLimit(Limit int) *ListEngagementParams {
   144  	params.Limit = &Limit
   145  	return params
   146  }
   147  
   148  // Retrieve a single page of Engagement records from the API. Request is executed immediately.
   149  func (c *ApiService) PageEngagement(FlowSid string, params *ListEngagementParams, pageToken, pageNumber string) (*ListEngagementResponse, error) {
   150  	path := "/v1/Flows/{FlowSid}/Engagements"
   151  
   152  	path = strings.Replace(path, "{"+"FlowSid"+"}", FlowSid, -1)
   153  
   154  	data := url.Values{}
   155  	headers := make(map[string]interface{})
   156  
   157  	if params != nil && params.PageSize != nil {
   158  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   159  	}
   160  
   161  	if pageToken != "" {
   162  		data.Set("PageToken", pageToken)
   163  	}
   164  	if pageNumber != "" {
   165  		data.Set("Page", pageNumber)
   166  	}
   167  
   168  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   169  	if err != nil {
   170  		return nil, err
   171  	}
   172  
   173  	defer resp.Body.Close()
   174  
   175  	ps := &ListEngagementResponse{}
   176  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   177  		return nil, err
   178  	}
   179  
   180  	return ps, err
   181  }
   182  
   183  // Lists Engagement records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   184  func (c *ApiService) ListEngagement(FlowSid string, params *ListEngagementParams) ([]StudioV1Engagement, error) {
   185  	response, errors := c.StreamEngagement(FlowSid, params)
   186  
   187  	records := make([]StudioV1Engagement, 0)
   188  	for record := range response {
   189  		records = append(records, record)
   190  	}
   191  
   192  	if err := <-errors; err != nil {
   193  		return nil, err
   194  	}
   195  
   196  	return records, nil
   197  }
   198  
   199  // Streams Engagement records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   200  func (c *ApiService) StreamEngagement(FlowSid string, params *ListEngagementParams) (chan StudioV1Engagement, chan error) {
   201  	if params == nil {
   202  		params = &ListEngagementParams{}
   203  	}
   204  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   205  
   206  	recordChannel := make(chan StudioV1Engagement, 1)
   207  	errorChannel := make(chan error, 1)
   208  
   209  	response, err := c.PageEngagement(FlowSid, params, "", "")
   210  	if err != nil {
   211  		errorChannel <- err
   212  		close(recordChannel)
   213  		close(errorChannel)
   214  	} else {
   215  		go c.streamEngagement(response, params, recordChannel, errorChannel)
   216  	}
   217  
   218  	return recordChannel, errorChannel
   219  }
   220  
   221  func (c *ApiService) streamEngagement(response *ListEngagementResponse, params *ListEngagementParams, recordChannel chan StudioV1Engagement, errorChannel chan error) {
   222  	curRecord := 1
   223  
   224  	for response != nil {
   225  		responseRecords := response.Engagements
   226  		for item := range responseRecords {
   227  			recordChannel <- responseRecords[item]
   228  			curRecord += 1
   229  			if params.Limit != nil && *params.Limit < curRecord {
   230  				close(recordChannel)
   231  				close(errorChannel)
   232  				return
   233  			}
   234  		}
   235  
   236  		record, err := client.GetNext(c.baseURL, response, c.getNextListEngagementResponse)
   237  		if err != nil {
   238  			errorChannel <- err
   239  			break
   240  		} else if record == nil {
   241  			break
   242  		}
   243  
   244  		response = record.(*ListEngagementResponse)
   245  	}
   246  
   247  	close(recordChannel)
   248  	close(errorChannel)
   249  }
   250  
   251  func (c *ApiService) getNextListEngagementResponse(nextPageUrl string) (interface{}, error) {
   252  	if nextPageUrl == "" {
   253  		return nil, nil
   254  	}
   255  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   256  	if err != nil {
   257  		return nil, err
   258  	}
   259  
   260  	defer resp.Body.Close()
   261  
   262  	ps := &ListEngagementResponse{}
   263  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   264  		return nil, err
   265  	}
   266  	return ps, nil
   267  }