github.com/twilio/twilio-go@v1.20.1/rest/flex/v1/plugin_service_configurations.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 'CreatePluginConfiguration' 27 type CreatePluginConfigurationParams struct { 28 // The Flex-Metadata HTTP request header 29 FlexMetadata *string `json:"Flex-Metadata,omitempty"` 30 // The Flex Plugin Configuration's name. 31 Name *string `json:"Name,omitempty"` 32 // A list of objects that describe the plugin versions included in the configuration. Each object contains the sid of the plugin version. 33 Plugins *[]interface{} `json:"Plugins,omitempty"` 34 // The Flex Plugin Configuration's description. 35 Description *string `json:"Description,omitempty"` 36 } 37 38 func (params *CreatePluginConfigurationParams) SetFlexMetadata(FlexMetadata string) *CreatePluginConfigurationParams { 39 params.FlexMetadata = &FlexMetadata 40 return params 41 } 42 func (params *CreatePluginConfigurationParams) SetName(Name string) *CreatePluginConfigurationParams { 43 params.Name = &Name 44 return params 45 } 46 func (params *CreatePluginConfigurationParams) SetPlugins(Plugins []interface{}) *CreatePluginConfigurationParams { 47 params.Plugins = &Plugins 48 return params 49 } 50 func (params *CreatePluginConfigurationParams) SetDescription(Description string) *CreatePluginConfigurationParams { 51 params.Description = &Description 52 return params 53 } 54 55 // 56 func (c *ApiService) CreatePluginConfiguration(params *CreatePluginConfigurationParams) (*FlexV1PluginConfiguration, error) { 57 path := "/v1/PluginService/Configurations" 58 59 data := url.Values{} 60 headers := make(map[string]interface{}) 61 62 if params != nil && params.Name != nil { 63 data.Set("Name", *params.Name) 64 } 65 if params != nil && params.Plugins != nil { 66 for _, item := range *params.Plugins { 67 v, err := json.Marshal(item) 68 69 if err != nil { 70 return nil, err 71 } 72 73 data.Add("Plugins", string(v)) 74 } 75 } 76 if params != nil && params.Description != nil { 77 data.Set("Description", *params.Description) 78 } 79 80 if params != nil && params.FlexMetadata != nil { 81 headers["Flex-Metadata"] = *params.FlexMetadata 82 } 83 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 84 if err != nil { 85 return nil, err 86 } 87 88 defer resp.Body.Close() 89 90 ps := &FlexV1PluginConfiguration{} 91 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 92 return nil, err 93 } 94 95 return ps, err 96 } 97 98 // Optional parameters for the method 'FetchPluginConfiguration' 99 type FetchPluginConfigurationParams struct { 100 // The Flex-Metadata HTTP request header 101 FlexMetadata *string `json:"Flex-Metadata,omitempty"` 102 } 103 104 func (params *FetchPluginConfigurationParams) SetFlexMetadata(FlexMetadata string) *FetchPluginConfigurationParams { 105 params.FlexMetadata = &FlexMetadata 106 return params 107 } 108 109 // 110 func (c *ApiService) FetchPluginConfiguration(Sid string, params *FetchPluginConfigurationParams) (*FlexV1PluginConfiguration, error) { 111 path := "/v1/PluginService/Configurations/{Sid}" 112 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 113 114 data := url.Values{} 115 headers := make(map[string]interface{}) 116 117 if params != nil && params.FlexMetadata != nil { 118 headers["Flex-Metadata"] = *params.FlexMetadata 119 } 120 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 121 if err != nil { 122 return nil, err 123 } 124 125 defer resp.Body.Close() 126 127 ps := &FlexV1PluginConfiguration{} 128 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 129 return nil, err 130 } 131 132 return ps, err 133 } 134 135 // Optional parameters for the method 'ListPluginConfiguration' 136 type ListPluginConfigurationParams struct { 137 // The Flex-Metadata HTTP request header 138 FlexMetadata *string `json:"Flex-Metadata,omitempty"` 139 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 140 PageSize *int `json:"PageSize,omitempty"` 141 // Max number of records to return. 142 Limit *int `json:"limit,omitempty"` 143 } 144 145 func (params *ListPluginConfigurationParams) SetFlexMetadata(FlexMetadata string) *ListPluginConfigurationParams { 146 params.FlexMetadata = &FlexMetadata 147 return params 148 } 149 func (params *ListPluginConfigurationParams) SetPageSize(PageSize int) *ListPluginConfigurationParams { 150 params.PageSize = &PageSize 151 return params 152 } 153 func (params *ListPluginConfigurationParams) SetLimit(Limit int) *ListPluginConfigurationParams { 154 params.Limit = &Limit 155 return params 156 } 157 158 // Retrieve a single page of PluginConfiguration records from the API. Request is executed immediately. 159 func (c *ApiService) PagePluginConfiguration(params *ListPluginConfigurationParams, pageToken, pageNumber string) (*ListPluginConfigurationResponse, error) { 160 path := "/v1/PluginService/Configurations" 161 162 data := url.Values{} 163 headers := make(map[string]interface{}) 164 165 if params != nil && params.PageSize != nil { 166 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 167 } 168 169 if pageToken != "" { 170 data.Set("PageToken", pageToken) 171 } 172 if pageNumber != "" { 173 data.Set("Page", pageNumber) 174 } 175 176 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 177 if err != nil { 178 return nil, err 179 } 180 181 defer resp.Body.Close() 182 183 ps := &ListPluginConfigurationResponse{} 184 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 185 return nil, err 186 } 187 188 return ps, err 189 } 190 191 // Lists PluginConfiguration records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 192 func (c *ApiService) ListPluginConfiguration(params *ListPluginConfigurationParams) ([]FlexV1PluginConfiguration, error) { 193 response, errors := c.StreamPluginConfiguration(params) 194 195 records := make([]FlexV1PluginConfiguration, 0) 196 for record := range response { 197 records = append(records, record) 198 } 199 200 if err := <-errors; err != nil { 201 return nil, err 202 } 203 204 return records, nil 205 } 206 207 // Streams PluginConfiguration records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 208 func (c *ApiService) StreamPluginConfiguration(params *ListPluginConfigurationParams) (chan FlexV1PluginConfiguration, chan error) { 209 if params == nil { 210 params = &ListPluginConfigurationParams{} 211 } 212 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 213 214 recordChannel := make(chan FlexV1PluginConfiguration, 1) 215 errorChannel := make(chan error, 1) 216 217 response, err := c.PagePluginConfiguration(params, "", "") 218 if err != nil { 219 errorChannel <- err 220 close(recordChannel) 221 close(errorChannel) 222 } else { 223 go c.streamPluginConfiguration(response, params, recordChannel, errorChannel) 224 } 225 226 return recordChannel, errorChannel 227 } 228 229 func (c *ApiService) streamPluginConfiguration(response *ListPluginConfigurationResponse, params *ListPluginConfigurationParams, recordChannel chan FlexV1PluginConfiguration, errorChannel chan error) { 230 curRecord := 1 231 232 for response != nil { 233 responseRecords := response.Configurations 234 for item := range responseRecords { 235 recordChannel <- responseRecords[item] 236 curRecord += 1 237 if params.Limit != nil && *params.Limit < curRecord { 238 close(recordChannel) 239 close(errorChannel) 240 return 241 } 242 } 243 244 record, err := client.GetNext(c.baseURL, response, c.getNextListPluginConfigurationResponse) 245 if err != nil { 246 errorChannel <- err 247 break 248 } else if record == nil { 249 break 250 } 251 252 response = record.(*ListPluginConfigurationResponse) 253 } 254 255 close(recordChannel) 256 close(errorChannel) 257 } 258 259 func (c *ApiService) getNextListPluginConfigurationResponse(nextPageUrl string) (interface{}, error) { 260 if nextPageUrl == "" { 261 return nil, nil 262 } 263 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 264 if err != nil { 265 return nil, err 266 } 267 268 defer resp.Body.Close() 269 270 ps := &ListPluginConfigurationResponse{} 271 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 272 return nil, err 273 } 274 return ps, nil 275 }