github.com/twilio/twilio-go@v1.20.1/rest/events/v1/sinks.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Events 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 'CreateSink' 27 type CreateSinkParams struct { 28 // A human readable description for the Sink **This value should not contain PII.** 29 Description *string `json:"Description,omitempty"` 30 // The information required for Twilio to connect to the provided Sink encoded as JSON. 31 SinkConfiguration *interface{} `json:"SinkConfiguration,omitempty"` 32 // 33 SinkType *string `json:"SinkType,omitempty"` 34 } 35 36 func (params *CreateSinkParams) SetDescription(Description string) *CreateSinkParams { 37 params.Description = &Description 38 return params 39 } 40 func (params *CreateSinkParams) SetSinkConfiguration(SinkConfiguration interface{}) *CreateSinkParams { 41 params.SinkConfiguration = &SinkConfiguration 42 return params 43 } 44 func (params *CreateSinkParams) SetSinkType(SinkType string) *CreateSinkParams { 45 params.SinkType = &SinkType 46 return params 47 } 48 49 // Create a new Sink 50 func (c *ApiService) CreateSink(params *CreateSinkParams) (*EventsV1Sink, error) { 51 path := "/v1/Sinks" 52 53 data := url.Values{} 54 headers := make(map[string]interface{}) 55 56 if params != nil && params.Description != nil { 57 data.Set("Description", *params.Description) 58 } 59 if params != nil && params.SinkConfiguration != nil { 60 v, err := json.Marshal(params.SinkConfiguration) 61 62 if err != nil { 63 return nil, err 64 } 65 66 data.Set("SinkConfiguration", string(v)) 67 } 68 if params != nil && params.SinkType != nil { 69 data.Set("SinkType", *params.SinkType) 70 } 71 72 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 73 if err != nil { 74 return nil, err 75 } 76 77 defer resp.Body.Close() 78 79 ps := &EventsV1Sink{} 80 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 81 return nil, err 82 } 83 84 return ps, err 85 } 86 87 // Delete a specific Sink. 88 func (c *ApiService) DeleteSink(Sid string) error { 89 path := "/v1/Sinks/{Sid}" 90 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 91 92 data := url.Values{} 93 headers := make(map[string]interface{}) 94 95 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 96 if err != nil { 97 return err 98 } 99 100 defer resp.Body.Close() 101 102 return nil 103 } 104 105 // Fetch a specific Sink. 106 func (c *ApiService) FetchSink(Sid string) (*EventsV1Sink, error) { 107 path := "/v1/Sinks/{Sid}" 108 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 109 110 data := url.Values{} 111 headers := make(map[string]interface{}) 112 113 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 114 if err != nil { 115 return nil, err 116 } 117 118 defer resp.Body.Close() 119 120 ps := &EventsV1Sink{} 121 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 122 return nil, err 123 } 124 125 return ps, err 126 } 127 128 // Optional parameters for the method 'ListSink' 129 type ListSinkParams struct { 130 // A boolean query parameter filtering the results to return sinks used/not used by a subscription. 131 InUse *bool `json:"InUse,omitempty"` 132 // A String query parameter filtering the results by status `initialized`, `validating`, `active` or `failed`. 133 Status *string `json:"Status,omitempty"` 134 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 135 PageSize *int `json:"PageSize,omitempty"` 136 // Max number of records to return. 137 Limit *int `json:"limit,omitempty"` 138 } 139 140 func (params *ListSinkParams) SetInUse(InUse bool) *ListSinkParams { 141 params.InUse = &InUse 142 return params 143 } 144 func (params *ListSinkParams) SetStatus(Status string) *ListSinkParams { 145 params.Status = &Status 146 return params 147 } 148 func (params *ListSinkParams) SetPageSize(PageSize int) *ListSinkParams { 149 params.PageSize = &PageSize 150 return params 151 } 152 func (params *ListSinkParams) SetLimit(Limit int) *ListSinkParams { 153 params.Limit = &Limit 154 return params 155 } 156 157 // Retrieve a single page of Sink records from the API. Request is executed immediately. 158 func (c *ApiService) PageSink(params *ListSinkParams, pageToken, pageNumber string) (*ListSinkResponse, error) { 159 path := "/v1/Sinks" 160 161 data := url.Values{} 162 headers := make(map[string]interface{}) 163 164 if params != nil && params.InUse != nil { 165 data.Set("InUse", fmt.Sprint(*params.InUse)) 166 } 167 if params != nil && params.Status != nil { 168 data.Set("Status", *params.Status) 169 } 170 if params != nil && params.PageSize != nil { 171 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 172 } 173 174 if pageToken != "" { 175 data.Set("PageToken", pageToken) 176 } 177 if pageNumber != "" { 178 data.Set("Page", pageNumber) 179 } 180 181 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 182 if err != nil { 183 return nil, err 184 } 185 186 defer resp.Body.Close() 187 188 ps := &ListSinkResponse{} 189 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 190 return nil, err 191 } 192 193 return ps, err 194 } 195 196 // Lists Sink records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 197 func (c *ApiService) ListSink(params *ListSinkParams) ([]EventsV1Sink, error) { 198 response, errors := c.StreamSink(params) 199 200 records := make([]EventsV1Sink, 0) 201 for record := range response { 202 records = append(records, record) 203 } 204 205 if err := <-errors; err != nil { 206 return nil, err 207 } 208 209 return records, nil 210 } 211 212 // Streams Sink records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 213 func (c *ApiService) StreamSink(params *ListSinkParams) (chan EventsV1Sink, chan error) { 214 if params == nil { 215 params = &ListSinkParams{} 216 } 217 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 218 219 recordChannel := make(chan EventsV1Sink, 1) 220 errorChannel := make(chan error, 1) 221 222 response, err := c.PageSink(params, "", "") 223 if err != nil { 224 errorChannel <- err 225 close(recordChannel) 226 close(errorChannel) 227 } else { 228 go c.streamSink(response, params, recordChannel, errorChannel) 229 } 230 231 return recordChannel, errorChannel 232 } 233 234 func (c *ApiService) streamSink(response *ListSinkResponse, params *ListSinkParams, recordChannel chan EventsV1Sink, errorChannel chan error) { 235 curRecord := 1 236 237 for response != nil { 238 responseRecords := response.Sinks 239 for item := range responseRecords { 240 recordChannel <- responseRecords[item] 241 curRecord += 1 242 if params.Limit != nil && *params.Limit < curRecord { 243 close(recordChannel) 244 close(errorChannel) 245 return 246 } 247 } 248 249 record, err := client.GetNext(c.baseURL, response, c.getNextListSinkResponse) 250 if err != nil { 251 errorChannel <- err 252 break 253 } else if record == nil { 254 break 255 } 256 257 response = record.(*ListSinkResponse) 258 } 259 260 close(recordChannel) 261 close(errorChannel) 262 } 263 264 func (c *ApiService) getNextListSinkResponse(nextPageUrl string) (interface{}, error) { 265 if nextPageUrl == "" { 266 return nil, nil 267 } 268 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 269 if err != nil { 270 return nil, err 271 } 272 273 defer resp.Body.Close() 274 275 ps := &ListSinkResponse{} 276 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 277 return nil, err 278 } 279 return ps, nil 280 } 281 282 // Optional parameters for the method 'UpdateSink' 283 type UpdateSinkParams struct { 284 // A human readable description for the Sink **This value should not contain PII.** 285 Description *string `json:"Description,omitempty"` 286 } 287 288 func (params *UpdateSinkParams) SetDescription(Description string) *UpdateSinkParams { 289 params.Description = &Description 290 return params 291 } 292 293 // Update a specific Sink 294 func (c *ApiService) UpdateSink(Sid string, params *UpdateSinkParams) (*EventsV1Sink, error) { 295 path := "/v1/Sinks/{Sid}" 296 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 297 298 data := url.Values{} 299 headers := make(map[string]interface{}) 300 301 if params != nil && params.Description != nil { 302 data.Set("Description", *params.Description) 303 } 304 305 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 306 if err != nil { 307 return nil, err 308 } 309 310 defer resp.Body.Close() 311 312 ps := &EventsV1Sink{} 313 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 314 return nil, err 315 } 316 317 return ps, err 318 }