github.com/twilio/twilio-go@v1.20.1/rest/events/v1/subscriptions.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 'CreateSubscription' 27 type CreateSubscriptionParams struct { 28 // A human readable description for the Subscription **This value should not contain PII.** 29 Description *string `json:"Description,omitempty"` 30 // The SID of the sink that events selected by this subscription should be sent to. Sink must be active for the subscription to be created. 31 SinkSid *string `json:"SinkSid,omitempty"` 32 // An array of objects containing the subscribed Event Types 33 Types *[]interface{} `json:"Types,omitempty"` 34 } 35 36 func (params *CreateSubscriptionParams) SetDescription(Description string) *CreateSubscriptionParams { 37 params.Description = &Description 38 return params 39 } 40 func (params *CreateSubscriptionParams) SetSinkSid(SinkSid string) *CreateSubscriptionParams { 41 params.SinkSid = &SinkSid 42 return params 43 } 44 func (params *CreateSubscriptionParams) SetTypes(Types []interface{}) *CreateSubscriptionParams { 45 params.Types = &Types 46 return params 47 } 48 49 // Create a new Subscription. 50 func (c *ApiService) CreateSubscription(params *CreateSubscriptionParams) (*EventsV1Subscription, error) { 51 path := "/v1/Subscriptions" 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.SinkSid != nil { 60 data.Set("SinkSid", *params.SinkSid) 61 } 62 if params != nil && params.Types != nil { 63 for _, item := range *params.Types { 64 v, err := json.Marshal(item) 65 66 if err != nil { 67 return nil, err 68 } 69 70 data.Add("Types", string(v)) 71 } 72 } 73 74 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 75 if err != nil { 76 return nil, err 77 } 78 79 defer resp.Body.Close() 80 81 ps := &EventsV1Subscription{} 82 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 83 return nil, err 84 } 85 86 return ps, err 87 } 88 89 // Delete a specific Subscription. 90 func (c *ApiService) DeleteSubscription(Sid string) error { 91 path := "/v1/Subscriptions/{Sid}" 92 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 93 94 data := url.Values{} 95 headers := make(map[string]interface{}) 96 97 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 98 if err != nil { 99 return err 100 } 101 102 defer resp.Body.Close() 103 104 return nil 105 } 106 107 // Fetch a specific Subscription. 108 func (c *ApiService) FetchSubscription(Sid string) (*EventsV1Subscription, error) { 109 path := "/v1/Subscriptions/{Sid}" 110 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 111 112 data := url.Values{} 113 headers := make(map[string]interface{}) 114 115 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 116 if err != nil { 117 return nil, err 118 } 119 120 defer resp.Body.Close() 121 122 ps := &EventsV1Subscription{} 123 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 124 return nil, err 125 } 126 127 return ps, err 128 } 129 130 // Optional parameters for the method 'ListSubscription' 131 type ListSubscriptionParams struct { 132 // The SID of the sink that the list of Subscriptions should be filtered by. 133 SinkSid *string `json:"SinkSid,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 *ListSubscriptionParams) SetSinkSid(SinkSid string) *ListSubscriptionParams { 141 params.SinkSid = &SinkSid 142 return params 143 } 144 func (params *ListSubscriptionParams) SetPageSize(PageSize int) *ListSubscriptionParams { 145 params.PageSize = &PageSize 146 return params 147 } 148 func (params *ListSubscriptionParams) SetLimit(Limit int) *ListSubscriptionParams { 149 params.Limit = &Limit 150 return params 151 } 152 153 // Retrieve a single page of Subscription records from the API. Request is executed immediately. 154 func (c *ApiService) PageSubscription(params *ListSubscriptionParams, pageToken, pageNumber string) (*ListSubscriptionResponse, error) { 155 path := "/v1/Subscriptions" 156 157 data := url.Values{} 158 headers := make(map[string]interface{}) 159 160 if params != nil && params.SinkSid != nil { 161 data.Set("SinkSid", *params.SinkSid) 162 } 163 if params != nil && params.PageSize != nil { 164 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 165 } 166 167 if pageToken != "" { 168 data.Set("PageToken", pageToken) 169 } 170 if pageNumber != "" { 171 data.Set("Page", pageNumber) 172 } 173 174 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 175 if err != nil { 176 return nil, err 177 } 178 179 defer resp.Body.Close() 180 181 ps := &ListSubscriptionResponse{} 182 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 183 return nil, err 184 } 185 186 return ps, err 187 } 188 189 // Lists Subscription records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 190 func (c *ApiService) ListSubscription(params *ListSubscriptionParams) ([]EventsV1Subscription, error) { 191 response, errors := c.StreamSubscription(params) 192 193 records := make([]EventsV1Subscription, 0) 194 for record := range response { 195 records = append(records, record) 196 } 197 198 if err := <-errors; err != nil { 199 return nil, err 200 } 201 202 return records, nil 203 } 204 205 // Streams Subscription records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 206 func (c *ApiService) StreamSubscription(params *ListSubscriptionParams) (chan EventsV1Subscription, chan error) { 207 if params == nil { 208 params = &ListSubscriptionParams{} 209 } 210 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 211 212 recordChannel := make(chan EventsV1Subscription, 1) 213 errorChannel := make(chan error, 1) 214 215 response, err := c.PageSubscription(params, "", "") 216 if err != nil { 217 errorChannel <- err 218 close(recordChannel) 219 close(errorChannel) 220 } else { 221 go c.streamSubscription(response, params, recordChannel, errorChannel) 222 } 223 224 return recordChannel, errorChannel 225 } 226 227 func (c *ApiService) streamSubscription(response *ListSubscriptionResponse, params *ListSubscriptionParams, recordChannel chan EventsV1Subscription, errorChannel chan error) { 228 curRecord := 1 229 230 for response != nil { 231 responseRecords := response.Subscriptions 232 for item := range responseRecords { 233 recordChannel <- responseRecords[item] 234 curRecord += 1 235 if params.Limit != nil && *params.Limit < curRecord { 236 close(recordChannel) 237 close(errorChannel) 238 return 239 } 240 } 241 242 record, err := client.GetNext(c.baseURL, response, c.getNextListSubscriptionResponse) 243 if err != nil { 244 errorChannel <- err 245 break 246 } else if record == nil { 247 break 248 } 249 250 response = record.(*ListSubscriptionResponse) 251 } 252 253 close(recordChannel) 254 close(errorChannel) 255 } 256 257 func (c *ApiService) getNextListSubscriptionResponse(nextPageUrl string) (interface{}, error) { 258 if nextPageUrl == "" { 259 return nil, nil 260 } 261 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 262 if err != nil { 263 return nil, err 264 } 265 266 defer resp.Body.Close() 267 268 ps := &ListSubscriptionResponse{} 269 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 270 return nil, err 271 } 272 return ps, nil 273 } 274 275 // Optional parameters for the method 'UpdateSubscription' 276 type UpdateSubscriptionParams struct { 277 // A human readable description for the Subscription. 278 Description *string `json:"Description,omitempty"` 279 // The SID of the sink that events selected by this subscription should be sent to. Sink must be active for the subscription to be created. 280 SinkSid *string `json:"SinkSid,omitempty"` 281 } 282 283 func (params *UpdateSubscriptionParams) SetDescription(Description string) *UpdateSubscriptionParams { 284 params.Description = &Description 285 return params 286 } 287 func (params *UpdateSubscriptionParams) SetSinkSid(SinkSid string) *UpdateSubscriptionParams { 288 params.SinkSid = &SinkSid 289 return params 290 } 291 292 // Update a Subscription. 293 func (c *ApiService) UpdateSubscription(Sid string, params *UpdateSubscriptionParams) (*EventsV1Subscription, error) { 294 path := "/v1/Subscriptions/{Sid}" 295 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 296 297 data := url.Values{} 298 headers := make(map[string]interface{}) 299 300 if params != nil && params.Description != nil { 301 data.Set("Description", *params.Description) 302 } 303 if params != nil && params.SinkSid != nil { 304 data.Set("SinkSid", *params.SinkSid) 305 } 306 307 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 308 if err != nil { 309 return nil, err 310 } 311 312 defer resp.Body.Close() 313 314 ps := &EventsV1Subscription{} 315 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 316 return nil, err 317 } 318 319 return ps, err 320 }