github.com/openshift-online/ocm-sdk-go@v0.1.473/accountsmgmt/v1/permission_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 // PermissionClient is the client of the 'permission' resource. 35 // 36 // Manages a specific permission. 37 type PermissionClient struct { 38 transport http.RoundTripper 39 path string 40 } 41 42 // NewPermissionClient creates a new client for the 'permission' 43 // resource using the given transport to send the requests and receive the 44 // responses. 45 func NewPermissionClient(transport http.RoundTripper, path string) *PermissionClient { 46 return &PermissionClient{ 47 transport: transport, 48 path: path, 49 } 50 } 51 52 // Delete creates a request for the 'delete' method. 53 // 54 // Deletes the permission. 55 func (c *PermissionClient) Delete() *PermissionDeleteRequest { 56 return &PermissionDeleteRequest{ 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 permission. 65 func (c *PermissionClient) Get() *PermissionGetRequest { 66 return &PermissionGetRequest{ 67 transport: c.transport, 68 path: c.path, 69 } 70 } 71 72 // PermissionPollRequest is the request for the Poll method. 73 type PermissionPollRequest struct { 74 request *PermissionGetRequest 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 *PermissionPollRequest) Parameter(name string, value interface{}) *PermissionPollRequest { 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 *PermissionPollRequest) Header(name string, value interface{}) *PermissionPollRequest { 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 *PermissionPollRequest) Interval(value time.Duration) *PermissionPollRequest { 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 *PermissionPollRequest) Status(value int) *PermissionPollRequest { 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 *PermissionPollRequest) Predicate(value func(*PermissionGetResponse) bool) *PermissionPollRequest { 109 r.predicates = append(r.predicates, func(response interface{}) bool { 110 return value(response.(*PermissionGetResponse)) 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 *PermissionPollRequest) StartContext(ctx context.Context) (response *PermissionPollResponse, err error) { 121 result, err := helpers.PollContext(ctx, r.interval, r.statuses, r.predicates, r.task) 122 if result != nil { 123 response = &PermissionPollResponse{ 124 response: result.(*PermissionGetResponse), 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 *PermissionPollRequest) 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 // PermissionPollResponse is the response for the Poll method. 142 type PermissionPollResponse struct { 143 response *PermissionGetResponse 144 } 145 146 // Status returns the response status code. 147 func (r *PermissionPollResponse) 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 *PermissionPollResponse) 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 *PermissionPollResponse) 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 *PermissionPollResponse) Body() *Permission { 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 *PermissionPollResponse) GetBody() (value *Permission, 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 *PermissionClient) Poll() *PermissionPollRequest { 184 return &PermissionPollRequest{ 185 request: c.Get(), 186 } 187 } 188 189 // PermissionDeleteRequest is the request for the 'delete' method. 190 type PermissionDeleteRequest 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 *PermissionDeleteRequest) Parameter(name string, value interface{}) *PermissionDeleteRequest { 199 helpers.AddValue(&r.query, name, value) 200 return r 201 } 202 203 // Header adds a request header. 204 func (r *PermissionDeleteRequest) Header(name string, value interface{}) *PermissionDeleteRequest { 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 *PermissionDeleteRequest) Impersonate(user string) *PermissionDeleteRequest { 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 *PermissionDeleteRequest) Send() (result *PermissionDeleteResponse, 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 *PermissionDeleteRequest) SendContext(ctx context.Context) (result *PermissionDeleteResponse, 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 = &PermissionDeleteResponse{} 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 // PermissionDeleteResponse is the response for the 'delete' method. 266 type PermissionDeleteResponse struct { 267 status int 268 header http.Header 269 err *errors.Error 270 } 271 272 // Status returns the response status code. 273 func (r *PermissionDeleteResponse) 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 *PermissionDeleteResponse) 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 *PermissionDeleteResponse) Error() *errors.Error { 290 if r == nil { 291 return nil 292 } 293 return r.err 294 } 295 296 // PermissionGetRequest is the request for the 'get' method. 297 type PermissionGetRequest 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 *PermissionGetRequest) Parameter(name string, value interface{}) *PermissionGetRequest { 306 helpers.AddValue(&r.query, name, value) 307 return r 308 } 309 310 // Header adds a request header. 311 func (r *PermissionGetRequest) Header(name string, value interface{}) *PermissionGetRequest { 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 *PermissionGetRequest) Impersonate(user string) *PermissionGetRequest { 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 *PermissionGetRequest) Send() (result *PermissionGetResponse, 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 *PermissionGetRequest) SendContext(ctx context.Context) (result *PermissionGetResponse, 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 = &PermissionGetResponse{} 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 = readPermissionGetResponse(result, reader) 370 if err != nil { 371 return 372 } 373 return 374 } 375 376 // PermissionGetResponse is the response for the 'get' method. 377 type PermissionGetResponse struct { 378 status int 379 header http.Header 380 err *errors.Error 381 body *Permission 382 } 383 384 // Status returns the response status code. 385 func (r *PermissionGetResponse) 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 *PermissionGetResponse) 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 *PermissionGetResponse) 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 *PermissionGetResponse) Body() *Permission { 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 *PermissionGetResponse) GetBody() (value *Permission, ok bool) { 419 ok = r != nil && r.body != nil 420 if ok { 421 value = r.body 422 } 423 return 424 }