github.com/twilio/twilio-go@v1.20.1/rest/flex/v1/plugin_service_plugins_versions.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 'CreatePluginVersion' 27 type CreatePluginVersionParams struct { 28 // The Flex-Metadata HTTP request header 29 FlexMetadata *string `json:"Flex-Metadata,omitempty"` 30 // The Flex Plugin Version's version. 31 Version *string `json:"Version,omitempty"` 32 // The URL of the Flex Plugin Version bundle 33 PluginUrl *string `json:"PluginUrl,omitempty"` 34 // The changelog of the Flex Plugin Version. 35 Changelog *string `json:"Changelog,omitempty"` 36 // Whether this Flex Plugin Version requires authorization. 37 Private *bool `json:"Private,omitempty"` 38 // The version of Flex Plugins CLI used to create this plugin 39 CliVersion *string `json:"CliVersion,omitempty"` 40 // The validation status of the plugin, indicating whether it has been validated 41 ValidateStatus *string `json:"ValidateStatus,omitempty"` 42 } 43 44 func (params *CreatePluginVersionParams) SetFlexMetadata(FlexMetadata string) *CreatePluginVersionParams { 45 params.FlexMetadata = &FlexMetadata 46 return params 47 } 48 func (params *CreatePluginVersionParams) SetVersion(Version string) *CreatePluginVersionParams { 49 params.Version = &Version 50 return params 51 } 52 func (params *CreatePluginVersionParams) SetPluginUrl(PluginUrl string) *CreatePluginVersionParams { 53 params.PluginUrl = &PluginUrl 54 return params 55 } 56 func (params *CreatePluginVersionParams) SetChangelog(Changelog string) *CreatePluginVersionParams { 57 params.Changelog = &Changelog 58 return params 59 } 60 func (params *CreatePluginVersionParams) SetPrivate(Private bool) *CreatePluginVersionParams { 61 params.Private = &Private 62 return params 63 } 64 func (params *CreatePluginVersionParams) SetCliVersion(CliVersion string) *CreatePluginVersionParams { 65 params.CliVersion = &CliVersion 66 return params 67 } 68 func (params *CreatePluginVersionParams) SetValidateStatus(ValidateStatus string) *CreatePluginVersionParams { 69 params.ValidateStatus = &ValidateStatus 70 return params 71 } 72 73 // 74 func (c *ApiService) CreatePluginVersion(PluginSid string, params *CreatePluginVersionParams) (*FlexV1PluginVersion, error) { 75 path := "/v1/PluginService/Plugins/{PluginSid}/Versions" 76 path = strings.Replace(path, "{"+"PluginSid"+"}", PluginSid, -1) 77 78 data := url.Values{} 79 headers := make(map[string]interface{}) 80 81 if params != nil && params.Version != nil { 82 data.Set("Version", *params.Version) 83 } 84 if params != nil && params.PluginUrl != nil { 85 data.Set("PluginUrl", *params.PluginUrl) 86 } 87 if params != nil && params.Changelog != nil { 88 data.Set("Changelog", *params.Changelog) 89 } 90 if params != nil && params.Private != nil { 91 data.Set("Private", fmt.Sprint(*params.Private)) 92 } 93 if params != nil && params.CliVersion != nil { 94 data.Set("CliVersion", *params.CliVersion) 95 } 96 if params != nil && params.ValidateStatus != nil { 97 data.Set("ValidateStatus", *params.ValidateStatus) 98 } 99 100 if params != nil && params.FlexMetadata != nil { 101 headers["Flex-Metadata"] = *params.FlexMetadata 102 } 103 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 104 if err != nil { 105 return nil, err 106 } 107 108 defer resp.Body.Close() 109 110 ps := &FlexV1PluginVersion{} 111 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 112 return nil, err 113 } 114 115 return ps, err 116 } 117 118 // Optional parameters for the method 'FetchPluginVersion' 119 type FetchPluginVersionParams struct { 120 // The Flex-Metadata HTTP request header 121 FlexMetadata *string `json:"Flex-Metadata,omitempty"` 122 } 123 124 func (params *FetchPluginVersionParams) SetFlexMetadata(FlexMetadata string) *FetchPluginVersionParams { 125 params.FlexMetadata = &FlexMetadata 126 return params 127 } 128 129 // 130 func (c *ApiService) FetchPluginVersion(PluginSid string, Sid string, params *FetchPluginVersionParams) (*FlexV1PluginVersion, error) { 131 path := "/v1/PluginService/Plugins/{PluginSid}/Versions/{Sid}" 132 path = strings.Replace(path, "{"+"PluginSid"+"}", PluginSid, -1) 133 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 134 135 data := url.Values{} 136 headers := make(map[string]interface{}) 137 138 if params != nil && params.FlexMetadata != nil { 139 headers["Flex-Metadata"] = *params.FlexMetadata 140 } 141 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 142 if err != nil { 143 return nil, err 144 } 145 146 defer resp.Body.Close() 147 148 ps := &FlexV1PluginVersion{} 149 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 150 return nil, err 151 } 152 153 return ps, err 154 } 155 156 // Optional parameters for the method 'ListPluginVersion' 157 type ListPluginVersionParams struct { 158 // The Flex-Metadata HTTP request header 159 FlexMetadata *string `json:"Flex-Metadata,omitempty"` 160 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 161 PageSize *int `json:"PageSize,omitempty"` 162 // Max number of records to return. 163 Limit *int `json:"limit,omitempty"` 164 } 165 166 func (params *ListPluginVersionParams) SetFlexMetadata(FlexMetadata string) *ListPluginVersionParams { 167 params.FlexMetadata = &FlexMetadata 168 return params 169 } 170 func (params *ListPluginVersionParams) SetPageSize(PageSize int) *ListPluginVersionParams { 171 params.PageSize = &PageSize 172 return params 173 } 174 func (params *ListPluginVersionParams) SetLimit(Limit int) *ListPluginVersionParams { 175 params.Limit = &Limit 176 return params 177 } 178 179 // Retrieve a single page of PluginVersion records from the API. Request is executed immediately. 180 func (c *ApiService) PagePluginVersion(PluginSid string, params *ListPluginVersionParams, pageToken, pageNumber string) (*ListPluginVersionResponse, error) { 181 path := "/v1/PluginService/Plugins/{PluginSid}/Versions" 182 183 path = strings.Replace(path, "{"+"PluginSid"+"}", PluginSid, -1) 184 185 data := url.Values{} 186 headers := make(map[string]interface{}) 187 188 if params != nil && params.PageSize != nil { 189 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 190 } 191 192 if pageToken != "" { 193 data.Set("PageToken", pageToken) 194 } 195 if pageNumber != "" { 196 data.Set("Page", pageNumber) 197 } 198 199 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 200 if err != nil { 201 return nil, err 202 } 203 204 defer resp.Body.Close() 205 206 ps := &ListPluginVersionResponse{} 207 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 208 return nil, err 209 } 210 211 return ps, err 212 } 213 214 // Lists PluginVersion records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 215 func (c *ApiService) ListPluginVersion(PluginSid string, params *ListPluginVersionParams) ([]FlexV1PluginVersion, error) { 216 response, errors := c.StreamPluginVersion(PluginSid, params) 217 218 records := make([]FlexV1PluginVersion, 0) 219 for record := range response { 220 records = append(records, record) 221 } 222 223 if err := <-errors; err != nil { 224 return nil, err 225 } 226 227 return records, nil 228 } 229 230 // Streams PluginVersion records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 231 func (c *ApiService) StreamPluginVersion(PluginSid string, params *ListPluginVersionParams) (chan FlexV1PluginVersion, chan error) { 232 if params == nil { 233 params = &ListPluginVersionParams{} 234 } 235 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 236 237 recordChannel := make(chan FlexV1PluginVersion, 1) 238 errorChannel := make(chan error, 1) 239 240 response, err := c.PagePluginVersion(PluginSid, params, "", "") 241 if err != nil { 242 errorChannel <- err 243 close(recordChannel) 244 close(errorChannel) 245 } else { 246 go c.streamPluginVersion(response, params, recordChannel, errorChannel) 247 } 248 249 return recordChannel, errorChannel 250 } 251 252 func (c *ApiService) streamPluginVersion(response *ListPluginVersionResponse, params *ListPluginVersionParams, recordChannel chan FlexV1PluginVersion, errorChannel chan error) { 253 curRecord := 1 254 255 for response != nil { 256 responseRecords := response.PluginVersions 257 for item := range responseRecords { 258 recordChannel <- responseRecords[item] 259 curRecord += 1 260 if params.Limit != nil && *params.Limit < curRecord { 261 close(recordChannel) 262 close(errorChannel) 263 return 264 } 265 } 266 267 record, err := client.GetNext(c.baseURL, response, c.getNextListPluginVersionResponse) 268 if err != nil { 269 errorChannel <- err 270 break 271 } else if record == nil { 272 break 273 } 274 275 response = record.(*ListPluginVersionResponse) 276 } 277 278 close(recordChannel) 279 close(errorChannel) 280 } 281 282 func (c *ApiService) getNextListPluginVersionResponse(nextPageUrl string) (interface{}, error) { 283 if nextPageUrl == "" { 284 return nil, nil 285 } 286 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 287 if err != nil { 288 return nil, err 289 } 290 291 defer resp.Body.Close() 292 293 ps := &ListPluginVersionResponse{} 294 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 295 return nil, err 296 } 297 return ps, nil 298 }