github.com/twilio/twilio-go@v1.20.1/rest/api/v2010/accounts_sms_short_codes.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 'FetchShortCode' 27 type FetchShortCodeParams struct { 28 // The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the ShortCode resource(s) to fetch. 29 PathAccountSid *string `json:"PathAccountSid,omitempty"` 30 } 31 32 func (params *FetchShortCodeParams) SetPathAccountSid(PathAccountSid string) *FetchShortCodeParams { 33 params.PathAccountSid = &PathAccountSid 34 return params 35 } 36 37 // Fetch an instance of a short code 38 func (c *ApiService) FetchShortCode(Sid string, params *FetchShortCodeParams) (*ApiV2010ShortCode, error) { 39 path := "/2010-04-01/Accounts/{AccountSid}/SMS/ShortCodes/{Sid}.json" 40 if params != nil && params.PathAccountSid != nil { 41 path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) 42 } else { 43 path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1) 44 } 45 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 46 47 data := url.Values{} 48 headers := make(map[string]interface{}) 49 50 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 51 if err != nil { 52 return nil, err 53 } 54 55 defer resp.Body.Close() 56 57 ps := &ApiV2010ShortCode{} 58 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 59 return nil, err 60 } 61 62 return ps, err 63 } 64 65 // Optional parameters for the method 'ListShortCode' 66 type ListShortCodeParams struct { 67 // The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the ShortCode resource(s) to read. 68 PathAccountSid *string `json:"PathAccountSid,omitempty"` 69 // The string that identifies the ShortCode resources to read. 70 FriendlyName *string `json:"FriendlyName,omitempty"` 71 // Only show the ShortCode resources that match this pattern. You can specify partial numbers and use '*' as a wildcard for any digit. 72 ShortCode *string `json:"ShortCode,omitempty"` 73 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 74 PageSize *int `json:"PageSize,omitempty"` 75 // Max number of records to return. 76 Limit *int `json:"limit,omitempty"` 77 } 78 79 func (params *ListShortCodeParams) SetPathAccountSid(PathAccountSid string) *ListShortCodeParams { 80 params.PathAccountSid = &PathAccountSid 81 return params 82 } 83 func (params *ListShortCodeParams) SetFriendlyName(FriendlyName string) *ListShortCodeParams { 84 params.FriendlyName = &FriendlyName 85 return params 86 } 87 func (params *ListShortCodeParams) SetShortCode(ShortCode string) *ListShortCodeParams { 88 params.ShortCode = &ShortCode 89 return params 90 } 91 func (params *ListShortCodeParams) SetPageSize(PageSize int) *ListShortCodeParams { 92 params.PageSize = &PageSize 93 return params 94 } 95 func (params *ListShortCodeParams) SetLimit(Limit int) *ListShortCodeParams { 96 params.Limit = &Limit 97 return params 98 } 99 100 // Retrieve a single page of ShortCode records from the API. Request is executed immediately. 101 func (c *ApiService) PageShortCode(params *ListShortCodeParams, pageToken, pageNumber string) (*ListShortCodeResponse, error) { 102 path := "/2010-04-01/Accounts/{AccountSid}/SMS/ShortCodes.json" 103 104 if params != nil && params.PathAccountSid != nil { 105 path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) 106 } else { 107 path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1) 108 } 109 110 data := url.Values{} 111 headers := make(map[string]interface{}) 112 113 if params != nil && params.FriendlyName != nil { 114 data.Set("FriendlyName", *params.FriendlyName) 115 } 116 if params != nil && params.ShortCode != nil { 117 data.Set("ShortCode", *params.ShortCode) 118 } 119 if params != nil && params.PageSize != nil { 120 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 121 } 122 123 if pageToken != "" { 124 data.Set("PageToken", pageToken) 125 } 126 if pageNumber != "" { 127 data.Set("Page", pageNumber) 128 } 129 130 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 131 if err != nil { 132 return nil, err 133 } 134 135 defer resp.Body.Close() 136 137 ps := &ListShortCodeResponse{} 138 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 139 return nil, err 140 } 141 142 return ps, err 143 } 144 145 // Lists ShortCode records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 146 func (c *ApiService) ListShortCode(params *ListShortCodeParams) ([]ApiV2010ShortCode, error) { 147 response, errors := c.StreamShortCode(params) 148 149 records := make([]ApiV2010ShortCode, 0) 150 for record := range response { 151 records = append(records, record) 152 } 153 154 if err := <-errors; err != nil { 155 return nil, err 156 } 157 158 return records, nil 159 } 160 161 // Streams ShortCode records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 162 func (c *ApiService) StreamShortCode(params *ListShortCodeParams) (chan ApiV2010ShortCode, chan error) { 163 if params == nil { 164 params = &ListShortCodeParams{} 165 } 166 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 167 168 recordChannel := make(chan ApiV2010ShortCode, 1) 169 errorChannel := make(chan error, 1) 170 171 response, err := c.PageShortCode(params, "", "") 172 if err != nil { 173 errorChannel <- err 174 close(recordChannel) 175 close(errorChannel) 176 } else { 177 go c.streamShortCode(response, params, recordChannel, errorChannel) 178 } 179 180 return recordChannel, errorChannel 181 } 182 183 func (c *ApiService) streamShortCode(response *ListShortCodeResponse, params *ListShortCodeParams, recordChannel chan ApiV2010ShortCode, errorChannel chan error) { 184 curRecord := 1 185 186 for response != nil { 187 responseRecords := response.ShortCodes 188 for item := range responseRecords { 189 recordChannel <- responseRecords[item] 190 curRecord += 1 191 if params.Limit != nil && *params.Limit < curRecord { 192 close(recordChannel) 193 close(errorChannel) 194 return 195 } 196 } 197 198 record, err := client.GetNext(c.baseURL, response, c.getNextListShortCodeResponse) 199 if err != nil { 200 errorChannel <- err 201 break 202 } else if record == nil { 203 break 204 } 205 206 response = record.(*ListShortCodeResponse) 207 } 208 209 close(recordChannel) 210 close(errorChannel) 211 } 212 213 func (c *ApiService) getNextListShortCodeResponse(nextPageUrl string) (interface{}, error) { 214 if nextPageUrl == "" { 215 return nil, nil 216 } 217 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 218 if err != nil { 219 return nil, err 220 } 221 222 defer resp.Body.Close() 223 224 ps := &ListShortCodeResponse{} 225 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 226 return nil, err 227 } 228 return ps, nil 229 } 230 231 // Optional parameters for the method 'UpdateShortCode' 232 type UpdateShortCodeParams struct { 233 // The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the ShortCode resource(s) to update. 234 PathAccountSid *string `json:"PathAccountSid,omitempty"` 235 // A descriptive string that you created to describe this resource. It can be up to 64 characters long. By default, the `FriendlyName` is the short code. 236 FriendlyName *string `json:"FriendlyName,omitempty"` 237 // The API version to use to start a new TwiML session. Can be: `2010-04-01` or `2008-08-01`. 238 ApiVersion *string `json:"ApiVersion,omitempty"` 239 // The URL we should call when receiving an incoming SMS message to this short code. 240 SmsUrl *string `json:"SmsUrl,omitempty"` 241 // The HTTP method we should use when calling the `sms_url`. Can be: `GET` or `POST`. 242 SmsMethod *string `json:"SmsMethod,omitempty"` 243 // The URL that we should call if an error occurs while retrieving or executing the TwiML from `sms_url`. 244 SmsFallbackUrl *string `json:"SmsFallbackUrl,omitempty"` 245 // The HTTP method that we should use to call the `sms_fallback_url`. Can be: `GET` or `POST`. 246 SmsFallbackMethod *string `json:"SmsFallbackMethod,omitempty"` 247 } 248 249 func (params *UpdateShortCodeParams) SetPathAccountSid(PathAccountSid string) *UpdateShortCodeParams { 250 params.PathAccountSid = &PathAccountSid 251 return params 252 } 253 func (params *UpdateShortCodeParams) SetFriendlyName(FriendlyName string) *UpdateShortCodeParams { 254 params.FriendlyName = &FriendlyName 255 return params 256 } 257 func (params *UpdateShortCodeParams) SetApiVersion(ApiVersion string) *UpdateShortCodeParams { 258 params.ApiVersion = &ApiVersion 259 return params 260 } 261 func (params *UpdateShortCodeParams) SetSmsUrl(SmsUrl string) *UpdateShortCodeParams { 262 params.SmsUrl = &SmsUrl 263 return params 264 } 265 func (params *UpdateShortCodeParams) SetSmsMethod(SmsMethod string) *UpdateShortCodeParams { 266 params.SmsMethod = &SmsMethod 267 return params 268 } 269 func (params *UpdateShortCodeParams) SetSmsFallbackUrl(SmsFallbackUrl string) *UpdateShortCodeParams { 270 params.SmsFallbackUrl = &SmsFallbackUrl 271 return params 272 } 273 func (params *UpdateShortCodeParams) SetSmsFallbackMethod(SmsFallbackMethod string) *UpdateShortCodeParams { 274 params.SmsFallbackMethod = &SmsFallbackMethod 275 return params 276 } 277 278 // Update a short code with the following parameters 279 func (c *ApiService) UpdateShortCode(Sid string, params *UpdateShortCodeParams) (*ApiV2010ShortCode, error) { 280 path := "/2010-04-01/Accounts/{AccountSid}/SMS/ShortCodes/{Sid}.json" 281 if params != nil && params.PathAccountSid != nil { 282 path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) 283 } else { 284 path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1) 285 } 286 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 287 288 data := url.Values{} 289 headers := make(map[string]interface{}) 290 291 if params != nil && params.FriendlyName != nil { 292 data.Set("FriendlyName", *params.FriendlyName) 293 } 294 if params != nil && params.ApiVersion != nil { 295 data.Set("ApiVersion", *params.ApiVersion) 296 } 297 if params != nil && params.SmsUrl != nil { 298 data.Set("SmsUrl", *params.SmsUrl) 299 } 300 if params != nil && params.SmsMethod != nil { 301 data.Set("SmsMethod", *params.SmsMethod) 302 } 303 if params != nil && params.SmsFallbackUrl != nil { 304 data.Set("SmsFallbackUrl", *params.SmsFallbackUrl) 305 } 306 if params != nil && params.SmsFallbackMethod != nil { 307 data.Set("SmsFallbackMethod", *params.SmsFallbackMethod) 308 } 309 310 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 311 if err != nil { 312 return nil, err 313 } 314 315 defer resp.Body.Close() 316 317 ps := &ApiV2010ShortCode{} 318 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 319 return nil, err 320 } 321 322 return ps, err 323 }