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