github.com/openshift-online/ocm-sdk-go@v0.1.473/accesstransparency/v1/access_request_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/accesstransparency/v1
    21  
    22  import (
    23  	"bufio"
    24  	"context"
    25  	"io"
    26  	"net/http"
    27  	"net/url"
    28  	"path"
    29  	"time"
    30  
    31  	"github.com/openshift-online/ocm-sdk-go/errors"
    32  	"github.com/openshift-online/ocm-sdk-go/helpers"
    33  )
    34  
    35  // AccessRequestClient is the client of the 'access_request' resource.
    36  //
    37  // Manages a specific access request.
    38  type AccessRequestClient struct {
    39  	transport http.RoundTripper
    40  	path      string
    41  }
    42  
    43  // NewAccessRequestClient creates a new client for the 'access_request'
    44  // resource using the given transport to send the requests and receive the
    45  // responses.
    46  func NewAccessRequestClient(transport http.RoundTripper, path string) *AccessRequestClient {
    47  	return &AccessRequestClient{
    48  		transport: transport,
    49  		path:      path,
    50  	}
    51  }
    52  
    53  // Get creates a request for the 'get' method.
    54  //
    55  // Retrieves the details of the access request.
    56  func (c *AccessRequestClient) Get() *AccessRequestGetRequest {
    57  	return &AccessRequestGetRequest{
    58  		transport: c.transport,
    59  		path:      c.path,
    60  	}
    61  }
    62  
    63  // Decisions returns the target 'decisions' resource.
    64  //
    65  // Reference to the resource that manages the collection of decisions.
    66  func (c *AccessRequestClient) Decisions() *DecisionsClient {
    67  	return NewDecisionsClient(
    68  		c.transport,
    69  		path.Join(c.path, "decisions"),
    70  	)
    71  }
    72  
    73  // AccessRequestPollRequest is the request for the Poll method.
    74  type AccessRequestPollRequest struct {
    75  	request    *AccessRequestGetRequest
    76  	interval   time.Duration
    77  	statuses   []int
    78  	predicates []func(interface{}) bool
    79  }
    80  
    81  // Parameter adds a query parameter to all the requests that will be used to retrieve the object.
    82  func (r *AccessRequestPollRequest) Parameter(name string, value interface{}) *AccessRequestPollRequest {
    83  	r.request.Parameter(name, value)
    84  	return r
    85  }
    86  
    87  // Header adds a request header to all the requests that will be used to retrieve the object.
    88  func (r *AccessRequestPollRequest) Header(name string, value interface{}) *AccessRequestPollRequest {
    89  	r.request.Header(name, value)
    90  	return r
    91  }
    92  
    93  // Interval sets the polling interval. This parameter is mandatory and must be greater than zero.
    94  func (r *AccessRequestPollRequest) Interval(value time.Duration) *AccessRequestPollRequest {
    95  	r.interval = value
    96  	return r
    97  }
    98  
    99  // Status set the expected status of the response. Multiple values can be set calling this method
   100  // multiple times. The response will be considered successful if the status is any of those values.
   101  func (r *AccessRequestPollRequest) Status(value int) *AccessRequestPollRequest {
   102  	r.statuses = append(r.statuses, value)
   103  	return r
   104  }
   105  
   106  // Predicate adds a predicate that the response should satisfy be considered successful. Multiple
   107  // predicates can be set calling this method multiple times. The response will be considered successful
   108  // if all the predicates are satisfied.
   109  func (r *AccessRequestPollRequest) Predicate(value func(*AccessRequestGetResponse) bool) *AccessRequestPollRequest {
   110  	r.predicates = append(r.predicates, func(response interface{}) bool {
   111  		return value(response.(*AccessRequestGetResponse))
   112  	})
   113  	return r
   114  }
   115  
   116  // StartContext starts the polling loop. Responses will be considered successful if the status is one of
   117  // the values specified with the Status method and if all the predicates specified with the Predicate
   118  // method return nil.
   119  //
   120  // The context must have a timeout or deadline, otherwise this method will immediately return an error.
   121  func (r *AccessRequestPollRequest) StartContext(ctx context.Context) (response *AccessRequestPollResponse, err error) {
   122  	result, err := helpers.PollContext(ctx, r.interval, r.statuses, r.predicates, r.task)
   123  	if result != nil {
   124  		response = &AccessRequestPollResponse{
   125  			response: result.(*AccessRequestGetResponse),
   126  		}
   127  	}
   128  	return
   129  }
   130  
   131  // task adapts the types of the request/response types so that they can be used with the generic
   132  // polling function from the helpers package.
   133  func (r *AccessRequestPollRequest) task(ctx context.Context) (status int, result interface{}, err error) {
   134  	response, err := r.request.SendContext(ctx)
   135  	if response != nil {
   136  		status = response.Status()
   137  		result = response
   138  	}
   139  	return
   140  }
   141  
   142  // AccessRequestPollResponse is the response for the Poll method.
   143  type AccessRequestPollResponse struct {
   144  	response *AccessRequestGetResponse
   145  }
   146  
   147  // Status returns the response status code.
   148  func (r *AccessRequestPollResponse) Status() int {
   149  	if r == nil {
   150  		return 0
   151  	}
   152  	return r.response.Status()
   153  }
   154  
   155  // Header returns header of the response.
   156  func (r *AccessRequestPollResponse) Header() http.Header {
   157  	if r == nil {
   158  		return nil
   159  	}
   160  	return r.response.Header()
   161  }
   162  
   163  // Error returns the response error.
   164  func (r *AccessRequestPollResponse) Error() *errors.Error {
   165  	if r == nil {
   166  		return nil
   167  	}
   168  	return r.response.Error()
   169  }
   170  
   171  // Body returns the value of the 'body' parameter.
   172  func (r *AccessRequestPollResponse) Body() *AccessRequest {
   173  	return r.response.Body()
   174  }
   175  
   176  // GetBody returns the value of the 'body' parameter and
   177  // a flag indicating if the parameter has a value.
   178  func (r *AccessRequestPollResponse) GetBody() (value *AccessRequest, ok bool) {
   179  	return r.response.GetBody()
   180  }
   181  
   182  // Poll creates a request to repeatedly retrieve the object till the response has one of a given set
   183  // of states and satisfies a set of predicates.
   184  func (c *AccessRequestClient) Poll() *AccessRequestPollRequest {
   185  	return &AccessRequestPollRequest{
   186  		request: c.Get(),
   187  	}
   188  }
   189  
   190  // AccessRequestGetRequest is the request for the 'get' method.
   191  type AccessRequestGetRequest struct {
   192  	transport http.RoundTripper
   193  	path      string
   194  	query     url.Values
   195  	header    http.Header
   196  }
   197  
   198  // Parameter adds a query parameter.
   199  func (r *AccessRequestGetRequest) Parameter(name string, value interface{}) *AccessRequestGetRequest {
   200  	helpers.AddValue(&r.query, name, value)
   201  	return r
   202  }
   203  
   204  // Header adds a request header.
   205  func (r *AccessRequestGetRequest) Header(name string, value interface{}) *AccessRequestGetRequest {
   206  	helpers.AddHeader(&r.header, name, value)
   207  	return r
   208  }
   209  
   210  // Impersonate wraps requests on behalf of another user.
   211  // Note: Services that do not support this feature may silently ignore this call.
   212  func (r *AccessRequestGetRequest) Impersonate(user string) *AccessRequestGetRequest {
   213  	helpers.AddImpersonationHeader(&r.header, user)
   214  	return r
   215  }
   216  
   217  // Send sends this request, waits for the response, and returns it.
   218  //
   219  // This is a potentially lengthy operation, as it requires network communication.
   220  // Consider using a context and the SendContext method.
   221  func (r *AccessRequestGetRequest) Send() (result *AccessRequestGetResponse, err error) {
   222  	return r.SendContext(context.Background())
   223  }
   224  
   225  // SendContext sends this request, waits for the response, and returns it.
   226  func (r *AccessRequestGetRequest) SendContext(ctx context.Context) (result *AccessRequestGetResponse, err error) {
   227  	query := helpers.CopyQuery(r.query)
   228  	header := helpers.CopyHeader(r.header)
   229  	uri := &url.URL{
   230  		Path:     r.path,
   231  		RawQuery: query.Encode(),
   232  	}
   233  	request := &http.Request{
   234  		Method: "GET",
   235  		URL:    uri,
   236  		Header: header,
   237  	}
   238  	if ctx != nil {
   239  		request = request.WithContext(ctx)
   240  	}
   241  	response, err := r.transport.RoundTrip(request)
   242  	if err != nil {
   243  		return
   244  	}
   245  	defer response.Body.Close()
   246  	result = &AccessRequestGetResponse{}
   247  	result.status = response.StatusCode
   248  	result.header = response.Header
   249  	reader := bufio.NewReader(response.Body)
   250  	_, err = reader.Peek(1)
   251  	if err == io.EOF {
   252  		err = nil
   253  		return
   254  	}
   255  	if result.status >= 400 {
   256  		result.err, err = errors.UnmarshalErrorStatus(reader, result.status)
   257  		if err != nil {
   258  			return
   259  		}
   260  		err = result.err
   261  		return
   262  	}
   263  	err = readAccessRequestGetResponse(result, reader)
   264  	if err != nil {
   265  		return
   266  	}
   267  	return
   268  }
   269  
   270  // AccessRequestGetResponse is the response for the 'get' method.
   271  type AccessRequestGetResponse struct {
   272  	status int
   273  	header http.Header
   274  	err    *errors.Error
   275  	body   *AccessRequest
   276  }
   277  
   278  // Status returns the response status code.
   279  func (r *AccessRequestGetResponse) Status() int {
   280  	if r == nil {
   281  		return 0
   282  	}
   283  	return r.status
   284  }
   285  
   286  // Header returns header of the response.
   287  func (r *AccessRequestGetResponse) Header() http.Header {
   288  	if r == nil {
   289  		return nil
   290  	}
   291  	return r.header
   292  }
   293  
   294  // Error returns the response error.
   295  func (r *AccessRequestGetResponse) Error() *errors.Error {
   296  	if r == nil {
   297  		return nil
   298  	}
   299  	return r.err
   300  }
   301  
   302  // Body returns the value of the 'body' parameter.
   303  func (r *AccessRequestGetResponse) Body() *AccessRequest {
   304  	if r == nil {
   305  		return nil
   306  	}
   307  	return r.body
   308  }
   309  
   310  // GetBody returns the value of the 'body' parameter and
   311  // a flag indicating if the parameter has a value.
   312  func (r *AccessRequestGetResponse) GetBody() (value *AccessRequest, ok bool) {
   313  	ok = r != nil && r.body != nil
   314  	if ok {
   315  		value = r.body
   316  	}
   317  	return
   318  }