github.com/twilio/twilio-go@v1.20.1/rest/taskrouter/v1/workspaces_workflows.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Taskrouter
     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 'CreateWorkflow'
    27  type CreateWorkflowParams struct {
    28  	// A descriptive string that you create to describe the Workflow resource. For example, `Inbound Call Workflow` or `2014 Outbound Campaign`.
    29  	FriendlyName *string `json:"FriendlyName,omitempty"`
    30  	// A JSON string that contains the rules to apply to the Workflow. See [Configuring Workflows](https://www.twilio.com/docs/taskrouter/workflow-configuration) for more information.
    31  	Configuration *string `json:"Configuration,omitempty"`
    32  	// The URL from your application that will process task assignment events. See [Handling Task Assignment Callback](https://www.twilio.com/docs/taskrouter/handle-assignment-callbacks) for more details.
    33  	AssignmentCallbackUrl *string `json:"AssignmentCallbackUrl,omitempty"`
    34  	// The URL that we should call when a call to the `assignment_callback_url` fails.
    35  	FallbackAssignmentCallbackUrl *string `json:"FallbackAssignmentCallbackUrl,omitempty"`
    36  	// How long TaskRouter will wait for a confirmation response from your application after it assigns a Task to a Worker. Can be up to `86,400` (24 hours) and the default is `120`.
    37  	TaskReservationTimeout *int `json:"TaskReservationTimeout,omitempty"`
    38  }
    39  
    40  func (params *CreateWorkflowParams) SetFriendlyName(FriendlyName string) *CreateWorkflowParams {
    41  	params.FriendlyName = &FriendlyName
    42  	return params
    43  }
    44  func (params *CreateWorkflowParams) SetConfiguration(Configuration string) *CreateWorkflowParams {
    45  	params.Configuration = &Configuration
    46  	return params
    47  }
    48  func (params *CreateWorkflowParams) SetAssignmentCallbackUrl(AssignmentCallbackUrl string) *CreateWorkflowParams {
    49  	params.AssignmentCallbackUrl = &AssignmentCallbackUrl
    50  	return params
    51  }
    52  func (params *CreateWorkflowParams) SetFallbackAssignmentCallbackUrl(FallbackAssignmentCallbackUrl string) *CreateWorkflowParams {
    53  	params.FallbackAssignmentCallbackUrl = &FallbackAssignmentCallbackUrl
    54  	return params
    55  }
    56  func (params *CreateWorkflowParams) SetTaskReservationTimeout(TaskReservationTimeout int) *CreateWorkflowParams {
    57  	params.TaskReservationTimeout = &TaskReservationTimeout
    58  	return params
    59  }
    60  
    61  //
    62  func (c *ApiService) CreateWorkflow(WorkspaceSid string, params *CreateWorkflowParams) (*TaskrouterV1Workflow, error) {
    63  	path := "/v1/Workspaces/{WorkspaceSid}/Workflows"
    64  	path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1)
    65  
    66  	data := url.Values{}
    67  	headers := make(map[string]interface{})
    68  
    69  	if params != nil && params.FriendlyName != nil {
    70  		data.Set("FriendlyName", *params.FriendlyName)
    71  	}
    72  	if params != nil && params.Configuration != nil {
    73  		data.Set("Configuration", *params.Configuration)
    74  	}
    75  	if params != nil && params.AssignmentCallbackUrl != nil {
    76  		data.Set("AssignmentCallbackUrl", *params.AssignmentCallbackUrl)
    77  	}
    78  	if params != nil && params.FallbackAssignmentCallbackUrl != nil {
    79  		data.Set("FallbackAssignmentCallbackUrl", *params.FallbackAssignmentCallbackUrl)
    80  	}
    81  	if params != nil && params.TaskReservationTimeout != nil {
    82  		data.Set("TaskReservationTimeout", fmt.Sprint(*params.TaskReservationTimeout))
    83  	}
    84  
    85  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  
    90  	defer resp.Body.Close()
    91  
    92  	ps := &TaskrouterV1Workflow{}
    93  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    94  		return nil, err
    95  	}
    96  
    97  	return ps, err
    98  }
    99  
   100  //
   101  func (c *ApiService) DeleteWorkflow(WorkspaceSid string, Sid string) error {
   102  	path := "/v1/Workspaces/{WorkspaceSid}/Workflows/{Sid}"
   103  	path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1)
   104  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   105  
   106  	data := url.Values{}
   107  	headers := make(map[string]interface{})
   108  
   109  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   110  	if err != nil {
   111  		return err
   112  	}
   113  
   114  	defer resp.Body.Close()
   115  
   116  	return nil
   117  }
   118  
   119  //
   120  func (c *ApiService) FetchWorkflow(WorkspaceSid string, Sid string) (*TaskrouterV1Workflow, error) {
   121  	path := "/v1/Workspaces/{WorkspaceSid}/Workflows/{Sid}"
   122  	path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1)
   123  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   124  
   125  	data := url.Values{}
   126  	headers := make(map[string]interface{})
   127  
   128  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  
   133  	defer resp.Body.Close()
   134  
   135  	ps := &TaskrouterV1Workflow{}
   136  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   137  		return nil, err
   138  	}
   139  
   140  	return ps, err
   141  }
   142  
   143  // Optional parameters for the method 'ListWorkflow'
   144  type ListWorkflowParams struct {
   145  	// The `friendly_name` of the Workflow resources to read.
   146  	FriendlyName *string `json:"FriendlyName,omitempty"`
   147  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   148  	PageSize *int `json:"PageSize,omitempty"`
   149  	// Max number of records to return.
   150  	Limit *int `json:"limit,omitempty"`
   151  }
   152  
   153  func (params *ListWorkflowParams) SetFriendlyName(FriendlyName string) *ListWorkflowParams {
   154  	params.FriendlyName = &FriendlyName
   155  	return params
   156  }
   157  func (params *ListWorkflowParams) SetPageSize(PageSize int) *ListWorkflowParams {
   158  	params.PageSize = &PageSize
   159  	return params
   160  }
   161  func (params *ListWorkflowParams) SetLimit(Limit int) *ListWorkflowParams {
   162  	params.Limit = &Limit
   163  	return params
   164  }
   165  
   166  // Retrieve a single page of Workflow records from the API. Request is executed immediately.
   167  func (c *ApiService) PageWorkflow(WorkspaceSid string, params *ListWorkflowParams, pageToken, pageNumber string) (*ListWorkflowResponse, error) {
   168  	path := "/v1/Workspaces/{WorkspaceSid}/Workflows"
   169  
   170  	path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1)
   171  
   172  	data := url.Values{}
   173  	headers := make(map[string]interface{})
   174  
   175  	if params != nil && params.FriendlyName != nil {
   176  		data.Set("FriendlyName", *params.FriendlyName)
   177  	}
   178  	if params != nil && params.PageSize != nil {
   179  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   180  	}
   181  
   182  	if pageToken != "" {
   183  		data.Set("PageToken", pageToken)
   184  	}
   185  	if pageNumber != "" {
   186  		data.Set("Page", pageNumber)
   187  	}
   188  
   189  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   190  	if err != nil {
   191  		return nil, err
   192  	}
   193  
   194  	defer resp.Body.Close()
   195  
   196  	ps := &ListWorkflowResponse{}
   197  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   198  		return nil, err
   199  	}
   200  
   201  	return ps, err
   202  }
   203  
   204  // Lists Workflow records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   205  func (c *ApiService) ListWorkflow(WorkspaceSid string, params *ListWorkflowParams) ([]TaskrouterV1Workflow, error) {
   206  	response, errors := c.StreamWorkflow(WorkspaceSid, params)
   207  
   208  	records := make([]TaskrouterV1Workflow, 0)
   209  	for record := range response {
   210  		records = append(records, record)
   211  	}
   212  
   213  	if err := <-errors; err != nil {
   214  		return nil, err
   215  	}
   216  
   217  	return records, nil
   218  }
   219  
   220  // Streams Workflow records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   221  func (c *ApiService) StreamWorkflow(WorkspaceSid string, params *ListWorkflowParams) (chan TaskrouterV1Workflow, chan error) {
   222  	if params == nil {
   223  		params = &ListWorkflowParams{}
   224  	}
   225  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   226  
   227  	recordChannel := make(chan TaskrouterV1Workflow, 1)
   228  	errorChannel := make(chan error, 1)
   229  
   230  	response, err := c.PageWorkflow(WorkspaceSid, params, "", "")
   231  	if err != nil {
   232  		errorChannel <- err
   233  		close(recordChannel)
   234  		close(errorChannel)
   235  	} else {
   236  		go c.streamWorkflow(response, params, recordChannel, errorChannel)
   237  	}
   238  
   239  	return recordChannel, errorChannel
   240  }
   241  
   242  func (c *ApiService) streamWorkflow(response *ListWorkflowResponse, params *ListWorkflowParams, recordChannel chan TaskrouterV1Workflow, errorChannel chan error) {
   243  	curRecord := 1
   244  
   245  	for response != nil {
   246  		responseRecords := response.Workflows
   247  		for item := range responseRecords {
   248  			recordChannel <- responseRecords[item]
   249  			curRecord += 1
   250  			if params.Limit != nil && *params.Limit < curRecord {
   251  				close(recordChannel)
   252  				close(errorChannel)
   253  				return
   254  			}
   255  		}
   256  
   257  		record, err := client.GetNext(c.baseURL, response, c.getNextListWorkflowResponse)
   258  		if err != nil {
   259  			errorChannel <- err
   260  			break
   261  		} else if record == nil {
   262  			break
   263  		}
   264  
   265  		response = record.(*ListWorkflowResponse)
   266  	}
   267  
   268  	close(recordChannel)
   269  	close(errorChannel)
   270  }
   271  
   272  func (c *ApiService) getNextListWorkflowResponse(nextPageUrl string) (interface{}, error) {
   273  	if nextPageUrl == "" {
   274  		return nil, nil
   275  	}
   276  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   277  	if err != nil {
   278  		return nil, err
   279  	}
   280  
   281  	defer resp.Body.Close()
   282  
   283  	ps := &ListWorkflowResponse{}
   284  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   285  		return nil, err
   286  	}
   287  	return ps, nil
   288  }
   289  
   290  // Optional parameters for the method 'UpdateWorkflow'
   291  type UpdateWorkflowParams struct {
   292  	// A descriptive string that you create to describe the Workflow resource. For example, `Inbound Call Workflow` or `2014 Outbound Campaign`.
   293  	FriendlyName *string `json:"FriendlyName,omitempty"`
   294  	// The URL from your application that will process task assignment events. See [Handling Task Assignment Callback](https://www.twilio.com/docs/taskrouter/handle-assignment-callbacks) for more details.
   295  	AssignmentCallbackUrl *string `json:"AssignmentCallbackUrl,omitempty"`
   296  	// The URL that we should call when a call to the `assignment_callback_url` fails.
   297  	FallbackAssignmentCallbackUrl *string `json:"FallbackAssignmentCallbackUrl,omitempty"`
   298  	// A JSON string that contains the rules to apply to the Workflow. See [Configuring Workflows](https://www.twilio.com/docs/taskrouter/workflow-configuration) for more information.
   299  	Configuration *string `json:"Configuration,omitempty"`
   300  	// How long TaskRouter will wait for a confirmation response from your application after it assigns a Task to a Worker. Can be up to `86,400` (24 hours) and the default is `120`.
   301  	TaskReservationTimeout *int `json:"TaskReservationTimeout,omitempty"`
   302  	// Whether or not to re-evaluate Tasks. The default is `false`, which means Tasks in the Workflow will not be processed through the assignment loop again.
   303  	ReEvaluateTasks *string `json:"ReEvaluateTasks,omitempty"`
   304  }
   305  
   306  func (params *UpdateWorkflowParams) SetFriendlyName(FriendlyName string) *UpdateWorkflowParams {
   307  	params.FriendlyName = &FriendlyName
   308  	return params
   309  }
   310  func (params *UpdateWorkflowParams) SetAssignmentCallbackUrl(AssignmentCallbackUrl string) *UpdateWorkflowParams {
   311  	params.AssignmentCallbackUrl = &AssignmentCallbackUrl
   312  	return params
   313  }
   314  func (params *UpdateWorkflowParams) SetFallbackAssignmentCallbackUrl(FallbackAssignmentCallbackUrl string) *UpdateWorkflowParams {
   315  	params.FallbackAssignmentCallbackUrl = &FallbackAssignmentCallbackUrl
   316  	return params
   317  }
   318  func (params *UpdateWorkflowParams) SetConfiguration(Configuration string) *UpdateWorkflowParams {
   319  	params.Configuration = &Configuration
   320  	return params
   321  }
   322  func (params *UpdateWorkflowParams) SetTaskReservationTimeout(TaskReservationTimeout int) *UpdateWorkflowParams {
   323  	params.TaskReservationTimeout = &TaskReservationTimeout
   324  	return params
   325  }
   326  func (params *UpdateWorkflowParams) SetReEvaluateTasks(ReEvaluateTasks string) *UpdateWorkflowParams {
   327  	params.ReEvaluateTasks = &ReEvaluateTasks
   328  	return params
   329  }
   330  
   331  //
   332  func (c *ApiService) UpdateWorkflow(WorkspaceSid string, Sid string, params *UpdateWorkflowParams) (*TaskrouterV1Workflow, error) {
   333  	path := "/v1/Workspaces/{WorkspaceSid}/Workflows/{Sid}"
   334  	path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1)
   335  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   336  
   337  	data := url.Values{}
   338  	headers := make(map[string]interface{})
   339  
   340  	if params != nil && params.FriendlyName != nil {
   341  		data.Set("FriendlyName", *params.FriendlyName)
   342  	}
   343  	if params != nil && params.AssignmentCallbackUrl != nil {
   344  		data.Set("AssignmentCallbackUrl", *params.AssignmentCallbackUrl)
   345  	}
   346  	if params != nil && params.FallbackAssignmentCallbackUrl != nil {
   347  		data.Set("FallbackAssignmentCallbackUrl", *params.FallbackAssignmentCallbackUrl)
   348  	}
   349  	if params != nil && params.Configuration != nil {
   350  		data.Set("Configuration", *params.Configuration)
   351  	}
   352  	if params != nil && params.TaskReservationTimeout != nil {
   353  		data.Set("TaskReservationTimeout", fmt.Sprint(*params.TaskReservationTimeout))
   354  	}
   355  	if params != nil && params.ReEvaluateTasks != nil {
   356  		data.Set("ReEvaluateTasks", *params.ReEvaluateTasks)
   357  	}
   358  
   359  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   360  	if err != nil {
   361  		return nil, err
   362  	}
   363  
   364  	defer resp.Body.Close()
   365  
   366  	ps := &TaskrouterV1Workflow{}
   367  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   368  		return nil, err
   369  	}
   370  
   371  	return ps, err
   372  }