github.com/openshift-online/ocm-sdk-go@v0.1.473/statusboard/v1/status_update_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  	"time"
    30  
    31  	"github.com/openshift-online/ocm-sdk-go/errors"
    32  	"github.com/openshift-online/ocm-sdk-go/helpers"
    33  )
    34  
    35  // StatusUpdateClient is the client of the 'status_update' resource.
    36  //
    37  // Provides detailed information about the specified status.
    38  type StatusUpdateClient struct {
    39  	transport http.RoundTripper
    40  	path      string
    41  }
    42  
    43  // NewStatusUpdateClient creates a new client for the 'status_update'
    44  // resource using the given transport to send the requests and receive the
    45  // responses.
    46  func NewStatusUpdateClient(transport http.RoundTripper, path string) *StatusUpdateClient {
    47  	return &StatusUpdateClient{
    48  		transport: transport,
    49  		path:      path,
    50  	}
    51  }
    52  
    53  // Delete creates a request for the 'delete' method.
    54  func (c *StatusUpdateClient) Delete() *StatusUpdateDeleteRequest {
    55  	return &StatusUpdateDeleteRequest{
    56  		transport: c.transport,
    57  		path:      c.path,
    58  	}
    59  }
    60  
    61  // Get creates a request for the 'get' method.
    62  func (c *StatusUpdateClient) Get() *StatusUpdateGetRequest {
    63  	return &StatusUpdateGetRequest{
    64  		transport: c.transport,
    65  		path:      c.path,
    66  	}
    67  }
    68  
    69  // Update creates a request for the 'update' method.
    70  func (c *StatusUpdateClient) Update() *StatusUpdateUpdateRequest {
    71  	return &StatusUpdateUpdateRequest{
    72  		transport: c.transport,
    73  		path:      c.path,
    74  	}
    75  }
    76  
    77  // StatusUpdatePollRequest is the request for the Poll method.
    78  type StatusUpdatePollRequest struct {
    79  	request    *StatusUpdateGetRequest
    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 *StatusUpdatePollRequest) Parameter(name string, value interface{}) *StatusUpdatePollRequest {
    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 *StatusUpdatePollRequest) Header(name string, value interface{}) *StatusUpdatePollRequest {
    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 *StatusUpdatePollRequest) Interval(value time.Duration) *StatusUpdatePollRequest {
    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 *StatusUpdatePollRequest) Status(value int) *StatusUpdatePollRequest {
   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 *StatusUpdatePollRequest) Predicate(value func(*StatusUpdateGetResponse) bool) *StatusUpdatePollRequest {
   114  	r.predicates = append(r.predicates, func(response interface{}) bool {
   115  		return value(response.(*StatusUpdateGetResponse))
   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 *StatusUpdatePollRequest) StartContext(ctx context.Context) (response *StatusUpdatePollResponse, err error) {
   126  	result, err := helpers.PollContext(ctx, r.interval, r.statuses, r.predicates, r.task)
   127  	if result != nil {
   128  		response = &StatusUpdatePollResponse{
   129  			response: result.(*StatusUpdateGetResponse),
   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 *StatusUpdatePollRequest) 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  // StatusUpdatePollResponse is the response for the Poll method.
   147  type StatusUpdatePollResponse struct {
   148  	response *StatusUpdateGetResponse
   149  }
   150  
   151  // Status returns the response status code.
   152  func (r *StatusUpdatePollResponse) 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 *StatusUpdatePollResponse) 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 *StatusUpdatePollResponse) 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 *StatusUpdatePollResponse) Body() *Status {
   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 *StatusUpdatePollResponse) GetBody() (value *Status, 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 *StatusUpdateClient) Poll() *StatusUpdatePollRequest {
   189  	return &StatusUpdatePollRequest{
   190  		request: c.Get(),
   191  	}
   192  }
   193  
   194  // StatusUpdateDeleteRequest is the request for the 'delete' method.
   195  type StatusUpdateDeleteRequest 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 *StatusUpdateDeleteRequest) Parameter(name string, value interface{}) *StatusUpdateDeleteRequest {
   204  	helpers.AddValue(&r.query, name, value)
   205  	return r
   206  }
   207  
   208  // Header adds a request header.
   209  func (r *StatusUpdateDeleteRequest) Header(name string, value interface{}) *StatusUpdateDeleteRequest {
   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 *StatusUpdateDeleteRequest) Impersonate(user string) *StatusUpdateDeleteRequest {
   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 *StatusUpdateDeleteRequest) Send() (result *StatusUpdateDeleteResponse, 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 *StatusUpdateDeleteRequest) SendContext(ctx context.Context) (result *StatusUpdateDeleteResponse, 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 = &StatusUpdateDeleteResponse{}
   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  // StatusUpdateDeleteResponse is the response for the 'delete' method.
   271  type StatusUpdateDeleteResponse struct {
   272  	status int
   273  	header http.Header
   274  	err    *errors.Error
   275  }
   276  
   277  // Status returns the response status code.
   278  func (r *StatusUpdateDeleteResponse) 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 *StatusUpdateDeleteResponse) 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 *StatusUpdateDeleteResponse) Error() *errors.Error {
   295  	if r == nil {
   296  		return nil
   297  	}
   298  	return r.err
   299  }
   300  
   301  // StatusUpdateGetRequest is the request for the 'get' method.
   302  type StatusUpdateGetRequest 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 *StatusUpdateGetRequest) Parameter(name string, value interface{}) *StatusUpdateGetRequest {
   311  	helpers.AddValue(&r.query, name, value)
   312  	return r
   313  }
   314  
   315  // Header adds a request header.
   316  func (r *StatusUpdateGetRequest) Header(name string, value interface{}) *StatusUpdateGetRequest {
   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 *StatusUpdateGetRequest) Impersonate(user string) *StatusUpdateGetRequest {
   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 *StatusUpdateGetRequest) Send() (result *StatusUpdateGetResponse, 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 *StatusUpdateGetRequest) SendContext(ctx context.Context) (result *StatusUpdateGetResponse, 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 = &StatusUpdateGetResponse{}
   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 = readStatusUpdateGetResponse(result, reader)
   375  	if err != nil {
   376  		return
   377  	}
   378  	return
   379  }
   380  
   381  // StatusUpdateGetResponse is the response for the 'get' method.
   382  type StatusUpdateGetResponse struct {
   383  	status int
   384  	header http.Header
   385  	err    *errors.Error
   386  	body   *Status
   387  }
   388  
   389  // Status returns the response status code.
   390  func (r *StatusUpdateGetResponse) 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 *StatusUpdateGetResponse) 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 *StatusUpdateGetResponse) 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 *StatusUpdateGetResponse) Body() *Status {
   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 *StatusUpdateGetResponse) GetBody() (value *Status, ok bool) {
   424  	ok = r != nil && r.body != nil
   425  	if ok {
   426  		value = r.body
   427  	}
   428  	return
   429  }
   430  
   431  // StatusUpdateUpdateRequest is the request for the 'update' method.
   432  type StatusUpdateUpdateRequest struct {
   433  	transport http.RoundTripper
   434  	path      string
   435  	query     url.Values
   436  	header    http.Header
   437  	body      *Status
   438  }
   439  
   440  // Parameter adds a query parameter.
   441  func (r *StatusUpdateUpdateRequest) Parameter(name string, value interface{}) *StatusUpdateUpdateRequest {
   442  	helpers.AddValue(&r.query, name, value)
   443  	return r
   444  }
   445  
   446  // Header adds a request header.
   447  func (r *StatusUpdateUpdateRequest) Header(name string, value interface{}) *StatusUpdateUpdateRequest {
   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 *StatusUpdateUpdateRequest) Impersonate(user string) *StatusUpdateUpdateRequest {
   455  	helpers.AddImpersonationHeader(&r.header, user)
   456  	return r
   457  }
   458  
   459  // Body sets the value of the 'body' parameter.
   460  func (r *StatusUpdateUpdateRequest) Body(value *Status) *StatusUpdateUpdateRequest {
   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 *StatusUpdateUpdateRequest) Send() (result *StatusUpdateUpdateResponse, 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 *StatusUpdateUpdateRequest) SendContext(ctx context.Context) (result *StatusUpdateUpdateResponse, err error) {
   475  	query := helpers.CopyQuery(r.query)
   476  	header := helpers.CopyHeader(r.header)
   477  	buffer := &bytes.Buffer{}
   478  	err = writeStatusUpdateUpdateRequest(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 = &StatusUpdateUpdateResponse{}
   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 = readStatusUpdateUpdateResponse(result, reader)
   518  	if err != nil {
   519  		return
   520  	}
   521  	return
   522  }
   523  
   524  // StatusUpdateUpdateResponse is the response for the 'update' method.
   525  type StatusUpdateUpdateResponse struct {
   526  	status int
   527  	header http.Header
   528  	err    *errors.Error
   529  	body   *Status
   530  }
   531  
   532  // Status returns the response status code.
   533  func (r *StatusUpdateUpdateResponse) 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 *StatusUpdateUpdateResponse) 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 *StatusUpdateUpdateResponse) 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 *StatusUpdateUpdateResponse) Body() *Status {
   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 *StatusUpdateUpdateResponse) GetBody() (value *Status, ok bool) {
   567  	ok = r != nil && r.body != nil
   568  	if ok {
   569  		value = r.body
   570  	}
   571  	return
   572  }