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