github.com/twilio/twilio-go@v1.20.1/rest/monitor/v1/events.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Monitor
     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(Sid string) (*MonitorV1Event, error) {
    29  	path := "/v1/Events/{Sid}"
    30  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    31  
    32  	data := url.Values{}
    33  	headers := make(map[string]interface{})
    34  
    35  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	defer resp.Body.Close()
    41  
    42  	ps := &MonitorV1Event{}
    43  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	return ps, err
    48  }
    49  
    50  // Optional parameters for the method 'ListEvent'
    51  type ListEventParams struct {
    52  	// Only include events initiated by this Actor. Useful for auditing actions taken by specific users or API credentials.
    53  	ActorSid *string `json:"ActorSid,omitempty"`
    54  	// Only include events of this [Event Type](https://www.twilio.com/docs/usage/monitor-events#event-types).
    55  	EventType *string `json:"EventType,omitempty"`
    56  	// Only include events that refer to this resource. Useful for discovering the history of a specific resource.
    57  	ResourceSid *string `json:"ResourceSid,omitempty"`
    58  	// Only include events that originated from this IP address. Useful for tracking suspicious activity originating from the API or the Twilio Console.
    59  	SourceIpAddress *string `json:"SourceIpAddress,omitempty"`
    60  	// Only include events that occurred on or after this date. Specify the date in GMT and [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format.
    61  	StartDate *time.Time `json:"StartDate,omitempty"`
    62  	// Only include events that occurred on or before this date. Specify the date in GMT and [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format.
    63  	EndDate *time.Time `json:"EndDate,omitempty"`
    64  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    65  	PageSize *int `json:"PageSize,omitempty"`
    66  	// Max number of records to return.
    67  	Limit *int `json:"limit,omitempty"`
    68  }
    69  
    70  func (params *ListEventParams) SetActorSid(ActorSid string) *ListEventParams {
    71  	params.ActorSid = &ActorSid
    72  	return params
    73  }
    74  func (params *ListEventParams) SetEventType(EventType string) *ListEventParams {
    75  	params.EventType = &EventType
    76  	return params
    77  }
    78  func (params *ListEventParams) SetResourceSid(ResourceSid string) *ListEventParams {
    79  	params.ResourceSid = &ResourceSid
    80  	return params
    81  }
    82  func (params *ListEventParams) SetSourceIpAddress(SourceIpAddress string) *ListEventParams {
    83  	params.SourceIpAddress = &SourceIpAddress
    84  	return params
    85  }
    86  func (params *ListEventParams) SetStartDate(StartDate time.Time) *ListEventParams {
    87  	params.StartDate = &StartDate
    88  	return params
    89  }
    90  func (params *ListEventParams) SetEndDate(EndDate time.Time) *ListEventParams {
    91  	params.EndDate = &EndDate
    92  	return params
    93  }
    94  func (params *ListEventParams) SetPageSize(PageSize int) *ListEventParams {
    95  	params.PageSize = &PageSize
    96  	return params
    97  }
    98  func (params *ListEventParams) SetLimit(Limit int) *ListEventParams {
    99  	params.Limit = &Limit
   100  	return params
   101  }
   102  
   103  // Retrieve a single page of Event records from the API. Request is executed immediately.
   104  func (c *ApiService) PageEvent(params *ListEventParams, pageToken, pageNumber string) (*ListEventResponse, error) {
   105  	path := "/v1/Events"
   106  
   107  	data := url.Values{}
   108  	headers := make(map[string]interface{})
   109  
   110  	if params != nil && params.ActorSid != nil {
   111  		data.Set("ActorSid", *params.ActorSid)
   112  	}
   113  	if params != nil && params.EventType != nil {
   114  		data.Set("EventType", *params.EventType)
   115  	}
   116  	if params != nil && params.ResourceSid != nil {
   117  		data.Set("ResourceSid", *params.ResourceSid)
   118  	}
   119  	if params != nil && params.SourceIpAddress != nil {
   120  		data.Set("SourceIpAddress", *params.SourceIpAddress)
   121  	}
   122  	if params != nil && params.StartDate != nil {
   123  		data.Set("StartDate", fmt.Sprint((*params.StartDate).Format(time.RFC3339)))
   124  	}
   125  	if params != nil && params.EndDate != nil {
   126  		data.Set("EndDate", fmt.Sprint((*params.EndDate).Format(time.RFC3339)))
   127  	}
   128  	if params != nil && params.PageSize != nil {
   129  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   130  	}
   131  
   132  	if pageToken != "" {
   133  		data.Set("PageToken", pageToken)
   134  	}
   135  	if pageNumber != "" {
   136  		data.Set("Page", pageNumber)
   137  	}
   138  
   139  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   140  	if err != nil {
   141  		return nil, err
   142  	}
   143  
   144  	defer resp.Body.Close()
   145  
   146  	ps := &ListEventResponse{}
   147  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   148  		return nil, err
   149  	}
   150  
   151  	return ps, err
   152  }
   153  
   154  // Lists Event records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   155  func (c *ApiService) ListEvent(params *ListEventParams) ([]MonitorV1Event, error) {
   156  	response, errors := c.StreamEvent(params)
   157  
   158  	records := make([]MonitorV1Event, 0)
   159  	for record := range response {
   160  		records = append(records, record)
   161  	}
   162  
   163  	if err := <-errors; err != nil {
   164  		return nil, err
   165  	}
   166  
   167  	return records, nil
   168  }
   169  
   170  // Streams Event records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   171  func (c *ApiService) StreamEvent(params *ListEventParams) (chan MonitorV1Event, chan error) {
   172  	if params == nil {
   173  		params = &ListEventParams{}
   174  	}
   175  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   176  
   177  	recordChannel := make(chan MonitorV1Event, 1)
   178  	errorChannel := make(chan error, 1)
   179  
   180  	response, err := c.PageEvent(params, "", "")
   181  	if err != nil {
   182  		errorChannel <- err
   183  		close(recordChannel)
   184  		close(errorChannel)
   185  	} else {
   186  		go c.streamEvent(response, params, recordChannel, errorChannel)
   187  	}
   188  
   189  	return recordChannel, errorChannel
   190  }
   191  
   192  func (c *ApiService) streamEvent(response *ListEventResponse, params *ListEventParams, recordChannel chan MonitorV1Event, errorChannel chan error) {
   193  	curRecord := 1
   194  
   195  	for response != nil {
   196  		responseRecords := response.Events
   197  		for item := range responseRecords {
   198  			recordChannel <- responseRecords[item]
   199  			curRecord += 1
   200  			if params.Limit != nil && *params.Limit < curRecord {
   201  				close(recordChannel)
   202  				close(errorChannel)
   203  				return
   204  			}
   205  		}
   206  
   207  		record, err := client.GetNext(c.baseURL, response, c.getNextListEventResponse)
   208  		if err != nil {
   209  			errorChannel <- err
   210  			break
   211  		} else if record == nil {
   212  			break
   213  		}
   214  
   215  		response = record.(*ListEventResponse)
   216  	}
   217  
   218  	close(recordChannel)
   219  	close(errorChannel)
   220  }
   221  
   222  func (c *ApiService) getNextListEventResponse(nextPageUrl string) (interface{}, error) {
   223  	if nextPageUrl == "" {
   224  		return nil, nil
   225  	}
   226  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   227  	if err != nil {
   228  		return nil, err
   229  	}
   230  
   231  	defer resp.Body.Close()
   232  
   233  	ps := &ListEventResponse{}
   234  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   235  		return nil, err
   236  	}
   237  	return ps, nil
   238  }