github.com/twilio/twilio-go@v1.20.1/rest/wireless/v1/sims_data_sessions.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 'ListDataSession' 27 type ListDataSessionParams struct { 28 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 29 PageSize *int `json:"PageSize,omitempty"` 30 // Max number of records to return. 31 Limit *int `json:"limit,omitempty"` 32 } 33 34 func (params *ListDataSessionParams) SetPageSize(PageSize int) *ListDataSessionParams { 35 params.PageSize = &PageSize 36 return params 37 } 38 func (params *ListDataSessionParams) SetLimit(Limit int) *ListDataSessionParams { 39 params.Limit = &Limit 40 return params 41 } 42 43 // Retrieve a single page of DataSession records from the API. Request is executed immediately. 44 func (c *ApiService) PageDataSession(SimSid string, params *ListDataSessionParams, pageToken, pageNumber string) (*ListDataSessionResponse, error) { 45 path := "/v1/Sims/{SimSid}/DataSessions" 46 47 path = strings.Replace(path, "{"+"SimSid"+"}", SimSid, -1) 48 49 data := url.Values{} 50 headers := make(map[string]interface{}) 51 52 if params != nil && params.PageSize != nil { 53 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 54 } 55 56 if pageToken != "" { 57 data.Set("PageToken", pageToken) 58 } 59 if pageNumber != "" { 60 data.Set("Page", pageNumber) 61 } 62 63 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 64 if err != nil { 65 return nil, err 66 } 67 68 defer resp.Body.Close() 69 70 ps := &ListDataSessionResponse{} 71 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 72 return nil, err 73 } 74 75 return ps, err 76 } 77 78 // Lists DataSession records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 79 func (c *ApiService) ListDataSession(SimSid string, params *ListDataSessionParams) ([]WirelessV1DataSession, error) { 80 response, errors := c.StreamDataSession(SimSid, params) 81 82 records := make([]WirelessV1DataSession, 0) 83 for record := range response { 84 records = append(records, record) 85 } 86 87 if err := <-errors; err != nil { 88 return nil, err 89 } 90 91 return records, nil 92 } 93 94 // Streams DataSession records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 95 func (c *ApiService) StreamDataSession(SimSid string, params *ListDataSessionParams) (chan WirelessV1DataSession, chan error) { 96 if params == nil { 97 params = &ListDataSessionParams{} 98 } 99 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 100 101 recordChannel := make(chan WirelessV1DataSession, 1) 102 errorChannel := make(chan error, 1) 103 104 response, err := c.PageDataSession(SimSid, params, "", "") 105 if err != nil { 106 errorChannel <- err 107 close(recordChannel) 108 close(errorChannel) 109 } else { 110 go c.streamDataSession(response, params, recordChannel, errorChannel) 111 } 112 113 return recordChannel, errorChannel 114 } 115 116 func (c *ApiService) streamDataSession(response *ListDataSessionResponse, params *ListDataSessionParams, recordChannel chan WirelessV1DataSession, errorChannel chan error) { 117 curRecord := 1 118 119 for response != nil { 120 responseRecords := response.DataSessions 121 for item := range responseRecords { 122 recordChannel <- responseRecords[item] 123 curRecord += 1 124 if params.Limit != nil && *params.Limit < curRecord { 125 close(recordChannel) 126 close(errorChannel) 127 return 128 } 129 } 130 131 record, err := client.GetNext(c.baseURL, response, c.getNextListDataSessionResponse) 132 if err != nil { 133 errorChannel <- err 134 break 135 } else if record == nil { 136 break 137 } 138 139 response = record.(*ListDataSessionResponse) 140 } 141 142 close(recordChannel) 143 close(errorChannel) 144 } 145 146 func (c *ApiService) getNextListDataSessionResponse(nextPageUrl string) (interface{}, error) { 147 if nextPageUrl == "" { 148 return nil, nil 149 } 150 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 151 if err != nil { 152 return nil, err 153 } 154 155 defer resp.Body.Close() 156 157 ps := &ListDataSessionResponse{} 158 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 159 return nil, err 160 } 161 return ps, nil 162 }