github.com/twilio/twilio-go@v1.20.1/rest/microvisor/v1/devices_configs.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Microvisor 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 'CreateDeviceConfig' 27 type CreateDeviceConfigParams struct { 28 // The config key; up to 100 characters. 29 Key *string `json:"Key,omitempty"` 30 // The config value; up to 4096 characters. 31 Value *string `json:"Value,omitempty"` 32 } 33 34 func (params *CreateDeviceConfigParams) SetKey(Key string) *CreateDeviceConfigParams { 35 params.Key = &Key 36 return params 37 } 38 func (params *CreateDeviceConfigParams) SetValue(Value string) *CreateDeviceConfigParams { 39 params.Value = &Value 40 return params 41 } 42 43 // Create a config for a Microvisor Device. 44 func (c *ApiService) CreateDeviceConfig(DeviceSid string, params *CreateDeviceConfigParams) (*MicrovisorV1DeviceConfig, error) { 45 path := "/v1/Devices/{DeviceSid}/Configs" 46 path = strings.Replace(path, "{"+"DeviceSid"+"}", DeviceSid, -1) 47 48 data := url.Values{} 49 headers := make(map[string]interface{}) 50 51 if params != nil && params.Key != nil { 52 data.Set("Key", *params.Key) 53 } 54 if params != nil && params.Value != nil { 55 data.Set("Value", *params.Value) 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 := &MicrovisorV1DeviceConfig{} 66 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 67 return nil, err 68 } 69 70 return ps, err 71 } 72 73 // Delete a config for a Microvisor Device. 74 func (c *ApiService) DeleteDeviceConfig(DeviceSid string, Key string) error { 75 path := "/v1/Devices/{DeviceSid}/Configs/{Key}" 76 path = strings.Replace(path, "{"+"DeviceSid"+"}", DeviceSid, -1) 77 path = strings.Replace(path, "{"+"Key"+"}", Key, -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 // Retrieve a Config for a Device. 93 func (c *ApiService) FetchDeviceConfig(DeviceSid string, Key string) (*MicrovisorV1DeviceConfig, error) { 94 path := "/v1/Devices/{DeviceSid}/Configs/{Key}" 95 path = strings.Replace(path, "{"+"DeviceSid"+"}", DeviceSid, -1) 96 path = strings.Replace(path, "{"+"Key"+"}", Key, -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 := &MicrovisorV1DeviceConfig{} 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 'ListDeviceConfig' 117 type ListDeviceConfigParams 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 *ListDeviceConfigParams) SetPageSize(PageSize int) *ListDeviceConfigParams { 125 params.PageSize = &PageSize 126 return params 127 } 128 func (params *ListDeviceConfigParams) SetLimit(Limit int) *ListDeviceConfigParams { 129 params.Limit = &Limit 130 return params 131 } 132 133 // Retrieve a single page of DeviceConfig records from the API. Request is executed immediately. 134 func (c *ApiService) PageDeviceConfig(DeviceSid string, params *ListDeviceConfigParams, pageToken, pageNumber string) (*ListDeviceConfigResponse, error) { 135 path := "/v1/Devices/{DeviceSid}/Configs" 136 137 path = strings.Replace(path, "{"+"DeviceSid"+"}", DeviceSid, -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 := &ListDeviceConfigResponse{} 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 DeviceConfig 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) ListDeviceConfig(DeviceSid string, params *ListDeviceConfigParams) ([]MicrovisorV1DeviceConfig, error) { 170 response, errors := c.StreamDeviceConfig(DeviceSid, params) 171 172 records := make([]MicrovisorV1DeviceConfig, 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 DeviceConfig 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) StreamDeviceConfig(DeviceSid string, params *ListDeviceConfigParams) (chan MicrovisorV1DeviceConfig, chan error) { 186 if params == nil { 187 params = &ListDeviceConfigParams{} 188 } 189 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 190 191 recordChannel := make(chan MicrovisorV1DeviceConfig, 1) 192 errorChannel := make(chan error, 1) 193 194 response, err := c.PageDeviceConfig(DeviceSid, params, "", "") 195 if err != nil { 196 errorChannel <- err 197 close(recordChannel) 198 close(errorChannel) 199 } else { 200 go c.streamDeviceConfig(response, params, recordChannel, errorChannel) 201 } 202 203 return recordChannel, errorChannel 204 } 205 206 func (c *ApiService) streamDeviceConfig(response *ListDeviceConfigResponse, params *ListDeviceConfigParams, recordChannel chan MicrovisorV1DeviceConfig, errorChannel chan error) { 207 curRecord := 1 208 209 for response != nil { 210 responseRecords := response.Configs 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.getNextListDeviceConfigResponse) 222 if err != nil { 223 errorChannel <- err 224 break 225 } else if record == nil { 226 break 227 } 228 229 response = record.(*ListDeviceConfigResponse) 230 } 231 232 close(recordChannel) 233 close(errorChannel) 234 } 235 236 func (c *ApiService) getNextListDeviceConfigResponse(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 := &ListDeviceConfigResponse{} 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 'UpdateDeviceConfig' 255 type UpdateDeviceConfigParams struct { 256 // The config value; up to 4096 characters. 257 Value *string `json:"Value,omitempty"` 258 } 259 260 func (params *UpdateDeviceConfigParams) SetValue(Value string) *UpdateDeviceConfigParams { 261 params.Value = &Value 262 return params 263 } 264 265 // Update a config for a Microvisor Device. 266 func (c *ApiService) UpdateDeviceConfig(DeviceSid string, Key string, params *UpdateDeviceConfigParams) (*MicrovisorV1DeviceConfig, error) { 267 path := "/v1/Devices/{DeviceSid}/Configs/{Key}" 268 path = strings.Replace(path, "{"+"DeviceSid"+"}", DeviceSid, -1) 269 path = strings.Replace(path, "{"+"Key"+"}", Key, -1) 270 271 data := url.Values{} 272 headers := make(map[string]interface{}) 273 274 if params != nil && params.Value != nil { 275 data.Set("Value", *params.Value) 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 := &MicrovisorV1DeviceConfig{} 286 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 287 return nil, err 288 } 289 290 return ps, err 291 }