github.com/twilio/twilio-go@v1.20.1/rest/insights/v1/voice_events.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Insights 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 'ListEvent' 27 type ListEventParams struct { 28 // The Edge of this Event. One of `unknown_edge`, `carrier_edge`, `sip_edge`, `sdk_edge` or `client_edge`. 29 Edge *string `json:"Edge,omitempty"` 30 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 31 PageSize *int `json:"PageSize,omitempty"` 32 // Max number of records to return. 33 Limit *int `json:"limit,omitempty"` 34 } 35 36 func (params *ListEventParams) SetEdge(Edge string) *ListEventParams { 37 params.Edge = &Edge 38 return params 39 } 40 func (params *ListEventParams) SetPageSize(PageSize int) *ListEventParams { 41 params.PageSize = &PageSize 42 return params 43 } 44 func (params *ListEventParams) SetLimit(Limit int) *ListEventParams { 45 params.Limit = &Limit 46 return params 47 } 48 49 // Retrieve a single page of Event records from the API. Request is executed immediately. 50 func (c *ApiService) PageEvent(CallSid string, params *ListEventParams, pageToken, pageNumber string) (*ListEventResponse, error) { 51 path := "/v1/Voice/{CallSid}/Events" 52 53 path = strings.Replace(path, "{"+"CallSid"+"}", CallSid, -1) 54 55 data := url.Values{} 56 headers := make(map[string]interface{}) 57 58 if params != nil && params.Edge != nil { 59 data.Set("Edge", *params.Edge) 60 } 61 if params != nil && params.PageSize != nil { 62 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 63 } 64 65 if pageToken != "" { 66 data.Set("PageToken", pageToken) 67 } 68 if pageNumber != "" { 69 data.Set("Page", pageNumber) 70 } 71 72 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 73 if err != nil { 74 return nil, err 75 } 76 77 defer resp.Body.Close() 78 79 ps := &ListEventResponse{} 80 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 81 return nil, err 82 } 83 84 return ps, err 85 } 86 87 // Lists Event records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 88 func (c *ApiService) ListEvent(CallSid string, params *ListEventParams) ([]InsightsV1Event, error) { 89 response, errors := c.StreamEvent(CallSid, params) 90 91 records := make([]InsightsV1Event, 0) 92 for record := range response { 93 records = append(records, record) 94 } 95 96 if err := <-errors; err != nil { 97 return nil, err 98 } 99 100 return records, nil 101 } 102 103 // Streams Event records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 104 func (c *ApiService) StreamEvent(CallSid string, params *ListEventParams) (chan InsightsV1Event, chan error) { 105 if params == nil { 106 params = &ListEventParams{} 107 } 108 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 109 110 recordChannel := make(chan InsightsV1Event, 1) 111 errorChannel := make(chan error, 1) 112 113 response, err := c.PageEvent(CallSid, params, "", "") 114 if err != nil { 115 errorChannel <- err 116 close(recordChannel) 117 close(errorChannel) 118 } else { 119 go c.streamEvent(response, params, recordChannel, errorChannel) 120 } 121 122 return recordChannel, errorChannel 123 } 124 125 func (c *ApiService) streamEvent(response *ListEventResponse, params *ListEventParams, recordChannel chan InsightsV1Event, errorChannel chan error) { 126 curRecord := 1 127 128 for response != nil { 129 responseRecords := response.Events 130 for item := range responseRecords { 131 recordChannel <- responseRecords[item] 132 curRecord += 1 133 if params.Limit != nil && *params.Limit < curRecord { 134 close(recordChannel) 135 close(errorChannel) 136 return 137 } 138 } 139 140 record, err := client.GetNext(c.baseURL, response, c.getNextListEventResponse) 141 if err != nil { 142 errorChannel <- err 143 break 144 } else if record == nil { 145 break 146 } 147 148 response = record.(*ListEventResponse) 149 } 150 151 close(recordChannel) 152 close(errorChannel) 153 } 154 155 func (c *ApiService) getNextListEventResponse(nextPageUrl string) (interface{}, error) { 156 if nextPageUrl == "" { 157 return nil, nil 158 } 159 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 160 if err != nil { 161 return nil, err 162 } 163 164 defer resp.Body.Close() 165 166 ps := &ListEventResponse{} 167 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 168 return nil, err 169 } 170 return ps, nil 171 }