github.com/openshift-online/ocm-sdk-go@v0.1.473/servicelogs/v1/log_entry_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/servicelogs/v1
    21  
    22  import (
    23  	"bufio"
    24  	"context"
    25  	"io"
    26  	"net/http"
    27  	"net/url"
    28  	"time"
    29  
    30  	"github.com/openshift-online/ocm-sdk-go/errors"
    31  	"github.com/openshift-online/ocm-sdk-go/helpers"
    32  )
    33  
    34  // LogEntryClient is the client of the 'log_entry' resource.
    35  //
    36  // Manages a specific log entry.
    37  type LogEntryClient struct {
    38  	transport http.RoundTripper
    39  	path      string
    40  }
    41  
    42  // NewLogEntryClient creates a new client for the 'log_entry'
    43  // resource using the given transport to send the requests and receive the
    44  // responses.
    45  func NewLogEntryClient(transport http.RoundTripper, path string) *LogEntryClient {
    46  	return &LogEntryClient{
    47  		transport: transport,
    48  		path:      path,
    49  	}
    50  }
    51  
    52  // Delete creates a request for the 'delete' method.
    53  //
    54  // Deletes the log entry.
    55  func (c *LogEntryClient) Delete() *LogEntryDeleteRequest {
    56  	return &LogEntryDeleteRequest{
    57  		transport: c.transport,
    58  		path:      c.path,
    59  	}
    60  }
    61  
    62  // Get creates a request for the 'get' method.
    63  //
    64  // Retrieves the details of the log entry.
    65  func (c *LogEntryClient) Get() *LogEntryGetRequest {
    66  	return &LogEntryGetRequest{
    67  		transport: c.transport,
    68  		path:      c.path,
    69  	}
    70  }
    71  
    72  // LogEntryPollRequest is the request for the Poll method.
    73  type LogEntryPollRequest struct {
    74  	request    *LogEntryGetRequest
    75  	interval   time.Duration
    76  	statuses   []int
    77  	predicates []func(interface{}) bool
    78  }
    79  
    80  // Parameter adds a query parameter to all the requests that will be used to retrieve the object.
    81  func (r *LogEntryPollRequest) Parameter(name string, value interface{}) *LogEntryPollRequest {
    82  	r.request.Parameter(name, value)
    83  	return r
    84  }
    85  
    86  // Header adds a request header to all the requests that will be used to retrieve the object.
    87  func (r *LogEntryPollRequest) Header(name string, value interface{}) *LogEntryPollRequest {
    88  	r.request.Header(name, value)
    89  	return r
    90  }
    91  
    92  // Interval sets the polling interval. This parameter is mandatory and must be greater than zero.
    93  func (r *LogEntryPollRequest) Interval(value time.Duration) *LogEntryPollRequest {
    94  	r.interval = value
    95  	return r
    96  }
    97  
    98  // Status set the expected status of the response. Multiple values can be set calling this method
    99  // multiple times. The response will be considered successful if the status is any of those values.
   100  func (r *LogEntryPollRequest) Status(value int) *LogEntryPollRequest {
   101  	r.statuses = append(r.statuses, value)
   102  	return r
   103  }
   104  
   105  // Predicate adds a predicate that the response should satisfy be considered successful. Multiple
   106  // predicates can be set calling this method multiple times. The response will be considered successful
   107  // if all the predicates are satisfied.
   108  func (r *LogEntryPollRequest) Predicate(value func(*LogEntryGetResponse) bool) *LogEntryPollRequest {
   109  	r.predicates = append(r.predicates, func(response interface{}) bool {
   110  		return value(response.(*LogEntryGetResponse))
   111  	})
   112  	return r
   113  }
   114  
   115  // StartContext starts the polling loop. Responses will be considered successful if the status is one of
   116  // the values specified with the Status method and if all the predicates specified with the Predicate
   117  // method return nil.
   118  //
   119  // The context must have a timeout or deadline, otherwise this method will immediately return an error.
   120  func (r *LogEntryPollRequest) StartContext(ctx context.Context) (response *LogEntryPollResponse, err error) {
   121  	result, err := helpers.PollContext(ctx, r.interval, r.statuses, r.predicates, r.task)
   122  	if result != nil {
   123  		response = &LogEntryPollResponse{
   124  			response: result.(*LogEntryGetResponse),
   125  		}
   126  	}
   127  	return
   128  }
   129  
   130  // task adapts the types of the request/response types so that they can be used with the generic
   131  // polling function from the helpers package.
   132  func (r *LogEntryPollRequest) task(ctx context.Context) (status int, result interface{}, err error) {
   133  	response, err := r.request.SendContext(ctx)
   134  	if response != nil {
   135  		status = response.Status()
   136  		result = response
   137  	}
   138  	return
   139  }
   140  
   141  // LogEntryPollResponse is the response for the Poll method.
   142  type LogEntryPollResponse struct {
   143  	response *LogEntryGetResponse
   144  }
   145  
   146  // Status returns the response status code.
   147  func (r *LogEntryPollResponse) Status() int {
   148  	if r == nil {
   149  		return 0
   150  	}
   151  	return r.response.Status()
   152  }
   153  
   154  // Header returns header of the response.
   155  func (r *LogEntryPollResponse) Header() http.Header {
   156  	if r == nil {
   157  		return nil
   158  	}
   159  	return r.response.Header()
   160  }
   161  
   162  // Error returns the response error.
   163  func (r *LogEntryPollResponse) Error() *errors.Error {
   164  	if r == nil {
   165  		return nil
   166  	}
   167  	return r.response.Error()
   168  }
   169  
   170  // Body returns the value of the 'body' parameter.
   171  func (r *LogEntryPollResponse) Body() *LogEntry {
   172  	return r.response.Body()
   173  }
   174  
   175  // GetBody returns the value of the 'body' parameter and
   176  // a flag indicating if the parameter has a value.
   177  func (r *LogEntryPollResponse) GetBody() (value *LogEntry, ok bool) {
   178  	return r.response.GetBody()
   179  }
   180  
   181  // Poll creates a request to repeatedly retrieve the object till the response has one of a given set
   182  // of states and satisfies a set of predicates.
   183  func (c *LogEntryClient) Poll() *LogEntryPollRequest {
   184  	return &LogEntryPollRequest{
   185  		request: c.Get(),
   186  	}
   187  }
   188  
   189  // LogEntryDeleteRequest is the request for the 'delete' method.
   190  type LogEntryDeleteRequest struct {
   191  	transport http.RoundTripper
   192  	path      string
   193  	query     url.Values
   194  	header    http.Header
   195  }
   196  
   197  // Parameter adds a query parameter.
   198  func (r *LogEntryDeleteRequest) Parameter(name string, value interface{}) *LogEntryDeleteRequest {
   199  	helpers.AddValue(&r.query, name, value)
   200  	return r
   201  }
   202  
   203  // Header adds a request header.
   204  func (r *LogEntryDeleteRequest) Header(name string, value interface{}) *LogEntryDeleteRequest {
   205  	helpers.AddHeader(&r.header, name, value)
   206  	return r
   207  }
   208  
   209  // Impersonate wraps requests on behalf of another user.
   210  // Note: Services that do not support this feature may silently ignore this call.
   211  func (r *LogEntryDeleteRequest) Impersonate(user string) *LogEntryDeleteRequest {
   212  	helpers.AddImpersonationHeader(&r.header, user)
   213  	return r
   214  }
   215  
   216  // Send sends this request, waits for the response, and returns it.
   217  //
   218  // This is a potentially lengthy operation, as it requires network communication.
   219  // Consider using a context and the SendContext method.
   220  func (r *LogEntryDeleteRequest) Send() (result *LogEntryDeleteResponse, err error) {
   221  	return r.SendContext(context.Background())
   222  }
   223  
   224  // SendContext sends this request, waits for the response, and returns it.
   225  func (r *LogEntryDeleteRequest) SendContext(ctx context.Context) (result *LogEntryDeleteResponse, err error) {
   226  	query := helpers.CopyQuery(r.query)
   227  	header := helpers.CopyHeader(r.header)
   228  	uri := &url.URL{
   229  		Path:     r.path,
   230  		RawQuery: query.Encode(),
   231  	}
   232  	request := &http.Request{
   233  		Method: "DELETE",
   234  		URL:    uri,
   235  		Header: header,
   236  	}
   237  	if ctx != nil {
   238  		request = request.WithContext(ctx)
   239  	}
   240  	response, err := r.transport.RoundTrip(request)
   241  	if err != nil {
   242  		return
   243  	}
   244  	defer response.Body.Close()
   245  	result = &LogEntryDeleteResponse{}
   246  	result.status = response.StatusCode
   247  	result.header = response.Header
   248  	reader := bufio.NewReader(response.Body)
   249  	_, err = reader.Peek(1)
   250  	if err == io.EOF {
   251  		err = nil
   252  		return
   253  	}
   254  	if result.status >= 400 {
   255  		result.err, err = errors.UnmarshalErrorStatus(reader, result.status)
   256  		if err != nil {
   257  			return
   258  		}
   259  		err = result.err
   260  		return
   261  	}
   262  	return
   263  }
   264  
   265  // LogEntryDeleteResponse is the response for the 'delete' method.
   266  type LogEntryDeleteResponse struct {
   267  	status int
   268  	header http.Header
   269  	err    *errors.Error
   270  }
   271  
   272  // Status returns the response status code.
   273  func (r *LogEntryDeleteResponse) Status() int {
   274  	if r == nil {
   275  		return 0
   276  	}
   277  	return r.status
   278  }
   279  
   280  // Header returns header of the response.
   281  func (r *LogEntryDeleteResponse) Header() http.Header {
   282  	if r == nil {
   283  		return nil
   284  	}
   285  	return r.header
   286  }
   287  
   288  // Error returns the response error.
   289  func (r *LogEntryDeleteResponse) Error() *errors.Error {
   290  	if r == nil {
   291  		return nil
   292  	}
   293  	return r.err
   294  }
   295  
   296  // LogEntryGetRequest is the request for the 'get' method.
   297  type LogEntryGetRequest struct {
   298  	transport http.RoundTripper
   299  	path      string
   300  	query     url.Values
   301  	header    http.Header
   302  }
   303  
   304  // Parameter adds a query parameter.
   305  func (r *LogEntryGetRequest) Parameter(name string, value interface{}) *LogEntryGetRequest {
   306  	helpers.AddValue(&r.query, name, value)
   307  	return r
   308  }
   309  
   310  // Header adds a request header.
   311  func (r *LogEntryGetRequest) Header(name string, value interface{}) *LogEntryGetRequest {
   312  	helpers.AddHeader(&r.header, name, value)
   313  	return r
   314  }
   315  
   316  // Impersonate wraps requests on behalf of another user.
   317  // Note: Services that do not support this feature may silently ignore this call.
   318  func (r *LogEntryGetRequest) Impersonate(user string) *LogEntryGetRequest {
   319  	helpers.AddImpersonationHeader(&r.header, user)
   320  	return r
   321  }
   322  
   323  // Send sends this request, waits for the response, and returns it.
   324  //
   325  // This is a potentially lengthy operation, as it requires network communication.
   326  // Consider using a context and the SendContext method.
   327  func (r *LogEntryGetRequest) Send() (result *LogEntryGetResponse, err error) {
   328  	return r.SendContext(context.Background())
   329  }
   330  
   331  // SendContext sends this request, waits for the response, and returns it.
   332  func (r *LogEntryGetRequest) SendContext(ctx context.Context) (result *LogEntryGetResponse, err error) {
   333  	query := helpers.CopyQuery(r.query)
   334  	header := helpers.CopyHeader(r.header)
   335  	uri := &url.URL{
   336  		Path:     r.path,
   337  		RawQuery: query.Encode(),
   338  	}
   339  	request := &http.Request{
   340  		Method: "GET",
   341  		URL:    uri,
   342  		Header: header,
   343  	}
   344  	if ctx != nil {
   345  		request = request.WithContext(ctx)
   346  	}
   347  	response, err := r.transport.RoundTrip(request)
   348  	if err != nil {
   349  		return
   350  	}
   351  	defer response.Body.Close()
   352  	result = &LogEntryGetResponse{}
   353  	result.status = response.StatusCode
   354  	result.header = response.Header
   355  	reader := bufio.NewReader(response.Body)
   356  	_, err = reader.Peek(1)
   357  	if err == io.EOF {
   358  		err = nil
   359  		return
   360  	}
   361  	if result.status >= 400 {
   362  		result.err, err = errors.UnmarshalErrorStatus(reader, result.status)
   363  		if err != nil {
   364  			return
   365  		}
   366  		err = result.err
   367  		return
   368  	}
   369  	err = readLogEntryGetResponse(result, reader)
   370  	if err != nil {
   371  		return
   372  	}
   373  	return
   374  }
   375  
   376  // LogEntryGetResponse is the response for the 'get' method.
   377  type LogEntryGetResponse struct {
   378  	status int
   379  	header http.Header
   380  	err    *errors.Error
   381  	body   *LogEntry
   382  }
   383  
   384  // Status returns the response status code.
   385  func (r *LogEntryGetResponse) Status() int {
   386  	if r == nil {
   387  		return 0
   388  	}
   389  	return r.status
   390  }
   391  
   392  // Header returns header of the response.
   393  func (r *LogEntryGetResponse) Header() http.Header {
   394  	if r == nil {
   395  		return nil
   396  	}
   397  	return r.header
   398  }
   399  
   400  // Error returns the response error.
   401  func (r *LogEntryGetResponse) Error() *errors.Error {
   402  	if r == nil {
   403  		return nil
   404  	}
   405  	return r.err
   406  }
   407  
   408  // Body returns the value of the 'body' parameter.
   409  func (r *LogEntryGetResponse) Body() *LogEntry {
   410  	if r == nil {
   411  		return nil
   412  	}
   413  	return r.body
   414  }
   415  
   416  // GetBody returns the value of the 'body' parameter and
   417  // a flag indicating if the parameter has a value.
   418  func (r *LogEntryGetResponse) GetBody() (value *LogEntry, ok bool) {
   419  	ok = r != nil && r.body != nil
   420  	if ok {
   421  		value = r.body
   422  	}
   423  	return
   424  }