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