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