github.com/openshift-online/ocm-sdk-go@v0.1.473/clustersmgmt/v1/product_client.go (about) 1 /* 2 Copyright (c) 2020 Red Hat, Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // IMPORTANT: This file has been generated automatically, refrain from modifying it manually as all 18 // your changes will be lost when the file is generated again. 19 20 package v1 // github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1 21 22 import ( 23 "bufio" 24 "context" 25 "io" 26 "net/http" 27 "net/url" 28 "path" 29 "time" 30 31 "github.com/openshift-online/ocm-sdk-go/errors" 32 "github.com/openshift-online/ocm-sdk-go/helpers" 33 ) 34 35 // ProductClient is the client of the 'product' resource. 36 // 37 // Manages a specific product. 38 type ProductClient struct { 39 transport http.RoundTripper 40 path string 41 } 42 43 // NewProductClient creates a new client for the 'product' 44 // resource using the given transport to send the requests and receive the 45 // responses. 46 func NewProductClient(transport http.RoundTripper, path string) *ProductClient { 47 return &ProductClient{ 48 transport: transport, 49 path: path, 50 } 51 } 52 53 // Get creates a request for the 'get' method. 54 // 55 // Retrieves the details of the product. 56 func (c *ProductClient) Get() *ProductGetRequest { 57 return &ProductGetRequest{ 58 transport: c.transport, 59 path: c.path, 60 } 61 } 62 63 // MinimalVersions returns the target 'product_minimal_versions' resource. 64 func (c *ProductClient) MinimalVersions() *ProductMinimalVersionsClient { 65 return NewProductMinimalVersionsClient( 66 c.transport, 67 path.Join(c.path, "minimal_versions"), 68 ) 69 } 70 71 // TechnologyPreviews returns the target 'product_technology_previews' resource. 72 func (c *ProductClient) TechnologyPreviews() *ProductTechnologyPreviewsClient { 73 return NewProductTechnologyPreviewsClient( 74 c.transport, 75 path.Join(c.path, "technology_previews"), 76 ) 77 } 78 79 // ProductPollRequest is the request for the Poll method. 80 type ProductPollRequest struct { 81 request *ProductGetRequest 82 interval time.Duration 83 statuses []int 84 predicates []func(interface{}) bool 85 } 86 87 // Parameter adds a query parameter to all the requests that will be used to retrieve the object. 88 func (r *ProductPollRequest) Parameter(name string, value interface{}) *ProductPollRequest { 89 r.request.Parameter(name, value) 90 return r 91 } 92 93 // Header adds a request header to all the requests that will be used to retrieve the object. 94 func (r *ProductPollRequest) Header(name string, value interface{}) *ProductPollRequest { 95 r.request.Header(name, value) 96 return r 97 } 98 99 // Interval sets the polling interval. This parameter is mandatory and must be greater than zero. 100 func (r *ProductPollRequest) Interval(value time.Duration) *ProductPollRequest { 101 r.interval = value 102 return r 103 } 104 105 // Status set the expected status of the response. Multiple values can be set calling this method 106 // multiple times. The response will be considered successful if the status is any of those values. 107 func (r *ProductPollRequest) Status(value int) *ProductPollRequest { 108 r.statuses = append(r.statuses, value) 109 return r 110 } 111 112 // Predicate adds a predicate that the response should satisfy be considered successful. Multiple 113 // predicates can be set calling this method multiple times. The response will be considered successful 114 // if all the predicates are satisfied. 115 func (r *ProductPollRequest) Predicate(value func(*ProductGetResponse) bool) *ProductPollRequest { 116 r.predicates = append(r.predicates, func(response interface{}) bool { 117 return value(response.(*ProductGetResponse)) 118 }) 119 return r 120 } 121 122 // StartContext starts the polling loop. Responses will be considered successful if the status is one of 123 // the values specified with the Status method and if all the predicates specified with the Predicate 124 // method return nil. 125 // 126 // The context must have a timeout or deadline, otherwise this method will immediately return an error. 127 func (r *ProductPollRequest) StartContext(ctx context.Context) (response *ProductPollResponse, err error) { 128 result, err := helpers.PollContext(ctx, r.interval, r.statuses, r.predicates, r.task) 129 if result != nil { 130 response = &ProductPollResponse{ 131 response: result.(*ProductGetResponse), 132 } 133 } 134 return 135 } 136 137 // task adapts the types of the request/response types so that they can be used with the generic 138 // polling function from the helpers package. 139 func (r *ProductPollRequest) task(ctx context.Context) (status int, result interface{}, err error) { 140 response, err := r.request.SendContext(ctx) 141 if response != nil { 142 status = response.Status() 143 result = response 144 } 145 return 146 } 147 148 // ProductPollResponse is the response for the Poll method. 149 type ProductPollResponse struct { 150 response *ProductGetResponse 151 } 152 153 // Status returns the response status code. 154 func (r *ProductPollResponse) Status() int { 155 if r == nil { 156 return 0 157 } 158 return r.response.Status() 159 } 160 161 // Header returns header of the response. 162 func (r *ProductPollResponse) Header() http.Header { 163 if r == nil { 164 return nil 165 } 166 return r.response.Header() 167 } 168 169 // Error returns the response error. 170 func (r *ProductPollResponse) Error() *errors.Error { 171 if r == nil { 172 return nil 173 } 174 return r.response.Error() 175 } 176 177 // Body returns the value of the 'body' parameter. 178 func (r *ProductPollResponse) Body() *Product { 179 return r.response.Body() 180 } 181 182 // GetBody returns the value of the 'body' parameter and 183 // a flag indicating if the parameter has a value. 184 func (r *ProductPollResponse) GetBody() (value *Product, ok bool) { 185 return r.response.GetBody() 186 } 187 188 // Poll creates a request to repeatedly retrieve the object till the response has one of a given set 189 // of states and satisfies a set of predicates. 190 func (c *ProductClient) Poll() *ProductPollRequest { 191 return &ProductPollRequest{ 192 request: c.Get(), 193 } 194 } 195 196 // ProductGetRequest is the request for the 'get' method. 197 type ProductGetRequest struct { 198 transport http.RoundTripper 199 path string 200 query url.Values 201 header http.Header 202 } 203 204 // Parameter adds a query parameter. 205 func (r *ProductGetRequest) Parameter(name string, value interface{}) *ProductGetRequest { 206 helpers.AddValue(&r.query, name, value) 207 return r 208 } 209 210 // Header adds a request header. 211 func (r *ProductGetRequest) Header(name string, value interface{}) *ProductGetRequest { 212 helpers.AddHeader(&r.header, name, value) 213 return r 214 } 215 216 // Impersonate wraps requests on behalf of another user. 217 // Note: Services that do not support this feature may silently ignore this call. 218 func (r *ProductGetRequest) Impersonate(user string) *ProductGetRequest { 219 helpers.AddImpersonationHeader(&r.header, user) 220 return r 221 } 222 223 // Send sends this request, waits for the response, and returns it. 224 // 225 // This is a potentially lengthy operation, as it requires network communication. 226 // Consider using a context and the SendContext method. 227 func (r *ProductGetRequest) Send() (result *ProductGetResponse, err error) { 228 return r.SendContext(context.Background()) 229 } 230 231 // SendContext sends this request, waits for the response, and returns it. 232 func (r *ProductGetRequest) SendContext(ctx context.Context) (result *ProductGetResponse, err error) { 233 query := helpers.CopyQuery(r.query) 234 header := helpers.CopyHeader(r.header) 235 uri := &url.URL{ 236 Path: r.path, 237 RawQuery: query.Encode(), 238 } 239 request := &http.Request{ 240 Method: "GET", 241 URL: uri, 242 Header: header, 243 } 244 if ctx != nil { 245 request = request.WithContext(ctx) 246 } 247 response, err := r.transport.RoundTrip(request) 248 if err != nil { 249 return 250 } 251 defer response.Body.Close() 252 result = &ProductGetResponse{} 253 result.status = response.StatusCode 254 result.header = response.Header 255 reader := bufio.NewReader(response.Body) 256 _, err = reader.Peek(1) 257 if err == io.EOF { 258 err = nil 259 return 260 } 261 if result.status >= 400 { 262 result.err, err = errors.UnmarshalErrorStatus(reader, result.status) 263 if err != nil { 264 return 265 } 266 err = result.err 267 return 268 } 269 err = readProductGetResponse(result, reader) 270 if err != nil { 271 return 272 } 273 return 274 } 275 276 // ProductGetResponse is the response for the 'get' method. 277 type ProductGetResponse struct { 278 status int 279 header http.Header 280 err *errors.Error 281 body *Product 282 } 283 284 // Status returns the response status code. 285 func (r *ProductGetResponse) Status() int { 286 if r == nil { 287 return 0 288 } 289 return r.status 290 } 291 292 // Header returns header of the response. 293 func (r *ProductGetResponse) Header() http.Header { 294 if r == nil { 295 return nil 296 } 297 return r.header 298 } 299 300 // Error returns the response error. 301 func (r *ProductGetResponse) Error() *errors.Error { 302 if r == nil { 303 return nil 304 } 305 return r.err 306 } 307 308 // Body returns the value of the 'body' parameter. 309 func (r *ProductGetResponse) Body() *Product { 310 if r == nil { 311 return nil 312 } 313 return r.body 314 } 315 316 // GetBody returns the value of the 'body' parameter and 317 // a flag indicating if the parameter has a value. 318 func (r *ProductGetResponse) GetBody() (value *Product, ok bool) { 319 ok = r != nil && r.body != nil 320 if ok { 321 value = r.body 322 } 323 return 324 }