github.com/twilio/twilio-go@v1.20.1/rest/flex/v1/plugin_service_plugins.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Flex 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 'CreatePlugin' 27 type CreatePluginParams struct { 28 // The Flex-Metadata HTTP request header 29 FlexMetadata *string `json:"Flex-Metadata,omitempty"` 30 // The Flex Plugin's unique name. 31 UniqueName *string `json:"UniqueName,omitempty"` 32 // The Flex Plugin's friendly name. 33 FriendlyName *string `json:"FriendlyName,omitempty"` 34 // A descriptive string that you create to describe the plugin resource. It can be up to 500 characters long 35 Description *string `json:"Description,omitempty"` 36 } 37 38 func (params *CreatePluginParams) SetFlexMetadata(FlexMetadata string) *CreatePluginParams { 39 params.FlexMetadata = &FlexMetadata 40 return params 41 } 42 func (params *CreatePluginParams) SetUniqueName(UniqueName string) *CreatePluginParams { 43 params.UniqueName = &UniqueName 44 return params 45 } 46 func (params *CreatePluginParams) SetFriendlyName(FriendlyName string) *CreatePluginParams { 47 params.FriendlyName = &FriendlyName 48 return params 49 } 50 func (params *CreatePluginParams) SetDescription(Description string) *CreatePluginParams { 51 params.Description = &Description 52 return params 53 } 54 55 // 56 func (c *ApiService) CreatePlugin(params *CreatePluginParams) (*FlexV1Plugin, error) { 57 path := "/v1/PluginService/Plugins" 58 59 data := url.Values{} 60 headers := make(map[string]interface{}) 61 62 if params != nil && params.UniqueName != nil { 63 data.Set("UniqueName", *params.UniqueName) 64 } 65 if params != nil && params.FriendlyName != nil { 66 data.Set("FriendlyName", *params.FriendlyName) 67 } 68 if params != nil && params.Description != nil { 69 data.Set("Description", *params.Description) 70 } 71 72 if params != nil && params.FlexMetadata != nil { 73 headers["Flex-Metadata"] = *params.FlexMetadata 74 } 75 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 76 if err != nil { 77 return nil, err 78 } 79 80 defer resp.Body.Close() 81 82 ps := &FlexV1Plugin{} 83 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 84 return nil, err 85 } 86 87 return ps, err 88 } 89 90 // Optional parameters for the method 'FetchPlugin' 91 type FetchPluginParams struct { 92 // The Flex-Metadata HTTP request header 93 FlexMetadata *string `json:"Flex-Metadata,omitempty"` 94 } 95 96 func (params *FetchPluginParams) SetFlexMetadata(FlexMetadata string) *FetchPluginParams { 97 params.FlexMetadata = &FlexMetadata 98 return params 99 } 100 101 // 102 func (c *ApiService) FetchPlugin(Sid string, params *FetchPluginParams) (*FlexV1Plugin, error) { 103 path := "/v1/PluginService/Plugins/{Sid}" 104 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 105 106 data := url.Values{} 107 headers := make(map[string]interface{}) 108 109 if params != nil && params.FlexMetadata != nil { 110 headers["Flex-Metadata"] = *params.FlexMetadata 111 } 112 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 113 if err != nil { 114 return nil, err 115 } 116 117 defer resp.Body.Close() 118 119 ps := &FlexV1Plugin{} 120 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 121 return nil, err 122 } 123 124 return ps, err 125 } 126 127 // Optional parameters for the method 'ListPlugin' 128 type ListPluginParams struct { 129 // The Flex-Metadata HTTP request header 130 FlexMetadata *string `json:"Flex-Metadata,omitempty"` 131 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 132 PageSize *int `json:"PageSize,omitempty"` 133 // Max number of records to return. 134 Limit *int `json:"limit,omitempty"` 135 } 136 137 func (params *ListPluginParams) SetFlexMetadata(FlexMetadata string) *ListPluginParams { 138 params.FlexMetadata = &FlexMetadata 139 return params 140 } 141 func (params *ListPluginParams) SetPageSize(PageSize int) *ListPluginParams { 142 params.PageSize = &PageSize 143 return params 144 } 145 func (params *ListPluginParams) SetLimit(Limit int) *ListPluginParams { 146 params.Limit = &Limit 147 return params 148 } 149 150 // Retrieve a single page of Plugin records from the API. Request is executed immediately. 151 func (c *ApiService) PagePlugin(params *ListPluginParams, pageToken, pageNumber string) (*ListPluginResponse, error) { 152 path := "/v1/PluginService/Plugins" 153 154 data := url.Values{} 155 headers := make(map[string]interface{}) 156 157 if params != nil && params.PageSize != nil { 158 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 159 } 160 161 if pageToken != "" { 162 data.Set("PageToken", pageToken) 163 } 164 if pageNumber != "" { 165 data.Set("Page", pageNumber) 166 } 167 168 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 169 if err != nil { 170 return nil, err 171 } 172 173 defer resp.Body.Close() 174 175 ps := &ListPluginResponse{} 176 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 177 return nil, err 178 } 179 180 return ps, err 181 } 182 183 // Lists Plugin records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 184 func (c *ApiService) ListPlugin(params *ListPluginParams) ([]FlexV1Plugin, error) { 185 response, errors := c.StreamPlugin(params) 186 187 records := make([]FlexV1Plugin, 0) 188 for record := range response { 189 records = append(records, record) 190 } 191 192 if err := <-errors; err != nil { 193 return nil, err 194 } 195 196 return records, nil 197 } 198 199 // Streams Plugin records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 200 func (c *ApiService) StreamPlugin(params *ListPluginParams) (chan FlexV1Plugin, chan error) { 201 if params == nil { 202 params = &ListPluginParams{} 203 } 204 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 205 206 recordChannel := make(chan FlexV1Plugin, 1) 207 errorChannel := make(chan error, 1) 208 209 response, err := c.PagePlugin(params, "", "") 210 if err != nil { 211 errorChannel <- err 212 close(recordChannel) 213 close(errorChannel) 214 } else { 215 go c.streamPlugin(response, params, recordChannel, errorChannel) 216 } 217 218 return recordChannel, errorChannel 219 } 220 221 func (c *ApiService) streamPlugin(response *ListPluginResponse, params *ListPluginParams, recordChannel chan FlexV1Plugin, errorChannel chan error) { 222 curRecord := 1 223 224 for response != nil { 225 responseRecords := response.Plugins 226 for item := range responseRecords { 227 recordChannel <- responseRecords[item] 228 curRecord += 1 229 if params.Limit != nil && *params.Limit < curRecord { 230 close(recordChannel) 231 close(errorChannel) 232 return 233 } 234 } 235 236 record, err := client.GetNext(c.baseURL, response, c.getNextListPluginResponse) 237 if err != nil { 238 errorChannel <- err 239 break 240 } else if record == nil { 241 break 242 } 243 244 response = record.(*ListPluginResponse) 245 } 246 247 close(recordChannel) 248 close(errorChannel) 249 } 250 251 func (c *ApiService) getNextListPluginResponse(nextPageUrl string) (interface{}, error) { 252 if nextPageUrl == "" { 253 return nil, nil 254 } 255 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 256 if err != nil { 257 return nil, err 258 } 259 260 defer resp.Body.Close() 261 262 ps := &ListPluginResponse{} 263 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 264 return nil, err 265 } 266 return ps, nil 267 } 268 269 // Optional parameters for the method 'UpdatePlugin' 270 type UpdatePluginParams struct { 271 // The Flex-Metadata HTTP request header 272 FlexMetadata *string `json:"Flex-Metadata,omitempty"` 273 // The Flex Plugin's friendly name. 274 FriendlyName *string `json:"FriendlyName,omitempty"` 275 // A descriptive string that you update to describe the plugin resource. It can be up to 500 characters long 276 Description *string `json:"Description,omitempty"` 277 } 278 279 func (params *UpdatePluginParams) SetFlexMetadata(FlexMetadata string) *UpdatePluginParams { 280 params.FlexMetadata = &FlexMetadata 281 return params 282 } 283 func (params *UpdatePluginParams) SetFriendlyName(FriendlyName string) *UpdatePluginParams { 284 params.FriendlyName = &FriendlyName 285 return params 286 } 287 func (params *UpdatePluginParams) SetDescription(Description string) *UpdatePluginParams { 288 params.Description = &Description 289 return params 290 } 291 292 // 293 func (c *ApiService) UpdatePlugin(Sid string, params *UpdatePluginParams) (*FlexV1Plugin, error) { 294 path := "/v1/PluginService/Plugins/{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.FriendlyName != nil { 301 data.Set("FriendlyName", *params.FriendlyName) 302 } 303 if params != nil && params.Description != nil { 304 data.Set("Description", *params.Description) 305 } 306 307 if params != nil && params.FlexMetadata != nil { 308 headers["Flex-Metadata"] = *params.FlexMetadata 309 } 310 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 311 if err != nil { 312 return nil, err 313 } 314 315 defer resp.Body.Close() 316 317 ps := &FlexV1Plugin{} 318 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 319 return nil, err 320 } 321 322 return ps, err 323 }