github.com/twilio/twilio-go@v1.20.1/rest/proxy/v1/services_sessions_interactions.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Proxy 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 // Delete a specific Interaction. 27 func (c *ApiService) DeleteInteraction(ServiceSid string, SessionSid string, Sid string) error { 28 path := "/v1/Services/{ServiceSid}/Sessions/{SessionSid}/Interactions/{Sid}" 29 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 30 path = strings.Replace(path, "{"+"SessionSid"+"}", SessionSid, -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.Delete(c.baseURL+path, data, headers) 37 if err != nil { 38 return err 39 } 40 41 defer resp.Body.Close() 42 43 return nil 44 } 45 46 // Retrieve a list of Interactions for a given [Session](https://www.twilio.com/docs/proxy/api/session). 47 func (c *ApiService) FetchInteraction(ServiceSid string, SessionSid string, Sid string) (*ProxyV1Interaction, error) { 48 path := "/v1/Services/{ServiceSid}/Sessions/{SessionSid}/Interactions/{Sid}" 49 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 50 path = strings.Replace(path, "{"+"SessionSid"+"}", SessionSid, -1) 51 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 52 53 data := url.Values{} 54 headers := make(map[string]interface{}) 55 56 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 57 if err != nil { 58 return nil, err 59 } 60 61 defer resp.Body.Close() 62 63 ps := &ProxyV1Interaction{} 64 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 65 return nil, err 66 } 67 68 return ps, err 69 } 70 71 // Optional parameters for the method 'ListInteraction' 72 type ListInteractionParams struct { 73 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 74 PageSize *int `json:"PageSize,omitempty"` 75 // Max number of records to return. 76 Limit *int `json:"limit,omitempty"` 77 } 78 79 func (params *ListInteractionParams) SetPageSize(PageSize int) *ListInteractionParams { 80 params.PageSize = &PageSize 81 return params 82 } 83 func (params *ListInteractionParams) SetLimit(Limit int) *ListInteractionParams { 84 params.Limit = &Limit 85 return params 86 } 87 88 // Retrieve a single page of Interaction records from the API. Request is executed immediately. 89 func (c *ApiService) PageInteraction(ServiceSid string, SessionSid string, params *ListInteractionParams, pageToken, pageNumber string) (*ListInteractionResponse, error) { 90 path := "/v1/Services/{ServiceSid}/Sessions/{SessionSid}/Interactions" 91 92 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 93 path = strings.Replace(path, "{"+"SessionSid"+"}", SessionSid, -1) 94 95 data := url.Values{} 96 headers := make(map[string]interface{}) 97 98 if params != nil && params.PageSize != nil { 99 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 100 } 101 102 if pageToken != "" { 103 data.Set("PageToken", pageToken) 104 } 105 if pageNumber != "" { 106 data.Set("Page", pageNumber) 107 } 108 109 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 110 if err != nil { 111 return nil, err 112 } 113 114 defer resp.Body.Close() 115 116 ps := &ListInteractionResponse{} 117 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 118 return nil, err 119 } 120 121 return ps, err 122 } 123 124 // Lists Interaction records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 125 func (c *ApiService) ListInteraction(ServiceSid string, SessionSid string, params *ListInteractionParams) ([]ProxyV1Interaction, error) { 126 response, errors := c.StreamInteraction(ServiceSid, SessionSid, params) 127 128 records := make([]ProxyV1Interaction, 0) 129 for record := range response { 130 records = append(records, record) 131 } 132 133 if err := <-errors; err != nil { 134 return nil, err 135 } 136 137 return records, nil 138 } 139 140 // Streams Interaction records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 141 func (c *ApiService) StreamInteraction(ServiceSid string, SessionSid string, params *ListInteractionParams) (chan ProxyV1Interaction, chan error) { 142 if params == nil { 143 params = &ListInteractionParams{} 144 } 145 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 146 147 recordChannel := make(chan ProxyV1Interaction, 1) 148 errorChannel := make(chan error, 1) 149 150 response, err := c.PageInteraction(ServiceSid, SessionSid, params, "", "") 151 if err != nil { 152 errorChannel <- err 153 close(recordChannel) 154 close(errorChannel) 155 } else { 156 go c.streamInteraction(response, params, recordChannel, errorChannel) 157 } 158 159 return recordChannel, errorChannel 160 } 161 162 func (c *ApiService) streamInteraction(response *ListInteractionResponse, params *ListInteractionParams, recordChannel chan ProxyV1Interaction, errorChannel chan error) { 163 curRecord := 1 164 165 for response != nil { 166 responseRecords := response.Interactions 167 for item := range responseRecords { 168 recordChannel <- responseRecords[item] 169 curRecord += 1 170 if params.Limit != nil && *params.Limit < curRecord { 171 close(recordChannel) 172 close(errorChannel) 173 return 174 } 175 } 176 177 record, err := client.GetNext(c.baseURL, response, c.getNextListInteractionResponse) 178 if err != nil { 179 errorChannel <- err 180 break 181 } else if record == nil { 182 break 183 } 184 185 response = record.(*ListInteractionResponse) 186 } 187 188 close(recordChannel) 189 close(errorChannel) 190 } 191 192 func (c *ApiService) getNextListInteractionResponse(nextPageUrl string) (interface{}, error) { 193 if nextPageUrl == "" { 194 return nil, nil 195 } 196 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 197 if err != nil { 198 return nil, err 199 } 200 201 defer resp.Body.Close() 202 203 ps := &ListInteractionResponse{} 204 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 205 return nil, err 206 } 207 return ps, nil 208 }