github.com/twilio/twilio-go@v1.20.1/rest/taskrouter/v1/workspaces_events.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  //
    28  func (c *ApiService) FetchEvent(WorkspaceSid string, Sid string) (*TaskrouterV1Event, error) {
    29  	path := "/v1/Workspaces/{WorkspaceSid}/Events/{Sid}"
    30  	path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -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 := &TaskrouterV1Event{}
    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 'ListEvent'
    52  type ListEventParams struct {
    53  	// Only include Events that occurred on or before this date, specified in GMT as an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date-time.
    54  	EndDate *time.Time `json:"EndDate,omitempty"`
    55  	// The type of Events to read. Returns only Events of the type specified.
    56  	EventType *string `json:"EventType,omitempty"`
    57  	// The period of events to read in minutes. Returns only Events that occurred since this many minutes in the past. The default is `15` minutes. Task Attributes for Events occuring more 43,200 minutes ago will be redacted.
    58  	Minutes *int `json:"Minutes,omitempty"`
    59  	// The SID of the Reservation with the Events to read. Returns only Events that pertain to the specified Reservation.
    60  	ReservationSid *string `json:"ReservationSid,omitempty"`
    61  	// Only include Events from on or after this date and time, specified in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format. Task Attributes for Events older than 30 days will be redacted.
    62  	StartDate *time.Time `json:"StartDate,omitempty"`
    63  	// The SID of the TaskQueue with the Events to read. Returns only the Events that pertain to the specified TaskQueue.
    64  	TaskQueueSid *string `json:"TaskQueueSid,omitempty"`
    65  	// The SID of the Task with the Events to read. Returns only the Events that pertain to the specified Task.
    66  	TaskSid *string `json:"TaskSid,omitempty"`
    67  	// The SID of the Worker with the Events to read. Returns only the Events that pertain to the specified Worker.
    68  	WorkerSid *string `json:"WorkerSid,omitempty"`
    69  	// The SID of the Workflow with the Events to read. Returns only the Events that pertain to the specified Workflow.
    70  	WorkflowSid *string `json:"WorkflowSid,omitempty"`
    71  	// The TaskChannel with the Events to read. Returns only the Events that pertain to the specified TaskChannel.
    72  	TaskChannel *string `json:"TaskChannel,omitempty"`
    73  	// The SID of the Event resource to read.
    74  	Sid *string `json:"Sid,omitempty"`
    75  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    76  	PageSize *int `json:"PageSize,omitempty"`
    77  	// Max number of records to return.
    78  	Limit *int `json:"limit,omitempty"`
    79  }
    80  
    81  func (params *ListEventParams) SetEndDate(EndDate time.Time) *ListEventParams {
    82  	params.EndDate = &EndDate
    83  	return params
    84  }
    85  func (params *ListEventParams) SetEventType(EventType string) *ListEventParams {
    86  	params.EventType = &EventType
    87  	return params
    88  }
    89  func (params *ListEventParams) SetMinutes(Minutes int) *ListEventParams {
    90  	params.Minutes = &Minutes
    91  	return params
    92  }
    93  func (params *ListEventParams) SetReservationSid(ReservationSid string) *ListEventParams {
    94  	params.ReservationSid = &ReservationSid
    95  	return params
    96  }
    97  func (params *ListEventParams) SetStartDate(StartDate time.Time) *ListEventParams {
    98  	params.StartDate = &StartDate
    99  	return params
   100  }
   101  func (params *ListEventParams) SetTaskQueueSid(TaskQueueSid string) *ListEventParams {
   102  	params.TaskQueueSid = &TaskQueueSid
   103  	return params
   104  }
   105  func (params *ListEventParams) SetTaskSid(TaskSid string) *ListEventParams {
   106  	params.TaskSid = &TaskSid
   107  	return params
   108  }
   109  func (params *ListEventParams) SetWorkerSid(WorkerSid string) *ListEventParams {
   110  	params.WorkerSid = &WorkerSid
   111  	return params
   112  }
   113  func (params *ListEventParams) SetWorkflowSid(WorkflowSid string) *ListEventParams {
   114  	params.WorkflowSid = &WorkflowSid
   115  	return params
   116  }
   117  func (params *ListEventParams) SetTaskChannel(TaskChannel string) *ListEventParams {
   118  	params.TaskChannel = &TaskChannel
   119  	return params
   120  }
   121  func (params *ListEventParams) SetSid(Sid string) *ListEventParams {
   122  	params.Sid = &Sid
   123  	return params
   124  }
   125  func (params *ListEventParams) SetPageSize(PageSize int) *ListEventParams {
   126  	params.PageSize = &PageSize
   127  	return params
   128  }
   129  func (params *ListEventParams) SetLimit(Limit int) *ListEventParams {
   130  	params.Limit = &Limit
   131  	return params
   132  }
   133  
   134  // Retrieve a single page of Event records from the API. Request is executed immediately.
   135  func (c *ApiService) PageEvent(WorkspaceSid string, params *ListEventParams, pageToken, pageNumber string) (*ListEventResponse, error) {
   136  	path := "/v1/Workspaces/{WorkspaceSid}/Events"
   137  
   138  	path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1)
   139  
   140  	data := url.Values{}
   141  	headers := make(map[string]interface{})
   142  
   143  	if params != nil && params.EndDate != nil {
   144  		data.Set("EndDate", fmt.Sprint((*params.EndDate).Format(time.RFC3339)))
   145  	}
   146  	if params != nil && params.EventType != nil {
   147  		data.Set("EventType", *params.EventType)
   148  	}
   149  	if params != nil && params.Minutes != nil {
   150  		data.Set("Minutes", fmt.Sprint(*params.Minutes))
   151  	}
   152  	if params != nil && params.ReservationSid != nil {
   153  		data.Set("ReservationSid", *params.ReservationSid)
   154  	}
   155  	if params != nil && params.StartDate != nil {
   156  		data.Set("StartDate", fmt.Sprint((*params.StartDate).Format(time.RFC3339)))
   157  	}
   158  	if params != nil && params.TaskQueueSid != nil {
   159  		data.Set("TaskQueueSid", *params.TaskQueueSid)
   160  	}
   161  	if params != nil && params.TaskSid != nil {
   162  		data.Set("TaskSid", *params.TaskSid)
   163  	}
   164  	if params != nil && params.WorkerSid != nil {
   165  		data.Set("WorkerSid", *params.WorkerSid)
   166  	}
   167  	if params != nil && params.WorkflowSid != nil {
   168  		data.Set("WorkflowSid", *params.WorkflowSid)
   169  	}
   170  	if params != nil && params.TaskChannel != nil {
   171  		data.Set("TaskChannel", *params.TaskChannel)
   172  	}
   173  	if params != nil && params.Sid != nil {
   174  		data.Set("Sid", *params.Sid)
   175  	}
   176  	if params != nil && params.PageSize != nil {
   177  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   178  	}
   179  
   180  	if pageToken != "" {
   181  		data.Set("PageToken", pageToken)
   182  	}
   183  	if pageNumber != "" {
   184  		data.Set("Page", pageNumber)
   185  	}
   186  
   187  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   188  	if err != nil {
   189  		return nil, err
   190  	}
   191  
   192  	defer resp.Body.Close()
   193  
   194  	ps := &ListEventResponse{}
   195  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   196  		return nil, err
   197  	}
   198  
   199  	return ps, err
   200  }
   201  
   202  // Lists Event records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   203  func (c *ApiService) ListEvent(WorkspaceSid string, params *ListEventParams) ([]TaskrouterV1Event, error) {
   204  	response, errors := c.StreamEvent(WorkspaceSid, params)
   205  
   206  	records := make([]TaskrouterV1Event, 0)
   207  	for record := range response {
   208  		records = append(records, record)
   209  	}
   210  
   211  	if err := <-errors; err != nil {
   212  		return nil, err
   213  	}
   214  
   215  	return records, nil
   216  }
   217  
   218  // Streams Event records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   219  func (c *ApiService) StreamEvent(WorkspaceSid string, params *ListEventParams) (chan TaskrouterV1Event, chan error) {
   220  	if params == nil {
   221  		params = &ListEventParams{}
   222  	}
   223  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   224  
   225  	recordChannel := make(chan TaskrouterV1Event, 1)
   226  	errorChannel := make(chan error, 1)
   227  
   228  	response, err := c.PageEvent(WorkspaceSid, params, "", "")
   229  	if err != nil {
   230  		errorChannel <- err
   231  		close(recordChannel)
   232  		close(errorChannel)
   233  	} else {
   234  		go c.streamEvent(response, params, recordChannel, errorChannel)
   235  	}
   236  
   237  	return recordChannel, errorChannel
   238  }
   239  
   240  func (c *ApiService) streamEvent(response *ListEventResponse, params *ListEventParams, recordChannel chan TaskrouterV1Event, errorChannel chan error) {
   241  	curRecord := 1
   242  
   243  	for response != nil {
   244  		responseRecords := response.Events
   245  		for item := range responseRecords {
   246  			recordChannel <- responseRecords[item]
   247  			curRecord += 1
   248  			if params.Limit != nil && *params.Limit < curRecord {
   249  				close(recordChannel)
   250  				close(errorChannel)
   251  				return
   252  			}
   253  		}
   254  
   255  		record, err := client.GetNext(c.baseURL, response, c.getNextListEventResponse)
   256  		if err != nil {
   257  			errorChannel <- err
   258  			break
   259  		} else if record == nil {
   260  			break
   261  		}
   262  
   263  		response = record.(*ListEventResponse)
   264  	}
   265  
   266  	close(recordChannel)
   267  	close(errorChannel)
   268  }
   269  
   270  func (c *ApiService) getNextListEventResponse(nextPageUrl string) (interface{}, error) {
   271  	if nextPageUrl == "" {
   272  		return nil, nil
   273  	}
   274  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   275  	if err != nil {
   276  		return nil, err
   277  	}
   278  
   279  	defer resp.Body.Close()
   280  
   281  	ps := &ListEventResponse{}
   282  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   283  		return nil, err
   284  	}
   285  	return ps, nil
   286  }