github.com/twilio/twilio-go@v1.20.1/rest/events/v1/subscriptions_subscribed_events.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 'CreateSubscribedEvent' 27 type CreateSubscribedEventParams struct { 28 // Type of event being subscribed to. 29 Type *string `json:"Type,omitempty"` 30 // The schema version that the Subscription should use. 31 SchemaVersion *int `json:"SchemaVersion,omitempty"` 32 } 33 34 func (params *CreateSubscribedEventParams) SetType(Type string) *CreateSubscribedEventParams { 35 params.Type = &Type 36 return params 37 } 38 func (params *CreateSubscribedEventParams) SetSchemaVersion(SchemaVersion int) *CreateSubscribedEventParams { 39 params.SchemaVersion = &SchemaVersion 40 return params 41 } 42 43 // Add an event type to a Subscription. 44 func (c *ApiService) CreateSubscribedEvent(SubscriptionSid string, params *CreateSubscribedEventParams) (*EventsV1SubscribedEvent, error) { 45 path := "/v1/Subscriptions/{SubscriptionSid}/SubscribedEvents" 46 path = strings.Replace(path, "{"+"SubscriptionSid"+"}", SubscriptionSid, -1) 47 48 data := url.Values{} 49 headers := make(map[string]interface{}) 50 51 if params != nil && params.Type != nil { 52 data.Set("Type", *params.Type) 53 } 54 if params != nil && params.SchemaVersion != nil { 55 data.Set("SchemaVersion", fmt.Sprint(*params.SchemaVersion)) 56 } 57 58 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 59 if err != nil { 60 return nil, err 61 } 62 63 defer resp.Body.Close() 64 65 ps := &EventsV1SubscribedEvent{} 66 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 67 return nil, err 68 } 69 70 return ps, err 71 } 72 73 // Remove an event type from a Subscription. 74 func (c *ApiService) DeleteSubscribedEvent(SubscriptionSid string, Type string) error { 75 path := "/v1/Subscriptions/{SubscriptionSid}/SubscribedEvents/{Type}" 76 path = strings.Replace(path, "{"+"SubscriptionSid"+"}", SubscriptionSid, -1) 77 path = strings.Replace(path, "{"+"Type"+"}", Type, -1) 78 79 data := url.Values{} 80 headers := make(map[string]interface{}) 81 82 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 83 if err != nil { 84 return err 85 } 86 87 defer resp.Body.Close() 88 89 return nil 90 } 91 92 // Read an Event for a Subscription. 93 func (c *ApiService) FetchSubscribedEvent(SubscriptionSid string, Type string) (*EventsV1SubscribedEvent, error) { 94 path := "/v1/Subscriptions/{SubscriptionSid}/SubscribedEvents/{Type}" 95 path = strings.Replace(path, "{"+"SubscriptionSid"+"}", SubscriptionSid, -1) 96 path = strings.Replace(path, "{"+"Type"+"}", Type, -1) 97 98 data := url.Values{} 99 headers := make(map[string]interface{}) 100 101 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 102 if err != nil { 103 return nil, err 104 } 105 106 defer resp.Body.Close() 107 108 ps := &EventsV1SubscribedEvent{} 109 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 110 return nil, err 111 } 112 113 return ps, err 114 } 115 116 // Optional parameters for the method 'ListSubscribedEvent' 117 type ListSubscribedEventParams struct { 118 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 119 PageSize *int `json:"PageSize,omitempty"` 120 // Max number of records to return. 121 Limit *int `json:"limit,omitempty"` 122 } 123 124 func (params *ListSubscribedEventParams) SetPageSize(PageSize int) *ListSubscribedEventParams { 125 params.PageSize = &PageSize 126 return params 127 } 128 func (params *ListSubscribedEventParams) SetLimit(Limit int) *ListSubscribedEventParams { 129 params.Limit = &Limit 130 return params 131 } 132 133 // Retrieve a single page of SubscribedEvent records from the API. Request is executed immediately. 134 func (c *ApiService) PageSubscribedEvent(SubscriptionSid string, params *ListSubscribedEventParams, pageToken, pageNumber string) (*ListSubscribedEventResponse, error) { 135 path := "/v1/Subscriptions/{SubscriptionSid}/SubscribedEvents" 136 137 path = strings.Replace(path, "{"+"SubscriptionSid"+"}", SubscriptionSid, -1) 138 139 data := url.Values{} 140 headers := make(map[string]interface{}) 141 142 if params != nil && params.PageSize != nil { 143 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 144 } 145 146 if pageToken != "" { 147 data.Set("PageToken", pageToken) 148 } 149 if pageNumber != "" { 150 data.Set("Page", pageNumber) 151 } 152 153 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 154 if err != nil { 155 return nil, err 156 } 157 158 defer resp.Body.Close() 159 160 ps := &ListSubscribedEventResponse{} 161 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 162 return nil, err 163 } 164 165 return ps, err 166 } 167 168 // Lists SubscribedEvent records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 169 func (c *ApiService) ListSubscribedEvent(SubscriptionSid string, params *ListSubscribedEventParams) ([]EventsV1SubscribedEvent, error) { 170 response, errors := c.StreamSubscribedEvent(SubscriptionSid, params) 171 172 records := make([]EventsV1SubscribedEvent, 0) 173 for record := range response { 174 records = append(records, record) 175 } 176 177 if err := <-errors; err != nil { 178 return nil, err 179 } 180 181 return records, nil 182 } 183 184 // Streams SubscribedEvent records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 185 func (c *ApiService) StreamSubscribedEvent(SubscriptionSid string, params *ListSubscribedEventParams) (chan EventsV1SubscribedEvent, chan error) { 186 if params == nil { 187 params = &ListSubscribedEventParams{} 188 } 189 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 190 191 recordChannel := make(chan EventsV1SubscribedEvent, 1) 192 errorChannel := make(chan error, 1) 193 194 response, err := c.PageSubscribedEvent(SubscriptionSid, params, "", "") 195 if err != nil { 196 errorChannel <- err 197 close(recordChannel) 198 close(errorChannel) 199 } else { 200 go c.streamSubscribedEvent(response, params, recordChannel, errorChannel) 201 } 202 203 return recordChannel, errorChannel 204 } 205 206 func (c *ApiService) streamSubscribedEvent(response *ListSubscribedEventResponse, params *ListSubscribedEventParams, recordChannel chan EventsV1SubscribedEvent, errorChannel chan error) { 207 curRecord := 1 208 209 for response != nil { 210 responseRecords := response.Types 211 for item := range responseRecords { 212 recordChannel <- responseRecords[item] 213 curRecord += 1 214 if params.Limit != nil && *params.Limit < curRecord { 215 close(recordChannel) 216 close(errorChannel) 217 return 218 } 219 } 220 221 record, err := client.GetNext(c.baseURL, response, c.getNextListSubscribedEventResponse) 222 if err != nil { 223 errorChannel <- err 224 break 225 } else if record == nil { 226 break 227 } 228 229 response = record.(*ListSubscribedEventResponse) 230 } 231 232 close(recordChannel) 233 close(errorChannel) 234 } 235 236 func (c *ApiService) getNextListSubscribedEventResponse(nextPageUrl string) (interface{}, error) { 237 if nextPageUrl == "" { 238 return nil, nil 239 } 240 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 241 if err != nil { 242 return nil, err 243 } 244 245 defer resp.Body.Close() 246 247 ps := &ListSubscribedEventResponse{} 248 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 249 return nil, err 250 } 251 return ps, nil 252 } 253 254 // Optional parameters for the method 'UpdateSubscribedEvent' 255 type UpdateSubscribedEventParams struct { 256 // The schema version that the Subscription should use. 257 SchemaVersion *int `json:"SchemaVersion,omitempty"` 258 } 259 260 func (params *UpdateSubscribedEventParams) SetSchemaVersion(SchemaVersion int) *UpdateSubscribedEventParams { 261 params.SchemaVersion = &SchemaVersion 262 return params 263 } 264 265 // Update an Event for a Subscription. 266 func (c *ApiService) UpdateSubscribedEvent(SubscriptionSid string, Type string, params *UpdateSubscribedEventParams) (*EventsV1SubscribedEvent, error) { 267 path := "/v1/Subscriptions/{SubscriptionSid}/SubscribedEvents/{Type}" 268 path = strings.Replace(path, "{"+"SubscriptionSid"+"}", SubscriptionSid, -1) 269 path = strings.Replace(path, "{"+"Type"+"}", Type, -1) 270 271 data := url.Values{} 272 headers := make(map[string]interface{}) 273 274 if params != nil && params.SchemaVersion != nil { 275 data.Set("SchemaVersion", fmt.Sprint(*params.SchemaVersion)) 276 } 277 278 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 279 if err != nil { 280 return nil, err 281 } 282 283 defer resp.Body.Close() 284 285 ps := &EventsV1SubscribedEvent{} 286 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 287 return nil, err 288 } 289 290 return ps, err 291 }