github.com/twilio/twilio-go@v1.20.1/rest/wireless/v1/commands.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Wireless 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 'CreateCommand' 27 type CreateCommandParams struct { 28 // The message body of the Command. Can be plain text in text mode or a Base64 encoded byte string in binary mode. 29 Command *string `json:"Command,omitempty"` 30 // The `sid` or `unique_name` of the [SIM](https://www.twilio.com/docs/iot/wireless/api/sim-resource) to send the Command to. 31 Sim *string `json:"Sim,omitempty"` 32 // The HTTP method we use to call `callback_url`. Can be: `POST` or `GET`, and the default is `POST`. 33 CallbackMethod *string `json:"CallbackMethod,omitempty"` 34 // The URL we call using the `callback_url` when the Command has finished sending, whether the command was delivered or it failed. 35 CallbackUrl *string `json:"CallbackUrl,omitempty"` 36 // 37 CommandMode *string `json:"CommandMode,omitempty"` 38 // Whether to include the SID of the command in the message body. Can be: `none`, `start`, or `end`, and the default behavior is `none`. When sending a Command to a SIM in text mode, we can automatically include the SID of the Command in the message body, which could be used to ensure that the device does not process the same Command more than once. A value of `start` will prepend the message with the Command SID, and `end` will append it to the end, separating the Command SID from the message body with a space. The length of the Command SID is included in the 160 character limit so the SMS body must be 128 characters or less before the Command SID is included. 39 IncludeSid *string `json:"IncludeSid,omitempty"` 40 // Whether to request delivery receipt from the recipient. For Commands that request delivery receipt, the Command state transitions to 'delivered' once the server has received a delivery receipt from the device. The default value is `true`. 41 DeliveryReceiptRequested *bool `json:"DeliveryReceiptRequested,omitempty"` 42 } 43 44 func (params *CreateCommandParams) SetCommand(Command string) *CreateCommandParams { 45 params.Command = &Command 46 return params 47 } 48 func (params *CreateCommandParams) SetSim(Sim string) *CreateCommandParams { 49 params.Sim = &Sim 50 return params 51 } 52 func (params *CreateCommandParams) SetCallbackMethod(CallbackMethod string) *CreateCommandParams { 53 params.CallbackMethod = &CallbackMethod 54 return params 55 } 56 func (params *CreateCommandParams) SetCallbackUrl(CallbackUrl string) *CreateCommandParams { 57 params.CallbackUrl = &CallbackUrl 58 return params 59 } 60 func (params *CreateCommandParams) SetCommandMode(CommandMode string) *CreateCommandParams { 61 params.CommandMode = &CommandMode 62 return params 63 } 64 func (params *CreateCommandParams) SetIncludeSid(IncludeSid string) *CreateCommandParams { 65 params.IncludeSid = &IncludeSid 66 return params 67 } 68 func (params *CreateCommandParams) SetDeliveryReceiptRequested(DeliveryReceiptRequested bool) *CreateCommandParams { 69 params.DeliveryReceiptRequested = &DeliveryReceiptRequested 70 return params 71 } 72 73 // Send a Command to a Sim. 74 func (c *ApiService) CreateCommand(params *CreateCommandParams) (*WirelessV1Command, error) { 75 path := "/v1/Commands" 76 77 data := url.Values{} 78 headers := make(map[string]interface{}) 79 80 if params != nil && params.Command != nil { 81 data.Set("Command", *params.Command) 82 } 83 if params != nil && params.Sim != nil { 84 data.Set("Sim", *params.Sim) 85 } 86 if params != nil && params.CallbackMethod != nil { 87 data.Set("CallbackMethod", *params.CallbackMethod) 88 } 89 if params != nil && params.CallbackUrl != nil { 90 data.Set("CallbackUrl", *params.CallbackUrl) 91 } 92 if params != nil && params.CommandMode != nil { 93 data.Set("CommandMode", *params.CommandMode) 94 } 95 if params != nil && params.IncludeSid != nil { 96 data.Set("IncludeSid", *params.IncludeSid) 97 } 98 if params != nil && params.DeliveryReceiptRequested != nil { 99 data.Set("DeliveryReceiptRequested", fmt.Sprint(*params.DeliveryReceiptRequested)) 100 } 101 102 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 103 if err != nil { 104 return nil, err 105 } 106 107 defer resp.Body.Close() 108 109 ps := &WirelessV1Command{} 110 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 111 return nil, err 112 } 113 114 return ps, err 115 } 116 117 // Delete a Command instance from your account. 118 func (c *ApiService) DeleteCommand(Sid string) error { 119 path := "/v1/Commands/{Sid}" 120 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 121 122 data := url.Values{} 123 headers := make(map[string]interface{}) 124 125 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 126 if err != nil { 127 return err 128 } 129 130 defer resp.Body.Close() 131 132 return nil 133 } 134 135 // Fetch a Command instance from your account. 136 func (c *ApiService) FetchCommand(Sid string) (*WirelessV1Command, error) { 137 path := "/v1/Commands/{Sid}" 138 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 139 140 data := url.Values{} 141 headers := make(map[string]interface{}) 142 143 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 144 if err != nil { 145 return nil, err 146 } 147 148 defer resp.Body.Close() 149 150 ps := &WirelessV1Command{} 151 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 152 return nil, err 153 } 154 155 return ps, err 156 } 157 158 // Optional parameters for the method 'ListCommand' 159 type ListCommandParams struct { 160 // The `sid` or `unique_name` of the [Sim resources](https://www.twilio.com/docs/iot/wireless/api/sim-resource) to read. 161 Sim *string `json:"Sim,omitempty"` 162 // The status of the resources to read. Can be: `queued`, `sent`, `delivered`, `received`, or `failed`. 163 Status *string `json:"Status,omitempty"` 164 // Only return Commands with this direction value. 165 Direction *string `json:"Direction,omitempty"` 166 // Only return Commands with this transport value. Can be: `sms` or `ip`. 167 Transport *string `json:"Transport,omitempty"` 168 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 169 PageSize *int `json:"PageSize,omitempty"` 170 // Max number of records to return. 171 Limit *int `json:"limit,omitempty"` 172 } 173 174 func (params *ListCommandParams) SetSim(Sim string) *ListCommandParams { 175 params.Sim = &Sim 176 return params 177 } 178 func (params *ListCommandParams) SetStatus(Status string) *ListCommandParams { 179 params.Status = &Status 180 return params 181 } 182 func (params *ListCommandParams) SetDirection(Direction string) *ListCommandParams { 183 params.Direction = &Direction 184 return params 185 } 186 func (params *ListCommandParams) SetTransport(Transport string) *ListCommandParams { 187 params.Transport = &Transport 188 return params 189 } 190 func (params *ListCommandParams) SetPageSize(PageSize int) *ListCommandParams { 191 params.PageSize = &PageSize 192 return params 193 } 194 func (params *ListCommandParams) SetLimit(Limit int) *ListCommandParams { 195 params.Limit = &Limit 196 return params 197 } 198 199 // Retrieve a single page of Command records from the API. Request is executed immediately. 200 func (c *ApiService) PageCommand(params *ListCommandParams, pageToken, pageNumber string) (*ListCommandResponse, error) { 201 path := "/v1/Commands" 202 203 data := url.Values{} 204 headers := make(map[string]interface{}) 205 206 if params != nil && params.Sim != nil { 207 data.Set("Sim", *params.Sim) 208 } 209 if params != nil && params.Status != nil { 210 data.Set("Status", *params.Status) 211 } 212 if params != nil && params.Direction != nil { 213 data.Set("Direction", *params.Direction) 214 } 215 if params != nil && params.Transport != nil { 216 data.Set("Transport", *params.Transport) 217 } 218 if params != nil && params.PageSize != nil { 219 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 220 } 221 222 if pageToken != "" { 223 data.Set("PageToken", pageToken) 224 } 225 if pageNumber != "" { 226 data.Set("Page", pageNumber) 227 } 228 229 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 230 if err != nil { 231 return nil, err 232 } 233 234 defer resp.Body.Close() 235 236 ps := &ListCommandResponse{} 237 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 238 return nil, err 239 } 240 241 return ps, err 242 } 243 244 // Lists Command records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 245 func (c *ApiService) ListCommand(params *ListCommandParams) ([]WirelessV1Command, error) { 246 response, errors := c.StreamCommand(params) 247 248 records := make([]WirelessV1Command, 0) 249 for record := range response { 250 records = append(records, record) 251 } 252 253 if err := <-errors; err != nil { 254 return nil, err 255 } 256 257 return records, nil 258 } 259 260 // Streams Command records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 261 func (c *ApiService) StreamCommand(params *ListCommandParams) (chan WirelessV1Command, chan error) { 262 if params == nil { 263 params = &ListCommandParams{} 264 } 265 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 266 267 recordChannel := make(chan WirelessV1Command, 1) 268 errorChannel := make(chan error, 1) 269 270 response, err := c.PageCommand(params, "", "") 271 if err != nil { 272 errorChannel <- err 273 close(recordChannel) 274 close(errorChannel) 275 } else { 276 go c.streamCommand(response, params, recordChannel, errorChannel) 277 } 278 279 return recordChannel, errorChannel 280 } 281 282 func (c *ApiService) streamCommand(response *ListCommandResponse, params *ListCommandParams, recordChannel chan WirelessV1Command, errorChannel chan error) { 283 curRecord := 1 284 285 for response != nil { 286 responseRecords := response.Commands 287 for item := range responseRecords { 288 recordChannel <- responseRecords[item] 289 curRecord += 1 290 if params.Limit != nil && *params.Limit < curRecord { 291 close(recordChannel) 292 close(errorChannel) 293 return 294 } 295 } 296 297 record, err := client.GetNext(c.baseURL, response, c.getNextListCommandResponse) 298 if err != nil { 299 errorChannel <- err 300 break 301 } else if record == nil { 302 break 303 } 304 305 response = record.(*ListCommandResponse) 306 } 307 308 close(recordChannel) 309 close(errorChannel) 310 } 311 312 func (c *ApiService) getNextListCommandResponse(nextPageUrl string) (interface{}, error) { 313 if nextPageUrl == "" { 314 return nil, nil 315 } 316 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 317 if err != nil { 318 return nil, err 319 } 320 321 defer resp.Body.Close() 322 323 ps := &ListCommandResponse{} 324 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 325 return nil, err 326 } 327 return ps, nil 328 }