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