github.com/twilio/twilio-go@v1.20.1/rest/monitor/v1/alerts.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) FetchAlert(Sid string) (*MonitorV1AlertInstance, error) { 29 path := "/v1/Alerts/{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 := &MonitorV1AlertInstance{} 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 'ListAlert' 51 type ListAlertParams struct { 52 // Only show alerts for this log-level. Can be: `error`, `warning`, `notice`, or `debug`. 53 LogLevel *string `json:"LogLevel,omitempty"` 54 // Only include alerts that occurred on or after this date and time. Specify the date and time in GMT and format as `YYYY-MM-DD` or `YYYY-MM-DDThh:mm:ssZ`. Queries for alerts older than 30 days are not supported. 55 StartDate *time.Time `json:"StartDate,omitempty"` 56 // Only include alerts that occurred on or before this date and time. Specify the date and time in GMT and format as `YYYY-MM-DD` or `YYYY-MM-DDThh:mm:ssZ`. Queries for alerts older than 30 days are not supported. 57 EndDate *time.Time `json:"EndDate,omitempty"` 58 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 59 PageSize *int `json:"PageSize,omitempty"` 60 // Max number of records to return. 61 Limit *int `json:"limit,omitempty"` 62 } 63 64 func (params *ListAlertParams) SetLogLevel(LogLevel string) *ListAlertParams { 65 params.LogLevel = &LogLevel 66 return params 67 } 68 func (params *ListAlertParams) SetStartDate(StartDate time.Time) *ListAlertParams { 69 params.StartDate = &StartDate 70 return params 71 } 72 func (params *ListAlertParams) SetEndDate(EndDate time.Time) *ListAlertParams { 73 params.EndDate = &EndDate 74 return params 75 } 76 func (params *ListAlertParams) SetPageSize(PageSize int) *ListAlertParams { 77 params.PageSize = &PageSize 78 return params 79 } 80 func (params *ListAlertParams) SetLimit(Limit int) *ListAlertParams { 81 params.Limit = &Limit 82 return params 83 } 84 85 // Retrieve a single page of Alert records from the API. Request is executed immediately. 86 func (c *ApiService) PageAlert(params *ListAlertParams, pageToken, pageNumber string) (*ListAlertResponse, error) { 87 path := "/v1/Alerts" 88 89 data := url.Values{} 90 headers := make(map[string]interface{}) 91 92 if params != nil && params.LogLevel != nil { 93 data.Set("LogLevel", *params.LogLevel) 94 } 95 if params != nil && params.StartDate != nil { 96 data.Set("StartDate", fmt.Sprint((*params.StartDate).Format(time.RFC3339))) 97 } 98 if params != nil && params.EndDate != nil { 99 data.Set("EndDate", fmt.Sprint((*params.EndDate).Format(time.RFC3339))) 100 } 101 if params != nil && params.PageSize != nil { 102 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 103 } 104 105 if pageToken != "" { 106 data.Set("PageToken", pageToken) 107 } 108 if pageNumber != "" { 109 data.Set("Page", pageNumber) 110 } 111 112 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 113 if err != nil { 114 return nil, err 115 } 116 117 defer resp.Body.Close() 118 119 ps := &ListAlertResponse{} 120 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 121 return nil, err 122 } 123 124 return ps, err 125 } 126 127 // Lists Alert records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 128 func (c *ApiService) ListAlert(params *ListAlertParams) ([]MonitorV1Alert, error) { 129 response, errors := c.StreamAlert(params) 130 131 records := make([]MonitorV1Alert, 0) 132 for record := range response { 133 records = append(records, record) 134 } 135 136 if err := <-errors; err != nil { 137 return nil, err 138 } 139 140 return records, nil 141 } 142 143 // Streams Alert records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 144 func (c *ApiService) StreamAlert(params *ListAlertParams) (chan MonitorV1Alert, chan error) { 145 if params == nil { 146 params = &ListAlertParams{} 147 } 148 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 149 150 recordChannel := make(chan MonitorV1Alert, 1) 151 errorChannel := make(chan error, 1) 152 153 response, err := c.PageAlert(params, "", "") 154 if err != nil { 155 errorChannel <- err 156 close(recordChannel) 157 close(errorChannel) 158 } else { 159 go c.streamAlert(response, params, recordChannel, errorChannel) 160 } 161 162 return recordChannel, errorChannel 163 } 164 165 func (c *ApiService) streamAlert(response *ListAlertResponse, params *ListAlertParams, recordChannel chan MonitorV1Alert, errorChannel chan error) { 166 curRecord := 1 167 168 for response != nil { 169 responseRecords := response.Alerts 170 for item := range responseRecords { 171 recordChannel <- responseRecords[item] 172 curRecord += 1 173 if params.Limit != nil && *params.Limit < curRecord { 174 close(recordChannel) 175 close(errorChannel) 176 return 177 } 178 } 179 180 record, err := client.GetNext(c.baseURL, response, c.getNextListAlertResponse) 181 if err != nil { 182 errorChannel <- err 183 break 184 } else if record == nil { 185 break 186 } 187 188 response = record.(*ListAlertResponse) 189 } 190 191 close(recordChannel) 192 close(errorChannel) 193 } 194 195 func (c *ApiService) getNextListAlertResponse(nextPageUrl string) (interface{}, error) { 196 if nextPageUrl == "" { 197 return nil, nil 198 } 199 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 200 if err != nil { 201 return nil, err 202 } 203 204 defer resp.Body.Close() 205 206 ps := &ListAlertResponse{} 207 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 208 return nil, err 209 } 210 return ps, nil 211 }