github.com/twilio/twilio-go@v1.20.1/rest/taskrouter/v1/workspaces_tasks.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  	"time"
    23  
    24  	"github.com/twilio/twilio-go/client"
    25  )
    26  
    27  // Optional parameters for the method 'CreateTask'
    28  type CreateTaskParams struct {
    29  	// The amount of time in seconds the new task can live before being assigned. Can be up to a maximum of 2 weeks (1,209,600 seconds). The default value is 24 hours (86,400 seconds). On timeout, the `task.canceled` event will fire with description `Task TTL Exceeded`.
    30  	Timeout *int `json:"Timeout,omitempty"`
    31  	// The priority to assign the new task and override the default. When supplied, the new Task will have this priority unless it matches a Workflow Target with a Priority set. When not supplied, the new Task will have the priority of the matching Workflow Target. Value can be 0 to 2^31^ (2,147,483,647).
    32  	Priority *int `json:"Priority,omitempty"`
    33  	// When MultiTasking is enabled, specify the TaskChannel by passing either its `unique_name` or `sid`. Default value is `default`.
    34  	TaskChannel *string `json:"TaskChannel,omitempty"`
    35  	// The SID of the Workflow that you would like to handle routing for the new Task. If there is only one Workflow defined for the Workspace that you are posting the new task to, this parameter is optional.
    36  	WorkflowSid *string `json:"WorkflowSid,omitempty"`
    37  	// A URL-encoded JSON string with the attributes of the new task. This value is passed to the Workflow's `assignment_callback_url` when the Task is assigned to a Worker. For example: `{ \\\"task_type\\\": \\\"call\\\", \\\"twilio_call_sid\\\": \\\"CAxxx\\\", \\\"customer_ticket_number\\\": \\\"12345\\\" }`.
    38  	Attributes *string `json:"Attributes,omitempty"`
    39  	// The virtual start time to assign the new task and override the default. When supplied, the new task will have this virtual start time. When not supplied, the new task will have the virtual start time equal to `date_created`. Value can't be in the future.
    40  	VirtualStartTime *time.Time `json:"VirtualStartTime,omitempty"`
    41  }
    42  
    43  func (params *CreateTaskParams) SetTimeout(Timeout int) *CreateTaskParams {
    44  	params.Timeout = &Timeout
    45  	return params
    46  }
    47  func (params *CreateTaskParams) SetPriority(Priority int) *CreateTaskParams {
    48  	params.Priority = &Priority
    49  	return params
    50  }
    51  func (params *CreateTaskParams) SetTaskChannel(TaskChannel string) *CreateTaskParams {
    52  	params.TaskChannel = &TaskChannel
    53  	return params
    54  }
    55  func (params *CreateTaskParams) SetWorkflowSid(WorkflowSid string) *CreateTaskParams {
    56  	params.WorkflowSid = &WorkflowSid
    57  	return params
    58  }
    59  func (params *CreateTaskParams) SetAttributes(Attributes string) *CreateTaskParams {
    60  	params.Attributes = &Attributes
    61  	return params
    62  }
    63  func (params *CreateTaskParams) SetVirtualStartTime(VirtualStartTime time.Time) *CreateTaskParams {
    64  	params.VirtualStartTime = &VirtualStartTime
    65  	return params
    66  }
    67  
    68  //
    69  func (c *ApiService) CreateTask(WorkspaceSid string, params *CreateTaskParams) (*TaskrouterV1Task, error) {
    70  	path := "/v1/Workspaces/{WorkspaceSid}/Tasks"
    71  	path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1)
    72  
    73  	data := url.Values{}
    74  	headers := make(map[string]interface{})
    75  
    76  	if params != nil && params.Timeout != nil {
    77  		data.Set("Timeout", fmt.Sprint(*params.Timeout))
    78  	}
    79  	if params != nil && params.Priority != nil {
    80  		data.Set("Priority", fmt.Sprint(*params.Priority))
    81  	}
    82  	if params != nil && params.TaskChannel != nil {
    83  		data.Set("TaskChannel", *params.TaskChannel)
    84  	}
    85  	if params != nil && params.WorkflowSid != nil {
    86  		data.Set("WorkflowSid", *params.WorkflowSid)
    87  	}
    88  	if params != nil && params.Attributes != nil {
    89  		data.Set("Attributes", *params.Attributes)
    90  	}
    91  	if params != nil && params.VirtualStartTime != nil {
    92  		data.Set("VirtualStartTime", fmt.Sprint((*params.VirtualStartTime).Format(time.RFC3339)))
    93  	}
    94  
    95  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    96  	if err != nil {
    97  		return nil, err
    98  	}
    99  
   100  	defer resp.Body.Close()
   101  
   102  	ps := &TaskrouterV1Task{}
   103  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   104  		return nil, err
   105  	}
   106  
   107  	return ps, err
   108  }
   109  
   110  // Optional parameters for the method 'DeleteTask'
   111  type DeleteTaskParams struct {
   112  	// If provided, deletes this Task if (and only if) the [ETag](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag) header of the Task matches the provided value. This matches the semantics of (and is implemented with) the HTTP [If-Match header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Match).
   113  	IfMatch *string `json:"If-Match,omitempty"`
   114  }
   115  
   116  func (params *DeleteTaskParams) SetIfMatch(IfMatch string) *DeleteTaskParams {
   117  	params.IfMatch = &IfMatch
   118  	return params
   119  }
   120  
   121  //
   122  func (c *ApiService) DeleteTask(WorkspaceSid string, Sid string, params *DeleteTaskParams) error {
   123  	path := "/v1/Workspaces/{WorkspaceSid}/Tasks/{Sid}"
   124  	path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1)
   125  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   126  
   127  	data := url.Values{}
   128  	headers := make(map[string]interface{})
   129  
   130  	if params != nil && params.IfMatch != nil {
   131  		headers["If-Match"] = *params.IfMatch
   132  	}
   133  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   134  	if err != nil {
   135  		return err
   136  	}
   137  
   138  	defer resp.Body.Close()
   139  
   140  	return nil
   141  }
   142  
   143  //
   144  func (c *ApiService) FetchTask(WorkspaceSid string, Sid string) (*TaskrouterV1Task, error) {
   145  	path := "/v1/Workspaces/{WorkspaceSid}/Tasks/{Sid}"
   146  	path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1)
   147  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   148  
   149  	data := url.Values{}
   150  	headers := make(map[string]interface{})
   151  
   152  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   153  	if err != nil {
   154  		return nil, err
   155  	}
   156  
   157  	defer resp.Body.Close()
   158  
   159  	ps := &TaskrouterV1Task{}
   160  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   161  		return nil, err
   162  	}
   163  
   164  	return ps, err
   165  }
   166  
   167  // Optional parameters for the method 'ListTask'
   168  type ListTaskParams struct {
   169  	// The priority value of the Tasks to read. Returns the list of all Tasks in the Workspace with the specified priority.
   170  	Priority *int `json:"Priority,omitempty"`
   171  	// The `assignment_status` of the Tasks you want to read. Can be: `pending`, `reserved`, `assigned`, `canceled`, `wrapping`, or `completed`. Returns all Tasks in the Workspace with the specified `assignment_status`.
   172  	AssignmentStatus *[]string `json:"AssignmentStatus,omitempty"`
   173  	// The SID of the Workflow with the Tasks to read. Returns the Tasks controlled by the Workflow identified by this SID.
   174  	WorkflowSid *string `json:"WorkflowSid,omitempty"`
   175  	// The friendly name of the Workflow with the Tasks to read. Returns the Tasks controlled by the Workflow identified by this friendly name.
   176  	WorkflowName *string `json:"WorkflowName,omitempty"`
   177  	// The SID of the TaskQueue with the Tasks to read. Returns the Tasks waiting in the TaskQueue identified by this SID.
   178  	TaskQueueSid *string `json:"TaskQueueSid,omitempty"`
   179  	// The `friendly_name` of the TaskQueue with the Tasks to read. Returns the Tasks waiting in the TaskQueue identified by this friendly name.
   180  	TaskQueueName *string `json:"TaskQueueName,omitempty"`
   181  	// The attributes of the Tasks to read. Returns the Tasks that match the attributes specified in this parameter.
   182  	EvaluateTaskAttributes *string `json:"EvaluateTaskAttributes,omitempty"`
   183  	// How to order the returned Task resources. By default, Tasks are sorted by ascending DateCreated. This value is specified as: `Attribute:Order`, where `Attribute` can be either `DateCreated`, `Priority`, or `VirtualStartTime` and `Order` can be either `asc` or `desc`. For example, `Priority:desc` returns Tasks ordered in descending order of their Priority. Pairings of sort orders can be specified in a comma-separated list such as `Priority:desc,DateCreated:asc`, which returns the Tasks in descending Priority order and ascending DateCreated Order. The only ordering pairing not allowed is DateCreated and VirtualStartTime.
   184  	Ordering *string `json:"Ordering,omitempty"`
   185  	// Whether to read Tasks with Add-ons. If `true`, returns only Tasks with Add-ons. If `false`, returns only Tasks without Add-ons.
   186  	HasAddons *bool `json:"HasAddons,omitempty"`
   187  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   188  	PageSize *int `json:"PageSize,omitempty"`
   189  	// Max number of records to return.
   190  	Limit *int `json:"limit,omitempty"`
   191  }
   192  
   193  func (params *ListTaskParams) SetPriority(Priority int) *ListTaskParams {
   194  	params.Priority = &Priority
   195  	return params
   196  }
   197  func (params *ListTaskParams) SetAssignmentStatus(AssignmentStatus []string) *ListTaskParams {
   198  	params.AssignmentStatus = &AssignmentStatus
   199  	return params
   200  }
   201  func (params *ListTaskParams) SetWorkflowSid(WorkflowSid string) *ListTaskParams {
   202  	params.WorkflowSid = &WorkflowSid
   203  	return params
   204  }
   205  func (params *ListTaskParams) SetWorkflowName(WorkflowName string) *ListTaskParams {
   206  	params.WorkflowName = &WorkflowName
   207  	return params
   208  }
   209  func (params *ListTaskParams) SetTaskQueueSid(TaskQueueSid string) *ListTaskParams {
   210  	params.TaskQueueSid = &TaskQueueSid
   211  	return params
   212  }
   213  func (params *ListTaskParams) SetTaskQueueName(TaskQueueName string) *ListTaskParams {
   214  	params.TaskQueueName = &TaskQueueName
   215  	return params
   216  }
   217  func (params *ListTaskParams) SetEvaluateTaskAttributes(EvaluateTaskAttributes string) *ListTaskParams {
   218  	params.EvaluateTaskAttributes = &EvaluateTaskAttributes
   219  	return params
   220  }
   221  func (params *ListTaskParams) SetOrdering(Ordering string) *ListTaskParams {
   222  	params.Ordering = &Ordering
   223  	return params
   224  }
   225  func (params *ListTaskParams) SetHasAddons(HasAddons bool) *ListTaskParams {
   226  	params.HasAddons = &HasAddons
   227  	return params
   228  }
   229  func (params *ListTaskParams) SetPageSize(PageSize int) *ListTaskParams {
   230  	params.PageSize = &PageSize
   231  	return params
   232  }
   233  func (params *ListTaskParams) SetLimit(Limit int) *ListTaskParams {
   234  	params.Limit = &Limit
   235  	return params
   236  }
   237  
   238  // Retrieve a single page of Task records from the API. Request is executed immediately.
   239  func (c *ApiService) PageTask(WorkspaceSid string, params *ListTaskParams, pageToken, pageNumber string) (*ListTaskResponse, error) {
   240  	path := "/v1/Workspaces/{WorkspaceSid}/Tasks"
   241  
   242  	path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1)
   243  
   244  	data := url.Values{}
   245  	headers := make(map[string]interface{})
   246  
   247  	if params != nil && params.Priority != nil {
   248  		data.Set("Priority", fmt.Sprint(*params.Priority))
   249  	}
   250  	if params != nil && params.AssignmentStatus != nil {
   251  		for _, item := range *params.AssignmentStatus {
   252  			data.Add("AssignmentStatus", item)
   253  		}
   254  	}
   255  	if params != nil && params.WorkflowSid != nil {
   256  		data.Set("WorkflowSid", *params.WorkflowSid)
   257  	}
   258  	if params != nil && params.WorkflowName != nil {
   259  		data.Set("WorkflowName", *params.WorkflowName)
   260  	}
   261  	if params != nil && params.TaskQueueSid != nil {
   262  		data.Set("TaskQueueSid", *params.TaskQueueSid)
   263  	}
   264  	if params != nil && params.TaskQueueName != nil {
   265  		data.Set("TaskQueueName", *params.TaskQueueName)
   266  	}
   267  	if params != nil && params.EvaluateTaskAttributes != nil {
   268  		data.Set("EvaluateTaskAttributes", *params.EvaluateTaskAttributes)
   269  	}
   270  	if params != nil && params.Ordering != nil {
   271  		data.Set("Ordering", *params.Ordering)
   272  	}
   273  	if params != nil && params.HasAddons != nil {
   274  		data.Set("HasAddons", fmt.Sprint(*params.HasAddons))
   275  	}
   276  	if params != nil && params.PageSize != nil {
   277  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   278  	}
   279  
   280  	if pageToken != "" {
   281  		data.Set("PageToken", pageToken)
   282  	}
   283  	if pageNumber != "" {
   284  		data.Set("Page", pageNumber)
   285  	}
   286  
   287  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   288  	if err != nil {
   289  		return nil, err
   290  	}
   291  
   292  	defer resp.Body.Close()
   293  
   294  	ps := &ListTaskResponse{}
   295  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   296  		return nil, err
   297  	}
   298  
   299  	return ps, err
   300  }
   301  
   302  // Lists Task records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   303  func (c *ApiService) ListTask(WorkspaceSid string, params *ListTaskParams) ([]TaskrouterV1Task, error) {
   304  	response, errors := c.StreamTask(WorkspaceSid, params)
   305  
   306  	records := make([]TaskrouterV1Task, 0)
   307  	for record := range response {
   308  		records = append(records, record)
   309  	}
   310  
   311  	if err := <-errors; err != nil {
   312  		return nil, err
   313  	}
   314  
   315  	return records, nil
   316  }
   317  
   318  // Streams Task records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   319  func (c *ApiService) StreamTask(WorkspaceSid string, params *ListTaskParams) (chan TaskrouterV1Task, chan error) {
   320  	if params == nil {
   321  		params = &ListTaskParams{}
   322  	}
   323  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   324  
   325  	recordChannel := make(chan TaskrouterV1Task, 1)
   326  	errorChannel := make(chan error, 1)
   327  
   328  	response, err := c.PageTask(WorkspaceSid, params, "", "")
   329  	if err != nil {
   330  		errorChannel <- err
   331  		close(recordChannel)
   332  		close(errorChannel)
   333  	} else {
   334  		go c.streamTask(response, params, recordChannel, errorChannel)
   335  	}
   336  
   337  	return recordChannel, errorChannel
   338  }
   339  
   340  func (c *ApiService) streamTask(response *ListTaskResponse, params *ListTaskParams, recordChannel chan TaskrouterV1Task, errorChannel chan error) {
   341  	curRecord := 1
   342  
   343  	for response != nil {
   344  		responseRecords := response.Tasks
   345  		for item := range responseRecords {
   346  			recordChannel <- responseRecords[item]
   347  			curRecord += 1
   348  			if params.Limit != nil && *params.Limit < curRecord {
   349  				close(recordChannel)
   350  				close(errorChannel)
   351  				return
   352  			}
   353  		}
   354  
   355  		record, err := client.GetNext(c.baseURL, response, c.getNextListTaskResponse)
   356  		if err != nil {
   357  			errorChannel <- err
   358  			break
   359  		} else if record == nil {
   360  			break
   361  		}
   362  
   363  		response = record.(*ListTaskResponse)
   364  	}
   365  
   366  	close(recordChannel)
   367  	close(errorChannel)
   368  }
   369  
   370  func (c *ApiService) getNextListTaskResponse(nextPageUrl string) (interface{}, error) {
   371  	if nextPageUrl == "" {
   372  		return nil, nil
   373  	}
   374  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   375  	if err != nil {
   376  		return nil, err
   377  	}
   378  
   379  	defer resp.Body.Close()
   380  
   381  	ps := &ListTaskResponse{}
   382  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   383  		return nil, err
   384  	}
   385  	return ps, nil
   386  }
   387  
   388  // Optional parameters for the method 'UpdateTask'
   389  type UpdateTaskParams struct {
   390  	// If provided, applies this mutation if (and only if) the [ETag](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag) header of the Task matches the provided value. This matches the semantics of (and is implemented with) the HTTP [If-Match header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Match).
   391  	IfMatch *string `json:"If-Match,omitempty"`
   392  	// The JSON string that describes the custom attributes of the task.
   393  	Attributes *string `json:"Attributes,omitempty"`
   394  	//
   395  	AssignmentStatus *string `json:"AssignmentStatus,omitempty"`
   396  	// The reason that the Task was canceled or completed. This parameter is required only if the Task is canceled or completed. Setting this value queues the task for deletion and logs the reason.
   397  	Reason *string `json:"Reason,omitempty"`
   398  	// The Task's new priority value. When supplied, the Task takes on the specified priority unless it matches a Workflow Target with a Priority set. Value can be 0 to 2^31^ (2,147,483,647).
   399  	Priority *int `json:"Priority,omitempty"`
   400  	// When MultiTasking is enabled, specify the TaskChannel with the task to update. Can be the TaskChannel's SID or its `unique_name`, such as `voice`, `sms`, or `default`.
   401  	TaskChannel *string `json:"TaskChannel,omitempty"`
   402  	// The task's new virtual start time value. When supplied, the Task takes on the specified virtual start time. Value can't be in the future.
   403  	VirtualStartTime *time.Time `json:"VirtualStartTime,omitempty"`
   404  }
   405  
   406  func (params *UpdateTaskParams) SetIfMatch(IfMatch string) *UpdateTaskParams {
   407  	params.IfMatch = &IfMatch
   408  	return params
   409  }
   410  func (params *UpdateTaskParams) SetAttributes(Attributes string) *UpdateTaskParams {
   411  	params.Attributes = &Attributes
   412  	return params
   413  }
   414  func (params *UpdateTaskParams) SetAssignmentStatus(AssignmentStatus string) *UpdateTaskParams {
   415  	params.AssignmentStatus = &AssignmentStatus
   416  	return params
   417  }
   418  func (params *UpdateTaskParams) SetReason(Reason string) *UpdateTaskParams {
   419  	params.Reason = &Reason
   420  	return params
   421  }
   422  func (params *UpdateTaskParams) SetPriority(Priority int) *UpdateTaskParams {
   423  	params.Priority = &Priority
   424  	return params
   425  }
   426  func (params *UpdateTaskParams) SetTaskChannel(TaskChannel string) *UpdateTaskParams {
   427  	params.TaskChannel = &TaskChannel
   428  	return params
   429  }
   430  func (params *UpdateTaskParams) SetVirtualStartTime(VirtualStartTime time.Time) *UpdateTaskParams {
   431  	params.VirtualStartTime = &VirtualStartTime
   432  	return params
   433  }
   434  
   435  //
   436  func (c *ApiService) UpdateTask(WorkspaceSid string, Sid string, params *UpdateTaskParams) (*TaskrouterV1Task, error) {
   437  	path := "/v1/Workspaces/{WorkspaceSid}/Tasks/{Sid}"
   438  	path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1)
   439  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   440  
   441  	data := url.Values{}
   442  	headers := make(map[string]interface{})
   443  
   444  	if params != nil && params.Attributes != nil {
   445  		data.Set("Attributes", *params.Attributes)
   446  	}
   447  	if params != nil && params.AssignmentStatus != nil {
   448  		data.Set("AssignmentStatus", *params.AssignmentStatus)
   449  	}
   450  	if params != nil && params.Reason != nil {
   451  		data.Set("Reason", *params.Reason)
   452  	}
   453  	if params != nil && params.Priority != nil {
   454  		data.Set("Priority", fmt.Sprint(*params.Priority))
   455  	}
   456  	if params != nil && params.TaskChannel != nil {
   457  		data.Set("TaskChannel", *params.TaskChannel)
   458  	}
   459  	if params != nil && params.VirtualStartTime != nil {
   460  		data.Set("VirtualStartTime", fmt.Sprint((*params.VirtualStartTime).Format(time.RFC3339)))
   461  	}
   462  
   463  	if params != nil && params.IfMatch != nil {
   464  		headers["If-Match"] = *params.IfMatch
   465  	}
   466  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   467  	if err != nil {
   468  		return nil, err
   469  	}
   470  
   471  	defer resp.Body.Close()
   472  
   473  	ps := &TaskrouterV1Task{}
   474  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   475  		return nil, err
   476  	}
   477  
   478  	return ps, err
   479  }