github.com/twilio/twilio-go@v1.20.1/rest/api/v2010/accounts.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 'CreateAccount' 27 type CreateAccountParams struct { 28 // A human readable description of the account to create, defaults to `SubAccount Created at {YYYY-MM-DD HH:MM meridian}` 29 FriendlyName *string `json:"FriendlyName,omitempty"` 30 } 31 32 func (params *CreateAccountParams) SetFriendlyName(FriendlyName string) *CreateAccountParams { 33 params.FriendlyName = &FriendlyName 34 return params 35 } 36 37 // Create a new Twilio Subaccount from the account making the request 38 func (c *ApiService) CreateAccount(params *CreateAccountParams) (*ApiV2010Account, error) { 39 path := "/2010-04-01/Accounts.json" 40 41 data := url.Values{} 42 headers := make(map[string]interface{}) 43 44 if params != nil && params.FriendlyName != nil { 45 data.Set("FriendlyName", *params.FriendlyName) 46 } 47 48 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 49 if err != nil { 50 return nil, err 51 } 52 53 defer resp.Body.Close() 54 55 ps := &ApiV2010Account{} 56 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 57 return nil, err 58 } 59 60 return ps, err 61 } 62 63 // Fetch the account specified by the provided Account Sid 64 func (c *ApiService) FetchAccount(Sid string) (*ApiV2010Account, error) { 65 path := "/2010-04-01/Accounts/{Sid}.json" 66 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 67 68 data := url.Values{} 69 headers := make(map[string]interface{}) 70 71 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 72 if err != nil { 73 return nil, err 74 } 75 76 defer resp.Body.Close() 77 78 ps := &ApiV2010Account{} 79 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 80 return nil, err 81 } 82 83 return ps, err 84 } 85 86 // Optional parameters for the method 'ListAccount' 87 type ListAccountParams struct { 88 // Only return the Account resources with friendly names that exactly match this name. 89 FriendlyName *string `json:"FriendlyName,omitempty"` 90 // Only return Account resources with the given status. Can be `closed`, `suspended` or `active`. 91 Status *string `json:"Status,omitempty"` 92 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 93 PageSize *int `json:"PageSize,omitempty"` 94 // Max number of records to return. 95 Limit *int `json:"limit,omitempty"` 96 } 97 98 func (params *ListAccountParams) SetFriendlyName(FriendlyName string) *ListAccountParams { 99 params.FriendlyName = &FriendlyName 100 return params 101 } 102 func (params *ListAccountParams) SetStatus(Status string) *ListAccountParams { 103 params.Status = &Status 104 return params 105 } 106 func (params *ListAccountParams) SetPageSize(PageSize int) *ListAccountParams { 107 params.PageSize = &PageSize 108 return params 109 } 110 func (params *ListAccountParams) SetLimit(Limit int) *ListAccountParams { 111 params.Limit = &Limit 112 return params 113 } 114 115 // Retrieve a single page of Account records from the API. Request is executed immediately. 116 func (c *ApiService) PageAccount(params *ListAccountParams, pageToken, pageNumber string) (*ListAccountResponse, error) { 117 path := "/2010-04-01/Accounts.json" 118 119 data := url.Values{} 120 headers := make(map[string]interface{}) 121 122 if params != nil && params.FriendlyName != nil { 123 data.Set("FriendlyName", *params.FriendlyName) 124 } 125 if params != nil && params.Status != nil { 126 data.Set("Status", *params.Status) 127 } 128 if params != nil && params.PageSize != nil { 129 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 130 } 131 132 if pageToken != "" { 133 data.Set("PageToken", pageToken) 134 } 135 if pageNumber != "" { 136 data.Set("Page", pageNumber) 137 } 138 139 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 140 if err != nil { 141 return nil, err 142 } 143 144 defer resp.Body.Close() 145 146 ps := &ListAccountResponse{} 147 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 148 return nil, err 149 } 150 151 return ps, err 152 } 153 154 // Lists Account records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 155 func (c *ApiService) ListAccount(params *ListAccountParams) ([]ApiV2010Account, error) { 156 response, errors := c.StreamAccount(params) 157 158 records := make([]ApiV2010Account, 0) 159 for record := range response { 160 records = append(records, record) 161 } 162 163 if err := <-errors; err != nil { 164 return nil, err 165 } 166 167 return records, nil 168 } 169 170 // Streams Account records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 171 func (c *ApiService) StreamAccount(params *ListAccountParams) (chan ApiV2010Account, chan error) { 172 if params == nil { 173 params = &ListAccountParams{} 174 } 175 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 176 177 recordChannel := make(chan ApiV2010Account, 1) 178 errorChannel := make(chan error, 1) 179 180 response, err := c.PageAccount(params, "", "") 181 if err != nil { 182 errorChannel <- err 183 close(recordChannel) 184 close(errorChannel) 185 } else { 186 go c.streamAccount(response, params, recordChannel, errorChannel) 187 } 188 189 return recordChannel, errorChannel 190 } 191 192 func (c *ApiService) streamAccount(response *ListAccountResponse, params *ListAccountParams, recordChannel chan ApiV2010Account, errorChannel chan error) { 193 curRecord := 1 194 195 for response != nil { 196 responseRecords := response.Accounts 197 for item := range responseRecords { 198 recordChannel <- responseRecords[item] 199 curRecord += 1 200 if params.Limit != nil && *params.Limit < curRecord { 201 close(recordChannel) 202 close(errorChannel) 203 return 204 } 205 } 206 207 record, err := client.GetNext(c.baseURL, response, c.getNextListAccountResponse) 208 if err != nil { 209 errorChannel <- err 210 break 211 } else if record == nil { 212 break 213 } 214 215 response = record.(*ListAccountResponse) 216 } 217 218 close(recordChannel) 219 close(errorChannel) 220 } 221 222 func (c *ApiService) getNextListAccountResponse(nextPageUrl string) (interface{}, error) { 223 if nextPageUrl == "" { 224 return nil, nil 225 } 226 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 227 if err != nil { 228 return nil, err 229 } 230 231 defer resp.Body.Close() 232 233 ps := &ListAccountResponse{} 234 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 235 return nil, err 236 } 237 return ps, nil 238 } 239 240 // Optional parameters for the method 'UpdateAccount' 241 type UpdateAccountParams struct { 242 // Update the human-readable description of this Account 243 FriendlyName *string `json:"FriendlyName,omitempty"` 244 // 245 Status *string `json:"Status,omitempty"` 246 } 247 248 func (params *UpdateAccountParams) SetFriendlyName(FriendlyName string) *UpdateAccountParams { 249 params.FriendlyName = &FriendlyName 250 return params 251 } 252 func (params *UpdateAccountParams) SetStatus(Status string) *UpdateAccountParams { 253 params.Status = &Status 254 return params 255 } 256 257 // Modify the properties of a given Account 258 func (c *ApiService) UpdateAccount(Sid string, params *UpdateAccountParams) (*ApiV2010Account, error) { 259 path := "/2010-04-01/Accounts/{Sid}.json" 260 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 261 262 data := url.Values{} 263 headers := make(map[string]interface{}) 264 265 if params != nil && params.FriendlyName != nil { 266 data.Set("FriendlyName", *params.FriendlyName) 267 } 268 if params != nil && params.Status != nil { 269 data.Set("Status", *params.Status) 270 } 271 272 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 273 if err != nil { 274 return nil, err 275 } 276 277 defer resp.Body.Close() 278 279 ps := &ApiV2010Account{} 280 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 281 return nil, err 282 } 283 284 return ps, err 285 }