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