github.com/twilio/twilio-go@v1.20.1/rest/supersim/v1/sms_commands.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Supersim 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 'CreateSmsCommand' 27 type CreateSmsCommandParams struct { 28 // The `sid` or `unique_name` of the [SIM](https://www.twilio.com/docs/iot/supersim/api/sim-resource) to send the SMS Command to. 29 Sim *string `json:"Sim,omitempty"` 30 // The message body of the SMS Command. 31 Payload *string `json:"Payload,omitempty"` 32 // The HTTP method we should use to call `callback_url`. Can be: `GET` or `POST` and the default is POST. 33 CallbackMethod *string `json:"CallbackMethod,omitempty"` 34 // The URL we should call using the `callback_method` after we have sent the command. 35 CallbackUrl *string `json:"CallbackUrl,omitempty"` 36 } 37 38 func (params *CreateSmsCommandParams) SetSim(Sim string) *CreateSmsCommandParams { 39 params.Sim = &Sim 40 return params 41 } 42 func (params *CreateSmsCommandParams) SetPayload(Payload string) *CreateSmsCommandParams { 43 params.Payload = &Payload 44 return params 45 } 46 func (params *CreateSmsCommandParams) SetCallbackMethod(CallbackMethod string) *CreateSmsCommandParams { 47 params.CallbackMethod = &CallbackMethod 48 return params 49 } 50 func (params *CreateSmsCommandParams) SetCallbackUrl(CallbackUrl string) *CreateSmsCommandParams { 51 params.CallbackUrl = &CallbackUrl 52 return params 53 } 54 55 // Send SMS Command to a Sim. 56 func (c *ApiService) CreateSmsCommand(params *CreateSmsCommandParams) (*SupersimV1SmsCommand, error) { 57 path := "/v1/SmsCommands" 58 59 data := url.Values{} 60 headers := make(map[string]interface{}) 61 62 if params != nil && params.Sim != nil { 63 data.Set("Sim", *params.Sim) 64 } 65 if params != nil && params.Payload != nil { 66 data.Set("Payload", *params.Payload) 67 } 68 if params != nil && params.CallbackMethod != nil { 69 data.Set("CallbackMethod", *params.CallbackMethod) 70 } 71 if params != nil && params.CallbackUrl != nil { 72 data.Set("CallbackUrl", *params.CallbackUrl) 73 } 74 75 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 76 if err != nil { 77 return nil, err 78 } 79 80 defer resp.Body.Close() 81 82 ps := &SupersimV1SmsCommand{} 83 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 84 return nil, err 85 } 86 87 return ps, err 88 } 89 90 // Fetch SMS Command instance from your account. 91 func (c *ApiService) FetchSmsCommand(Sid string) (*SupersimV1SmsCommand, error) { 92 path := "/v1/SmsCommands/{Sid}" 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.Get(c.baseURL+path, data, headers) 99 if err != nil { 100 return nil, err 101 } 102 103 defer resp.Body.Close() 104 105 ps := &SupersimV1SmsCommand{} 106 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 107 return nil, err 108 } 109 110 return ps, err 111 } 112 113 // Optional parameters for the method 'ListSmsCommand' 114 type ListSmsCommandParams struct { 115 // The SID or unique name of the Sim resource that SMS Command was sent to or from. 116 Sim *string `json:"Sim,omitempty"` 117 // The status of the SMS Command. Can be: `queued`, `sent`, `delivered`, `received` or `failed`. See the [SMS Command Status Values](https://www.twilio.com/docs/iot/supersim/api/smscommand-resource#status-values) for a description of each. 118 Status *string `json:"Status,omitempty"` 119 // The direction of the SMS Command. Can be `to_sim` or `from_sim`. The value of `to_sim` is synonymous with the term `mobile terminated`, and `from_sim` is synonymous with the term `mobile originated`. 120 Direction *string `json:"Direction,omitempty"` 121 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 122 PageSize *int `json:"PageSize,omitempty"` 123 // Max number of records to return. 124 Limit *int `json:"limit,omitempty"` 125 } 126 127 func (params *ListSmsCommandParams) SetSim(Sim string) *ListSmsCommandParams { 128 params.Sim = &Sim 129 return params 130 } 131 func (params *ListSmsCommandParams) SetStatus(Status string) *ListSmsCommandParams { 132 params.Status = &Status 133 return params 134 } 135 func (params *ListSmsCommandParams) SetDirection(Direction string) *ListSmsCommandParams { 136 params.Direction = &Direction 137 return params 138 } 139 func (params *ListSmsCommandParams) SetPageSize(PageSize int) *ListSmsCommandParams { 140 params.PageSize = &PageSize 141 return params 142 } 143 func (params *ListSmsCommandParams) SetLimit(Limit int) *ListSmsCommandParams { 144 params.Limit = &Limit 145 return params 146 } 147 148 // Retrieve a single page of SmsCommand records from the API. Request is executed immediately. 149 func (c *ApiService) PageSmsCommand(params *ListSmsCommandParams, pageToken, pageNumber string) (*ListSmsCommandResponse, error) { 150 path := "/v1/SmsCommands" 151 152 data := url.Values{} 153 headers := make(map[string]interface{}) 154 155 if params != nil && params.Sim != nil { 156 data.Set("Sim", *params.Sim) 157 } 158 if params != nil && params.Status != nil { 159 data.Set("Status", *params.Status) 160 } 161 if params != nil && params.Direction != nil { 162 data.Set("Direction", *params.Direction) 163 } 164 if params != nil && params.PageSize != nil { 165 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 166 } 167 168 if pageToken != "" { 169 data.Set("PageToken", pageToken) 170 } 171 if pageNumber != "" { 172 data.Set("Page", pageNumber) 173 } 174 175 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 176 if err != nil { 177 return nil, err 178 } 179 180 defer resp.Body.Close() 181 182 ps := &ListSmsCommandResponse{} 183 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 184 return nil, err 185 } 186 187 return ps, err 188 } 189 190 // Lists SmsCommand records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 191 func (c *ApiService) ListSmsCommand(params *ListSmsCommandParams) ([]SupersimV1SmsCommand, error) { 192 response, errors := c.StreamSmsCommand(params) 193 194 records := make([]SupersimV1SmsCommand, 0) 195 for record := range response { 196 records = append(records, record) 197 } 198 199 if err := <-errors; err != nil { 200 return nil, err 201 } 202 203 return records, nil 204 } 205 206 // Streams SmsCommand records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 207 func (c *ApiService) StreamSmsCommand(params *ListSmsCommandParams) (chan SupersimV1SmsCommand, chan error) { 208 if params == nil { 209 params = &ListSmsCommandParams{} 210 } 211 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 212 213 recordChannel := make(chan SupersimV1SmsCommand, 1) 214 errorChannel := make(chan error, 1) 215 216 response, err := c.PageSmsCommand(params, "", "") 217 if err != nil { 218 errorChannel <- err 219 close(recordChannel) 220 close(errorChannel) 221 } else { 222 go c.streamSmsCommand(response, params, recordChannel, errorChannel) 223 } 224 225 return recordChannel, errorChannel 226 } 227 228 func (c *ApiService) streamSmsCommand(response *ListSmsCommandResponse, params *ListSmsCommandParams, recordChannel chan SupersimV1SmsCommand, errorChannel chan error) { 229 curRecord := 1 230 231 for response != nil { 232 responseRecords := response.SmsCommands 233 for item := range responseRecords { 234 recordChannel <- responseRecords[item] 235 curRecord += 1 236 if params.Limit != nil && *params.Limit < curRecord { 237 close(recordChannel) 238 close(errorChannel) 239 return 240 } 241 } 242 243 record, err := client.GetNext(c.baseURL, response, c.getNextListSmsCommandResponse) 244 if err != nil { 245 errorChannel <- err 246 break 247 } else if record == nil { 248 break 249 } 250 251 response = record.(*ListSmsCommandResponse) 252 } 253 254 close(recordChannel) 255 close(errorChannel) 256 } 257 258 func (c *ApiService) getNextListSmsCommandResponse(nextPageUrl string) (interface{}, error) { 259 if nextPageUrl == "" { 260 return nil, nil 261 } 262 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 263 if err != nil { 264 return nil, err 265 } 266 267 defer resp.Body.Close() 268 269 ps := &ListSmsCommandResponse{} 270 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 271 return nil, err 272 } 273 return ps, nil 274 }