github.com/twilio/twilio-go@v1.20.1/rest/serverless/v1/services_environments_deployments.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 'CreateDeployment' 27 type CreateDeploymentParams struct { 28 // The SID of the Build for the Deployment. 29 BuildSid *string `json:"BuildSid,omitempty"` 30 } 31 32 func (params *CreateDeploymentParams) SetBuildSid(BuildSid string) *CreateDeploymentParams { 33 params.BuildSid = &BuildSid 34 return params 35 } 36 37 // Create a new Deployment. 38 func (c *ApiService) CreateDeployment(ServiceSid string, EnvironmentSid string, params *CreateDeploymentParams) (*ServerlessV1Deployment, error) { 39 path := "/v1/Services/{ServiceSid}/Environments/{EnvironmentSid}/Deployments" 40 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 41 path = strings.Replace(path, "{"+"EnvironmentSid"+"}", EnvironmentSid, -1) 42 43 data := url.Values{} 44 headers := make(map[string]interface{}) 45 46 if params != nil && params.BuildSid != nil { 47 data.Set("BuildSid", *params.BuildSid) 48 } 49 50 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 51 if err != nil { 52 return nil, err 53 } 54 55 defer resp.Body.Close() 56 57 ps := &ServerlessV1Deployment{} 58 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 59 return nil, err 60 } 61 62 return ps, err 63 } 64 65 // Retrieve a specific Deployment. 66 func (c *ApiService) FetchDeployment(ServiceSid string, EnvironmentSid string, Sid string) (*ServerlessV1Deployment, error) { 67 path := "/v1/Services/{ServiceSid}/Environments/{EnvironmentSid}/Deployments/{Sid}" 68 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 69 path = strings.Replace(path, "{"+"EnvironmentSid"+"}", EnvironmentSid, -1) 70 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 71 72 data := url.Values{} 73 headers := make(map[string]interface{}) 74 75 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 76 if err != nil { 77 return nil, err 78 } 79 80 defer resp.Body.Close() 81 82 ps := &ServerlessV1Deployment{} 83 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 84 return nil, err 85 } 86 87 return ps, err 88 } 89 90 // Optional parameters for the method 'ListDeployment' 91 type ListDeploymentParams struct { 92 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 93 PageSize *int `json:"PageSize,omitempty"` 94 // Max number of records to return. 95 Limit *int `json:"limit,omitempty"` 96 } 97 98 func (params *ListDeploymentParams) SetPageSize(PageSize int) *ListDeploymentParams { 99 params.PageSize = &PageSize 100 return params 101 } 102 func (params *ListDeploymentParams) SetLimit(Limit int) *ListDeploymentParams { 103 params.Limit = &Limit 104 return params 105 } 106 107 // Retrieve a single page of Deployment records from the API. Request is executed immediately. 108 func (c *ApiService) PageDeployment(ServiceSid string, EnvironmentSid string, params *ListDeploymentParams, pageToken, pageNumber string) (*ListDeploymentResponse, error) { 109 path := "/v1/Services/{ServiceSid}/Environments/{EnvironmentSid}/Deployments" 110 111 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 112 path = strings.Replace(path, "{"+"EnvironmentSid"+"}", EnvironmentSid, -1) 113 114 data := url.Values{} 115 headers := make(map[string]interface{}) 116 117 if params != nil && params.PageSize != nil { 118 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 119 } 120 121 if pageToken != "" { 122 data.Set("PageToken", pageToken) 123 } 124 if pageNumber != "" { 125 data.Set("Page", pageNumber) 126 } 127 128 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 129 if err != nil { 130 return nil, err 131 } 132 133 defer resp.Body.Close() 134 135 ps := &ListDeploymentResponse{} 136 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 137 return nil, err 138 } 139 140 return ps, err 141 } 142 143 // Lists Deployment records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 144 func (c *ApiService) ListDeployment(ServiceSid string, EnvironmentSid string, params *ListDeploymentParams) ([]ServerlessV1Deployment, error) { 145 response, errors := c.StreamDeployment(ServiceSid, EnvironmentSid, params) 146 147 records := make([]ServerlessV1Deployment, 0) 148 for record := range response { 149 records = append(records, record) 150 } 151 152 if err := <-errors; err != nil { 153 return nil, err 154 } 155 156 return records, nil 157 } 158 159 // Streams Deployment records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 160 func (c *ApiService) StreamDeployment(ServiceSid string, EnvironmentSid string, params *ListDeploymentParams) (chan ServerlessV1Deployment, chan error) { 161 if params == nil { 162 params = &ListDeploymentParams{} 163 } 164 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 165 166 recordChannel := make(chan ServerlessV1Deployment, 1) 167 errorChannel := make(chan error, 1) 168 169 response, err := c.PageDeployment(ServiceSid, EnvironmentSid, params, "", "") 170 if err != nil { 171 errorChannel <- err 172 close(recordChannel) 173 close(errorChannel) 174 } else { 175 go c.streamDeployment(response, params, recordChannel, errorChannel) 176 } 177 178 return recordChannel, errorChannel 179 } 180 181 func (c *ApiService) streamDeployment(response *ListDeploymentResponse, params *ListDeploymentParams, recordChannel chan ServerlessV1Deployment, errorChannel chan error) { 182 curRecord := 1 183 184 for response != nil { 185 responseRecords := response.Deployments 186 for item := range responseRecords { 187 recordChannel <- responseRecords[item] 188 curRecord += 1 189 if params.Limit != nil && *params.Limit < curRecord { 190 close(recordChannel) 191 close(errorChannel) 192 return 193 } 194 } 195 196 record, err := client.GetNext(c.baseURL, response, c.getNextListDeploymentResponse) 197 if err != nil { 198 errorChannel <- err 199 break 200 } else if record == nil { 201 break 202 } 203 204 response = record.(*ListDeploymentResponse) 205 } 206 207 close(recordChannel) 208 close(errorChannel) 209 } 210 211 func (c *ApiService) getNextListDeploymentResponse(nextPageUrl string) (interface{}, error) { 212 if nextPageUrl == "" { 213 return nil, nil 214 } 215 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 216 if err != nil { 217 return nil, err 218 } 219 220 defer resp.Body.Close() 221 222 ps := &ListDeploymentResponse{} 223 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 224 return nil, err 225 } 226 return ps, nil 227 }