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