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