github.com/twilio/twilio-go@v1.20.1/rest/studio/v1/flows.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Studio 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 // Delete a specific Flow. 27 func (c *ApiService) DeleteFlow(Sid string) error { 28 path := "/v1/Flows/{Sid}" 29 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 30 31 data := url.Values{} 32 headers := make(map[string]interface{}) 33 34 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 35 if err != nil { 36 return err 37 } 38 39 defer resp.Body.Close() 40 41 return nil 42 } 43 44 // Retrieve a specific Flow. 45 func (c *ApiService) FetchFlow(Sid string) (*StudioV1Flow, error) { 46 path := "/v1/Flows/{Sid}" 47 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 48 49 data := url.Values{} 50 headers := make(map[string]interface{}) 51 52 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 53 if err != nil { 54 return nil, err 55 } 56 57 defer resp.Body.Close() 58 59 ps := &StudioV1Flow{} 60 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 61 return nil, err 62 } 63 64 return ps, err 65 } 66 67 // Optional parameters for the method 'ListFlow' 68 type ListFlowParams struct { 69 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 70 PageSize *int `json:"PageSize,omitempty"` 71 // Max number of records to return. 72 Limit *int `json:"limit,omitempty"` 73 } 74 75 func (params *ListFlowParams) SetPageSize(PageSize int) *ListFlowParams { 76 params.PageSize = &PageSize 77 return params 78 } 79 func (params *ListFlowParams) SetLimit(Limit int) *ListFlowParams { 80 params.Limit = &Limit 81 return params 82 } 83 84 // Retrieve a single page of Flow records from the API. Request is executed immediately. 85 func (c *ApiService) PageFlow(params *ListFlowParams, pageToken, pageNumber string) (*ListFlowResponse, error) { 86 path := "/v1/Flows" 87 88 data := url.Values{} 89 headers := make(map[string]interface{}) 90 91 if params != nil && params.PageSize != nil { 92 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 93 } 94 95 if pageToken != "" { 96 data.Set("PageToken", pageToken) 97 } 98 if pageNumber != "" { 99 data.Set("Page", pageNumber) 100 } 101 102 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 103 if err != nil { 104 return nil, err 105 } 106 107 defer resp.Body.Close() 108 109 ps := &ListFlowResponse{} 110 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 111 return nil, err 112 } 113 114 return ps, err 115 } 116 117 // Lists Flow records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 118 func (c *ApiService) ListFlow(params *ListFlowParams) ([]StudioV1Flow, error) { 119 response, errors := c.StreamFlow(params) 120 121 records := make([]StudioV1Flow, 0) 122 for record := range response { 123 records = append(records, record) 124 } 125 126 if err := <-errors; err != nil { 127 return nil, err 128 } 129 130 return records, nil 131 } 132 133 // Streams Flow records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 134 func (c *ApiService) StreamFlow(params *ListFlowParams) (chan StudioV1Flow, chan error) { 135 if params == nil { 136 params = &ListFlowParams{} 137 } 138 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 139 140 recordChannel := make(chan StudioV1Flow, 1) 141 errorChannel := make(chan error, 1) 142 143 response, err := c.PageFlow(params, "", "") 144 if err != nil { 145 errorChannel <- err 146 close(recordChannel) 147 close(errorChannel) 148 } else { 149 go c.streamFlow(response, params, recordChannel, errorChannel) 150 } 151 152 return recordChannel, errorChannel 153 } 154 155 func (c *ApiService) streamFlow(response *ListFlowResponse, params *ListFlowParams, recordChannel chan StudioV1Flow, errorChannel chan error) { 156 curRecord := 1 157 158 for response != nil { 159 responseRecords := response.Flows 160 for item := range responseRecords { 161 recordChannel <- responseRecords[item] 162 curRecord += 1 163 if params.Limit != nil && *params.Limit < curRecord { 164 close(recordChannel) 165 close(errorChannel) 166 return 167 } 168 } 169 170 record, err := client.GetNext(c.baseURL, response, c.getNextListFlowResponse) 171 if err != nil { 172 errorChannel <- err 173 break 174 } else if record == nil { 175 break 176 } 177 178 response = record.(*ListFlowResponse) 179 } 180 181 close(recordChannel) 182 close(errorChannel) 183 } 184 185 func (c *ApiService) getNextListFlowResponse(nextPageUrl string) (interface{}, error) { 186 if nextPageUrl == "" { 187 return nil, nil 188 } 189 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 190 if err != nil { 191 return nil, err 192 } 193 194 defer resp.Body.Close() 195 196 ps := &ListFlowResponse{} 197 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 198 return nil, err 199 } 200 return ps, nil 201 }