github.com/twilio/twilio-go@v1.20.1/rest/studio/v1/flows_executions_steps.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  // Retrieve a Step.
    27  func (c *ApiService) FetchExecutionStep(FlowSid string, ExecutionSid string, Sid string) (*StudioV1ExecutionStep, error) {
    28  	path := "/v1/Flows/{FlowSid}/Executions/{ExecutionSid}/Steps/{Sid}"
    29  	path = strings.Replace(path, "{"+"FlowSid"+"}", FlowSid, -1)
    30  	path = strings.Replace(path, "{"+"ExecutionSid"+"}", ExecutionSid, -1)
    31  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    32  
    33  	data := url.Values{}
    34  	headers := make(map[string]interface{})
    35  
    36  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	defer resp.Body.Close()
    42  
    43  	ps := &StudioV1ExecutionStep{}
    44  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	return ps, err
    49  }
    50  
    51  // Optional parameters for the method 'ListExecutionStep'
    52  type ListExecutionStepParams struct {
    53  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    54  	PageSize *int `json:"PageSize,omitempty"`
    55  	// Max number of records to return.
    56  	Limit *int `json:"limit,omitempty"`
    57  }
    58  
    59  func (params *ListExecutionStepParams) SetPageSize(PageSize int) *ListExecutionStepParams {
    60  	params.PageSize = &PageSize
    61  	return params
    62  }
    63  func (params *ListExecutionStepParams) SetLimit(Limit int) *ListExecutionStepParams {
    64  	params.Limit = &Limit
    65  	return params
    66  }
    67  
    68  // Retrieve a single page of ExecutionStep records from the API. Request is executed immediately.
    69  func (c *ApiService) PageExecutionStep(FlowSid string, ExecutionSid string, params *ListExecutionStepParams, pageToken, pageNumber string) (*ListExecutionStepResponse, error) {
    70  	path := "/v1/Flows/{FlowSid}/Executions/{ExecutionSid}/Steps"
    71  
    72  	path = strings.Replace(path, "{"+"FlowSid"+"}", FlowSid, -1)
    73  	path = strings.Replace(path, "{"+"ExecutionSid"+"}", ExecutionSid, -1)
    74  
    75  	data := url.Values{}
    76  	headers := make(map[string]interface{})
    77  
    78  	if params != nil && params.PageSize != nil {
    79  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
    80  	}
    81  
    82  	if pageToken != "" {
    83  		data.Set("PageToken", pageToken)
    84  	}
    85  	if pageNumber != "" {
    86  		data.Set("Page", pageNumber)
    87  	}
    88  
    89  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  
    94  	defer resp.Body.Close()
    95  
    96  	ps := &ListExecutionStepResponse{}
    97  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    98  		return nil, err
    99  	}
   100  
   101  	return ps, err
   102  }
   103  
   104  // Lists ExecutionStep records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   105  func (c *ApiService) ListExecutionStep(FlowSid string, ExecutionSid string, params *ListExecutionStepParams) ([]StudioV1ExecutionStep, error) {
   106  	response, errors := c.StreamExecutionStep(FlowSid, ExecutionSid, params)
   107  
   108  	records := make([]StudioV1ExecutionStep, 0)
   109  	for record := range response {
   110  		records = append(records, record)
   111  	}
   112  
   113  	if err := <-errors; err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	return records, nil
   118  }
   119  
   120  // Streams ExecutionStep records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   121  func (c *ApiService) StreamExecutionStep(FlowSid string, ExecutionSid string, params *ListExecutionStepParams) (chan StudioV1ExecutionStep, chan error) {
   122  	if params == nil {
   123  		params = &ListExecutionStepParams{}
   124  	}
   125  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   126  
   127  	recordChannel := make(chan StudioV1ExecutionStep, 1)
   128  	errorChannel := make(chan error, 1)
   129  
   130  	response, err := c.PageExecutionStep(FlowSid, ExecutionSid, params, "", "")
   131  	if err != nil {
   132  		errorChannel <- err
   133  		close(recordChannel)
   134  		close(errorChannel)
   135  	} else {
   136  		go c.streamExecutionStep(response, params, recordChannel, errorChannel)
   137  	}
   138  
   139  	return recordChannel, errorChannel
   140  }
   141  
   142  func (c *ApiService) streamExecutionStep(response *ListExecutionStepResponse, params *ListExecutionStepParams, recordChannel chan StudioV1ExecutionStep, errorChannel chan error) {
   143  	curRecord := 1
   144  
   145  	for response != nil {
   146  		responseRecords := response.Steps
   147  		for item := range responseRecords {
   148  			recordChannel <- responseRecords[item]
   149  			curRecord += 1
   150  			if params.Limit != nil && *params.Limit < curRecord {
   151  				close(recordChannel)
   152  				close(errorChannel)
   153  				return
   154  			}
   155  		}
   156  
   157  		record, err := client.GetNext(c.baseURL, response, c.getNextListExecutionStepResponse)
   158  		if err != nil {
   159  			errorChannel <- err
   160  			break
   161  		} else if record == nil {
   162  			break
   163  		}
   164  
   165  		response = record.(*ListExecutionStepResponse)
   166  	}
   167  
   168  	close(recordChannel)
   169  	close(errorChannel)
   170  }
   171  
   172  func (c *ApiService) getNextListExecutionStepResponse(nextPageUrl string) (interface{}, error) {
   173  	if nextPageUrl == "" {
   174  		return nil, nil
   175  	}
   176  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   177  	if err != nil {
   178  		return nil, err
   179  	}
   180  
   181  	defer resp.Body.Close()
   182  
   183  	ps := &ListExecutionStepResponse{}
   184  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   185  		return nil, err
   186  	}
   187  	return ps, nil
   188  }