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