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