github.com/twilio/twilio-go@v1.20.1/rest/serverless/v1/services_functions.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Serverless 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 'CreateFunction' 27 type CreateFunctionParams struct { 28 // A descriptive string that you create to describe the Function resource. It can be a maximum of 255 characters. 29 FriendlyName *string `json:"FriendlyName,omitempty"` 30 } 31 32 func (params *CreateFunctionParams) SetFriendlyName(FriendlyName string) *CreateFunctionParams { 33 params.FriendlyName = &FriendlyName 34 return params 35 } 36 37 // Create a new Function resource. 38 func (c *ApiService) CreateFunction(ServiceSid string, params *CreateFunctionParams) (*ServerlessV1Function, error) { 39 path := "/v1/Services/{ServiceSid}/Functions" 40 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 41 42 data := url.Values{} 43 headers := make(map[string]interface{}) 44 45 if params != nil && params.FriendlyName != nil { 46 data.Set("FriendlyName", *params.FriendlyName) 47 } 48 49 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 50 if err != nil { 51 return nil, err 52 } 53 54 defer resp.Body.Close() 55 56 ps := &ServerlessV1Function{} 57 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 58 return nil, err 59 } 60 61 return ps, err 62 } 63 64 // Delete a Function resource. 65 func (c *ApiService) DeleteFunction(ServiceSid string, Sid string) error { 66 path := "/v1/Services/{ServiceSid}/Functions/{Sid}" 67 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 68 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 69 70 data := url.Values{} 71 headers := make(map[string]interface{}) 72 73 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 74 if err != nil { 75 return err 76 } 77 78 defer resp.Body.Close() 79 80 return nil 81 } 82 83 // Retrieve a specific Function resource. 84 func (c *ApiService) FetchFunction(ServiceSid string, Sid string) (*ServerlessV1Function, error) { 85 path := "/v1/Services/{ServiceSid}/Functions/{Sid}" 86 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 87 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 88 89 data := url.Values{} 90 headers := make(map[string]interface{}) 91 92 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 93 if err != nil { 94 return nil, err 95 } 96 97 defer resp.Body.Close() 98 99 ps := &ServerlessV1Function{} 100 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 101 return nil, err 102 } 103 104 return ps, err 105 } 106 107 // Optional parameters for the method 'ListFunction' 108 type ListFunctionParams struct { 109 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 110 PageSize *int `json:"PageSize,omitempty"` 111 // Max number of records to return. 112 Limit *int `json:"limit,omitempty"` 113 } 114 115 func (params *ListFunctionParams) SetPageSize(PageSize int) *ListFunctionParams { 116 params.PageSize = &PageSize 117 return params 118 } 119 func (params *ListFunctionParams) SetLimit(Limit int) *ListFunctionParams { 120 params.Limit = &Limit 121 return params 122 } 123 124 // Retrieve a single page of Function records from the API. Request is executed immediately. 125 func (c *ApiService) PageFunction(ServiceSid string, params *ListFunctionParams, pageToken, pageNumber string) (*ListFunctionResponse, error) { 126 path := "/v1/Services/{ServiceSid}/Functions" 127 128 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 129 130 data := url.Values{} 131 headers := make(map[string]interface{}) 132 133 if params != nil && params.PageSize != nil { 134 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 135 } 136 137 if pageToken != "" { 138 data.Set("PageToken", pageToken) 139 } 140 if pageNumber != "" { 141 data.Set("Page", pageNumber) 142 } 143 144 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 145 if err != nil { 146 return nil, err 147 } 148 149 defer resp.Body.Close() 150 151 ps := &ListFunctionResponse{} 152 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 153 return nil, err 154 } 155 156 return ps, err 157 } 158 159 // Lists Function records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 160 func (c *ApiService) ListFunction(ServiceSid string, params *ListFunctionParams) ([]ServerlessV1Function, error) { 161 response, errors := c.StreamFunction(ServiceSid, params) 162 163 records := make([]ServerlessV1Function, 0) 164 for record := range response { 165 records = append(records, record) 166 } 167 168 if err := <-errors; err != nil { 169 return nil, err 170 } 171 172 return records, nil 173 } 174 175 // Streams Function records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 176 func (c *ApiService) StreamFunction(ServiceSid string, params *ListFunctionParams) (chan ServerlessV1Function, chan error) { 177 if params == nil { 178 params = &ListFunctionParams{} 179 } 180 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 181 182 recordChannel := make(chan ServerlessV1Function, 1) 183 errorChannel := make(chan error, 1) 184 185 response, err := c.PageFunction(ServiceSid, params, "", "") 186 if err != nil { 187 errorChannel <- err 188 close(recordChannel) 189 close(errorChannel) 190 } else { 191 go c.streamFunction(response, params, recordChannel, errorChannel) 192 } 193 194 return recordChannel, errorChannel 195 } 196 197 func (c *ApiService) streamFunction(response *ListFunctionResponse, params *ListFunctionParams, recordChannel chan ServerlessV1Function, errorChannel chan error) { 198 curRecord := 1 199 200 for response != nil { 201 responseRecords := response.Functions 202 for item := range responseRecords { 203 recordChannel <- responseRecords[item] 204 curRecord += 1 205 if params.Limit != nil && *params.Limit < curRecord { 206 close(recordChannel) 207 close(errorChannel) 208 return 209 } 210 } 211 212 record, err := client.GetNext(c.baseURL, response, c.getNextListFunctionResponse) 213 if err != nil { 214 errorChannel <- err 215 break 216 } else if record == nil { 217 break 218 } 219 220 response = record.(*ListFunctionResponse) 221 } 222 223 close(recordChannel) 224 close(errorChannel) 225 } 226 227 func (c *ApiService) getNextListFunctionResponse(nextPageUrl string) (interface{}, error) { 228 if nextPageUrl == "" { 229 return nil, nil 230 } 231 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 232 if err != nil { 233 return nil, err 234 } 235 236 defer resp.Body.Close() 237 238 ps := &ListFunctionResponse{} 239 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 240 return nil, err 241 } 242 return ps, nil 243 } 244 245 // Optional parameters for the method 'UpdateFunction' 246 type UpdateFunctionParams struct { 247 // A descriptive string that you create to describe the Function resource. It can be a maximum of 255 characters. 248 FriendlyName *string `json:"FriendlyName,omitempty"` 249 } 250 251 func (params *UpdateFunctionParams) SetFriendlyName(FriendlyName string) *UpdateFunctionParams { 252 params.FriendlyName = &FriendlyName 253 return params 254 } 255 256 // Update a specific Function resource. 257 func (c *ApiService) UpdateFunction(ServiceSid string, Sid string, params *UpdateFunctionParams) (*ServerlessV1Function, error) { 258 path := "/v1/Services/{ServiceSid}/Functions/{Sid}" 259 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 260 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 261 262 data := url.Values{} 263 headers := make(map[string]interface{}) 264 265 if params != nil && params.FriendlyName != nil { 266 data.Set("FriendlyName", *params.FriendlyName) 267 } 268 269 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 270 if err != nil { 271 return nil, err 272 } 273 274 defer resp.Body.Close() 275 276 ps := &ServerlessV1Function{} 277 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 278 return nil, err 279 } 280 281 return ps, err 282 }