github.com/twilio/twilio-go@v1.20.1/rest/accounts/v1/credentials_public_keys.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Accounts 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 'CreateCredentialPublicKey' 27 type CreateCredentialPublicKeyParams struct { 28 // A URL encoded representation of the public key. For example, `-----BEGIN PUBLIC KEY-----MIIBIjANB.pa9xQIDAQAB-----END PUBLIC KEY-----` 29 PublicKey *string `json:"PublicKey,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 // The SID of the Subaccount that this Credential should be associated with. Must be a valid Subaccount of the account issuing the request 33 AccountSid *string `json:"AccountSid,omitempty"` 34 } 35 36 func (params *CreateCredentialPublicKeyParams) SetPublicKey(PublicKey string) *CreateCredentialPublicKeyParams { 37 params.PublicKey = &PublicKey 38 return params 39 } 40 func (params *CreateCredentialPublicKeyParams) SetFriendlyName(FriendlyName string) *CreateCredentialPublicKeyParams { 41 params.FriendlyName = &FriendlyName 42 return params 43 } 44 func (params *CreateCredentialPublicKeyParams) SetAccountSid(AccountSid string) *CreateCredentialPublicKeyParams { 45 params.AccountSid = &AccountSid 46 return params 47 } 48 49 // Create a new Public Key Credential 50 func (c *ApiService) CreateCredentialPublicKey(params *CreateCredentialPublicKeyParams) (*AccountsV1CredentialPublicKey, error) { 51 path := "/v1/Credentials/PublicKeys" 52 53 data := url.Values{} 54 headers := make(map[string]interface{}) 55 56 if params != nil && params.PublicKey != nil { 57 data.Set("PublicKey", *params.PublicKey) 58 } 59 if params != nil && params.FriendlyName != nil { 60 data.Set("FriendlyName", *params.FriendlyName) 61 } 62 if params != nil && params.AccountSid != nil { 63 data.Set("AccountSid", *params.AccountSid) 64 } 65 66 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 67 if err != nil { 68 return nil, err 69 } 70 71 defer resp.Body.Close() 72 73 ps := &AccountsV1CredentialPublicKey{} 74 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 75 return nil, err 76 } 77 78 return ps, err 79 } 80 81 // Delete a Credential from your account 82 func (c *ApiService) DeleteCredentialPublicKey(Sid string) error { 83 path := "/v1/Credentials/PublicKeys/{Sid}" 84 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 85 86 data := url.Values{} 87 headers := make(map[string]interface{}) 88 89 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 90 if err != nil { 91 return err 92 } 93 94 defer resp.Body.Close() 95 96 return nil 97 } 98 99 // Fetch the public key specified by the provided Credential Sid 100 func (c *ApiService) FetchCredentialPublicKey(Sid string) (*AccountsV1CredentialPublicKey, error) { 101 path := "/v1/Credentials/PublicKeys/{Sid}" 102 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 103 104 data := url.Values{} 105 headers := make(map[string]interface{}) 106 107 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 108 if err != nil { 109 return nil, err 110 } 111 112 defer resp.Body.Close() 113 114 ps := &AccountsV1CredentialPublicKey{} 115 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 116 return nil, err 117 } 118 119 return ps, err 120 } 121 122 // Optional parameters for the method 'ListCredentialPublicKey' 123 type ListCredentialPublicKeyParams struct { 124 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 125 PageSize *int `json:"PageSize,omitempty"` 126 // Max number of records to return. 127 Limit *int `json:"limit,omitempty"` 128 } 129 130 func (params *ListCredentialPublicKeyParams) SetPageSize(PageSize int) *ListCredentialPublicKeyParams { 131 params.PageSize = &PageSize 132 return params 133 } 134 func (params *ListCredentialPublicKeyParams) SetLimit(Limit int) *ListCredentialPublicKeyParams { 135 params.Limit = &Limit 136 return params 137 } 138 139 // Retrieve a single page of CredentialPublicKey records from the API. Request is executed immediately. 140 func (c *ApiService) PageCredentialPublicKey(params *ListCredentialPublicKeyParams, pageToken, pageNumber string) (*ListCredentialPublicKeyResponse, error) { 141 path := "/v1/Credentials/PublicKeys" 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 := &ListCredentialPublicKeyResponse{} 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 CredentialPublicKey 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) ListCredentialPublicKey(params *ListCredentialPublicKeyParams) ([]AccountsV1CredentialPublicKey, error) { 174 response, errors := c.StreamCredentialPublicKey(params) 175 176 records := make([]AccountsV1CredentialPublicKey, 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 CredentialPublicKey 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) StreamCredentialPublicKey(params *ListCredentialPublicKeyParams) (chan AccountsV1CredentialPublicKey, chan error) { 190 if params == nil { 191 params = &ListCredentialPublicKeyParams{} 192 } 193 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 194 195 recordChannel := make(chan AccountsV1CredentialPublicKey, 1) 196 errorChannel := make(chan error, 1) 197 198 response, err := c.PageCredentialPublicKey(params, "", "") 199 if err != nil { 200 errorChannel <- err 201 close(recordChannel) 202 close(errorChannel) 203 } else { 204 go c.streamCredentialPublicKey(response, params, recordChannel, errorChannel) 205 } 206 207 return recordChannel, errorChannel 208 } 209 210 func (c *ApiService) streamCredentialPublicKey(response *ListCredentialPublicKeyResponse, params *ListCredentialPublicKeyParams, recordChannel chan AccountsV1CredentialPublicKey, errorChannel chan error) { 211 curRecord := 1 212 213 for response != nil { 214 responseRecords := response.Credentials 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.getNextListCredentialPublicKeyResponse) 226 if err != nil { 227 errorChannel <- err 228 break 229 } else if record == nil { 230 break 231 } 232 233 response = record.(*ListCredentialPublicKeyResponse) 234 } 235 236 close(recordChannel) 237 close(errorChannel) 238 } 239 240 func (c *ApiService) getNextListCredentialPublicKeyResponse(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 := &ListCredentialPublicKeyResponse{} 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 'UpdateCredentialPublicKey' 259 type UpdateCredentialPublicKeyParams struct { 260 // A descriptive string that you create to describe the resource. It can be up to 64 characters long. 261 FriendlyName *string `json:"FriendlyName,omitempty"` 262 } 263 264 func (params *UpdateCredentialPublicKeyParams) SetFriendlyName(FriendlyName string) *UpdateCredentialPublicKeyParams { 265 params.FriendlyName = &FriendlyName 266 return params 267 } 268 269 // Modify the properties of a given Account 270 func (c *ApiService) UpdateCredentialPublicKey(Sid string, params *UpdateCredentialPublicKeyParams) (*AccountsV1CredentialPublicKey, error) { 271 path := "/v1/Credentials/PublicKeys/{Sid}" 272 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 273 274 data := url.Values{} 275 headers := make(map[string]interface{}) 276 277 if params != nil && params.FriendlyName != nil { 278 data.Set("FriendlyName", *params.FriendlyName) 279 } 280 281 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 282 if err != nil { 283 return nil, err 284 } 285 286 defer resp.Body.Close() 287 288 ps := &AccountsV1CredentialPublicKey{} 289 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 290 return nil, err 291 } 292 293 return ps, err 294 }