github.com/openshift-online/ocm-sdk-go@v0.1.473/statusboard/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/statusboard/v1 21 22 import ( 23 "bufio" 24 "bytes" 25 "context" 26 "io" 27 "net/http" 28 "net/url" 29 "path" 30 "time" 31 32 "github.com/openshift-online/ocm-sdk-go/errors" 33 "github.com/openshift-online/ocm-sdk-go/helpers" 34 ) 35 36 // ProductClient is the client of the 'product' resource. 37 // 38 // Provides detailed information about a specific product. 39 type ProductClient struct { 40 transport http.RoundTripper 41 path string 42 } 43 44 // NewProductClient creates a new client for the 'product' 45 // resource using the given transport to send the requests and receive the 46 // responses. 47 func NewProductClient(transport http.RoundTripper, path string) *ProductClient { 48 return &ProductClient{ 49 transport: transport, 50 path: path, 51 } 52 } 53 54 // Delete creates a request for the 'delete' method. 55 func (c *ProductClient) Delete() *ProductDeleteRequest { 56 return &ProductDeleteRequest{ 57 transport: c.transport, 58 path: c.path, 59 } 60 } 61 62 // Get creates a request for the 'get' method. 63 func (c *ProductClient) Get() *ProductGetRequest { 64 return &ProductGetRequest{ 65 transport: c.transport, 66 path: c.path, 67 } 68 } 69 70 // Update creates a request for the 'update' method. 71 func (c *ProductClient) Update() *ProductUpdateRequest { 72 return &ProductUpdateRequest{ 73 transport: c.transport, 74 path: c.path, 75 } 76 } 77 78 // Applications returns the target 'applications' resource. 79 func (c *ProductClient) Applications() *ApplicationsClient { 80 return NewApplicationsClient( 81 c.transport, 82 path.Join(c.path, "applications"), 83 ) 84 } 85 86 // Updates returns the target 'statuses' resource. 87 func (c *ProductClient) Updates() *StatusesClient { 88 return NewStatusesClient( 89 c.transport, 90 path.Join(c.path, "updates"), 91 ) 92 } 93 94 // ProductPollRequest is the request for the Poll method. 95 type ProductPollRequest struct { 96 request *ProductGetRequest 97 interval time.Duration 98 statuses []int 99 predicates []func(interface{}) bool 100 } 101 102 // Parameter adds a query parameter to all the requests that will be used to retrieve the object. 103 func (r *ProductPollRequest) Parameter(name string, value interface{}) *ProductPollRequest { 104 r.request.Parameter(name, value) 105 return r 106 } 107 108 // Header adds a request header to all the requests that will be used to retrieve the object. 109 func (r *ProductPollRequest) Header(name string, value interface{}) *ProductPollRequest { 110 r.request.Header(name, value) 111 return r 112 } 113 114 // Interval sets the polling interval. This parameter is mandatory and must be greater than zero. 115 func (r *ProductPollRequest) Interval(value time.Duration) *ProductPollRequest { 116 r.interval = value 117 return r 118 } 119 120 // Status set the expected status of the response. Multiple values can be set calling this method 121 // multiple times. The response will be considered successful if the status is any of those values. 122 func (r *ProductPollRequest) Status(value int) *ProductPollRequest { 123 r.statuses = append(r.statuses, value) 124 return r 125 } 126 127 // Predicate adds a predicate that the response should satisfy be considered successful. Multiple 128 // predicates can be set calling this method multiple times. The response will be considered successful 129 // if all the predicates are satisfied. 130 func (r *ProductPollRequest) Predicate(value func(*ProductGetResponse) bool) *ProductPollRequest { 131 r.predicates = append(r.predicates, func(response interface{}) bool { 132 return value(response.(*ProductGetResponse)) 133 }) 134 return r 135 } 136 137 // StartContext starts the polling loop. Responses will be considered successful if the status is one of 138 // the values specified with the Status method and if all the predicates specified with the Predicate 139 // method return nil. 140 // 141 // The context must have a timeout or deadline, otherwise this method will immediately return an error. 142 func (r *ProductPollRequest) StartContext(ctx context.Context) (response *ProductPollResponse, err error) { 143 result, err := helpers.PollContext(ctx, r.interval, r.statuses, r.predicates, r.task) 144 if result != nil { 145 response = &ProductPollResponse{ 146 response: result.(*ProductGetResponse), 147 } 148 } 149 return 150 } 151 152 // task adapts the types of the request/response types so that they can be used with the generic 153 // polling function from the helpers package. 154 func (r *ProductPollRequest) task(ctx context.Context) (status int, result interface{}, err error) { 155 response, err := r.request.SendContext(ctx) 156 if response != nil { 157 status = response.Status() 158 result = response 159 } 160 return 161 } 162 163 // ProductPollResponse is the response for the Poll method. 164 type ProductPollResponse struct { 165 response *ProductGetResponse 166 } 167 168 // Status returns the response status code. 169 func (r *ProductPollResponse) Status() int { 170 if r == nil { 171 return 0 172 } 173 return r.response.Status() 174 } 175 176 // Header returns header of the response. 177 func (r *ProductPollResponse) Header() http.Header { 178 if r == nil { 179 return nil 180 } 181 return r.response.Header() 182 } 183 184 // Error returns the response error. 185 func (r *ProductPollResponse) Error() *errors.Error { 186 if r == nil { 187 return nil 188 } 189 return r.response.Error() 190 } 191 192 // Body returns the value of the 'body' parameter. 193 func (r *ProductPollResponse) Body() *Product { 194 return r.response.Body() 195 } 196 197 // GetBody returns the value of the 'body' parameter and 198 // a flag indicating if the parameter has a value. 199 func (r *ProductPollResponse) GetBody() (value *Product, ok bool) { 200 return r.response.GetBody() 201 } 202 203 // Poll creates a request to repeatedly retrieve the object till the response has one of a given set 204 // of states and satisfies a set of predicates. 205 func (c *ProductClient) Poll() *ProductPollRequest { 206 return &ProductPollRequest{ 207 request: c.Get(), 208 } 209 } 210 211 // ProductDeleteRequest is the request for the 'delete' method. 212 type ProductDeleteRequest struct { 213 transport http.RoundTripper 214 path string 215 query url.Values 216 header http.Header 217 } 218 219 // Parameter adds a query parameter. 220 func (r *ProductDeleteRequest) Parameter(name string, value interface{}) *ProductDeleteRequest { 221 helpers.AddValue(&r.query, name, value) 222 return r 223 } 224 225 // Header adds a request header. 226 func (r *ProductDeleteRequest) Header(name string, value interface{}) *ProductDeleteRequest { 227 helpers.AddHeader(&r.header, name, value) 228 return r 229 } 230 231 // Impersonate wraps requests on behalf of another user. 232 // Note: Services that do not support this feature may silently ignore this call. 233 func (r *ProductDeleteRequest) Impersonate(user string) *ProductDeleteRequest { 234 helpers.AddImpersonationHeader(&r.header, user) 235 return r 236 } 237 238 // Send sends this request, waits for the response, and returns it. 239 // 240 // This is a potentially lengthy operation, as it requires network communication. 241 // Consider using a context and the SendContext method. 242 func (r *ProductDeleteRequest) Send() (result *ProductDeleteResponse, err error) { 243 return r.SendContext(context.Background()) 244 } 245 246 // SendContext sends this request, waits for the response, and returns it. 247 func (r *ProductDeleteRequest) SendContext(ctx context.Context) (result *ProductDeleteResponse, err error) { 248 query := helpers.CopyQuery(r.query) 249 header := helpers.CopyHeader(r.header) 250 uri := &url.URL{ 251 Path: r.path, 252 RawQuery: query.Encode(), 253 } 254 request := &http.Request{ 255 Method: "DELETE", 256 URL: uri, 257 Header: header, 258 } 259 if ctx != nil { 260 request = request.WithContext(ctx) 261 } 262 response, err := r.transport.RoundTrip(request) 263 if err != nil { 264 return 265 } 266 defer response.Body.Close() 267 result = &ProductDeleteResponse{} 268 result.status = response.StatusCode 269 result.header = response.Header 270 reader := bufio.NewReader(response.Body) 271 _, err = reader.Peek(1) 272 if err == io.EOF { 273 err = nil 274 return 275 } 276 if result.status >= 400 { 277 result.err, err = errors.UnmarshalErrorStatus(reader, result.status) 278 if err != nil { 279 return 280 } 281 err = result.err 282 return 283 } 284 return 285 } 286 287 // ProductDeleteResponse is the response for the 'delete' method. 288 type ProductDeleteResponse struct { 289 status int 290 header http.Header 291 err *errors.Error 292 } 293 294 // Status returns the response status code. 295 func (r *ProductDeleteResponse) Status() int { 296 if r == nil { 297 return 0 298 } 299 return r.status 300 } 301 302 // Header returns header of the response. 303 func (r *ProductDeleteResponse) Header() http.Header { 304 if r == nil { 305 return nil 306 } 307 return r.header 308 } 309 310 // Error returns the response error. 311 func (r *ProductDeleteResponse) Error() *errors.Error { 312 if r == nil { 313 return nil 314 } 315 return r.err 316 } 317 318 // ProductGetRequest is the request for the 'get' method. 319 type ProductGetRequest struct { 320 transport http.RoundTripper 321 path string 322 query url.Values 323 header http.Header 324 } 325 326 // Parameter adds a query parameter. 327 func (r *ProductGetRequest) Parameter(name string, value interface{}) *ProductGetRequest { 328 helpers.AddValue(&r.query, name, value) 329 return r 330 } 331 332 // Header adds a request header. 333 func (r *ProductGetRequest) Header(name string, value interface{}) *ProductGetRequest { 334 helpers.AddHeader(&r.header, name, value) 335 return r 336 } 337 338 // Impersonate wraps requests on behalf of another user. 339 // Note: Services that do not support this feature may silently ignore this call. 340 func (r *ProductGetRequest) Impersonate(user string) *ProductGetRequest { 341 helpers.AddImpersonationHeader(&r.header, user) 342 return r 343 } 344 345 // Send sends this request, waits for the response, and returns it. 346 // 347 // This is a potentially lengthy operation, as it requires network communication. 348 // Consider using a context and the SendContext method. 349 func (r *ProductGetRequest) Send() (result *ProductGetResponse, err error) { 350 return r.SendContext(context.Background()) 351 } 352 353 // SendContext sends this request, waits for the response, and returns it. 354 func (r *ProductGetRequest) SendContext(ctx context.Context) (result *ProductGetResponse, err error) { 355 query := helpers.CopyQuery(r.query) 356 header := helpers.CopyHeader(r.header) 357 uri := &url.URL{ 358 Path: r.path, 359 RawQuery: query.Encode(), 360 } 361 request := &http.Request{ 362 Method: "GET", 363 URL: uri, 364 Header: header, 365 } 366 if ctx != nil { 367 request = request.WithContext(ctx) 368 } 369 response, err := r.transport.RoundTrip(request) 370 if err != nil { 371 return 372 } 373 defer response.Body.Close() 374 result = &ProductGetResponse{} 375 result.status = response.StatusCode 376 result.header = response.Header 377 reader := bufio.NewReader(response.Body) 378 _, err = reader.Peek(1) 379 if err == io.EOF { 380 err = nil 381 return 382 } 383 if result.status >= 400 { 384 result.err, err = errors.UnmarshalErrorStatus(reader, result.status) 385 if err != nil { 386 return 387 } 388 err = result.err 389 return 390 } 391 err = readProductGetResponse(result, reader) 392 if err != nil { 393 return 394 } 395 return 396 } 397 398 // ProductGetResponse is the response for the 'get' method. 399 type ProductGetResponse struct { 400 status int 401 header http.Header 402 err *errors.Error 403 body *Product 404 } 405 406 // Status returns the response status code. 407 func (r *ProductGetResponse) Status() int { 408 if r == nil { 409 return 0 410 } 411 return r.status 412 } 413 414 // Header returns header of the response. 415 func (r *ProductGetResponse) Header() http.Header { 416 if r == nil { 417 return nil 418 } 419 return r.header 420 } 421 422 // Error returns the response error. 423 func (r *ProductGetResponse) Error() *errors.Error { 424 if r == nil { 425 return nil 426 } 427 return r.err 428 } 429 430 // Body returns the value of the 'body' parameter. 431 func (r *ProductGetResponse) Body() *Product { 432 if r == nil { 433 return nil 434 } 435 return r.body 436 } 437 438 // GetBody returns the value of the 'body' parameter and 439 // a flag indicating if the parameter has a value. 440 func (r *ProductGetResponse) GetBody() (value *Product, ok bool) { 441 ok = r != nil && r.body != nil 442 if ok { 443 value = r.body 444 } 445 return 446 } 447 448 // ProductUpdateRequest is the request for the 'update' method. 449 type ProductUpdateRequest struct { 450 transport http.RoundTripper 451 path string 452 query url.Values 453 header http.Header 454 body *Product 455 } 456 457 // Parameter adds a query parameter. 458 func (r *ProductUpdateRequest) Parameter(name string, value interface{}) *ProductUpdateRequest { 459 helpers.AddValue(&r.query, name, value) 460 return r 461 } 462 463 // Header adds a request header. 464 func (r *ProductUpdateRequest) Header(name string, value interface{}) *ProductUpdateRequest { 465 helpers.AddHeader(&r.header, name, value) 466 return r 467 } 468 469 // Impersonate wraps requests on behalf of another user. 470 // Note: Services that do not support this feature may silently ignore this call. 471 func (r *ProductUpdateRequest) Impersonate(user string) *ProductUpdateRequest { 472 helpers.AddImpersonationHeader(&r.header, user) 473 return r 474 } 475 476 // Body sets the value of the 'body' parameter. 477 func (r *ProductUpdateRequest) Body(value *Product) *ProductUpdateRequest { 478 r.body = value 479 return r 480 } 481 482 // Send sends this request, waits for the response, and returns it. 483 // 484 // This is a potentially lengthy operation, as it requires network communication. 485 // Consider using a context and the SendContext method. 486 func (r *ProductUpdateRequest) Send() (result *ProductUpdateResponse, err error) { 487 return r.SendContext(context.Background()) 488 } 489 490 // SendContext sends this request, waits for the response, and returns it. 491 func (r *ProductUpdateRequest) SendContext(ctx context.Context) (result *ProductUpdateResponse, err error) { 492 query := helpers.CopyQuery(r.query) 493 header := helpers.CopyHeader(r.header) 494 buffer := &bytes.Buffer{} 495 err = writeProductUpdateRequest(r, buffer) 496 if err != nil { 497 return 498 } 499 uri := &url.URL{ 500 Path: r.path, 501 RawQuery: query.Encode(), 502 } 503 request := &http.Request{ 504 Method: "PATCH", 505 URL: uri, 506 Header: header, 507 Body: io.NopCloser(buffer), 508 } 509 if ctx != nil { 510 request = request.WithContext(ctx) 511 } 512 response, err := r.transport.RoundTrip(request) 513 if err != nil { 514 return 515 } 516 defer response.Body.Close() 517 result = &ProductUpdateResponse{} 518 result.status = response.StatusCode 519 result.header = response.Header 520 reader := bufio.NewReader(response.Body) 521 _, err = reader.Peek(1) 522 if err == io.EOF { 523 err = nil 524 return 525 } 526 if result.status >= 400 { 527 result.err, err = errors.UnmarshalErrorStatus(reader, result.status) 528 if err != nil { 529 return 530 } 531 err = result.err 532 return 533 } 534 err = readProductUpdateResponse(result, reader) 535 if err != nil { 536 return 537 } 538 return 539 } 540 541 // ProductUpdateResponse is the response for the 'update' method. 542 type ProductUpdateResponse struct { 543 status int 544 header http.Header 545 err *errors.Error 546 body *Product 547 } 548 549 // Status returns the response status code. 550 func (r *ProductUpdateResponse) Status() int { 551 if r == nil { 552 return 0 553 } 554 return r.status 555 } 556 557 // Header returns header of the response. 558 func (r *ProductUpdateResponse) Header() http.Header { 559 if r == nil { 560 return nil 561 } 562 return r.header 563 } 564 565 // Error returns the response error. 566 func (r *ProductUpdateResponse) Error() *errors.Error { 567 if r == nil { 568 return nil 569 } 570 return r.err 571 } 572 573 // Body returns the value of the 'body' parameter. 574 func (r *ProductUpdateResponse) Body() *Product { 575 if r == nil { 576 return nil 577 } 578 return r.body 579 } 580 581 // GetBody returns the value of the 'body' parameter and 582 // a flag indicating if the parameter has a value. 583 func (r *ProductUpdateResponse) GetBody() (value *Product, ok bool) { 584 ok = r != nil && r.body != nil 585 if ok { 586 value = r.body 587 } 588 return 589 }