github.com/openshift-online/ocm-sdk-go@v0.1.473/accountsmgmt/v1/accounts_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  	"bytes"
    25  	"context"
    26  	"io"
    27  	"net/http"
    28  	"net/url"
    29  	"path"
    30  
    31  	"github.com/openshift-online/ocm-sdk-go/errors"
    32  	"github.com/openshift-online/ocm-sdk-go/helpers"
    33  )
    34  
    35  // AccountsClient is the client of the 'accounts' resource.
    36  //
    37  // Manages the collection of accounts.
    38  type AccountsClient struct {
    39  	transport http.RoundTripper
    40  	path      string
    41  }
    42  
    43  // NewAccountsClient creates a new client for the 'accounts'
    44  // resource using the given transport to send the requests and receive the
    45  // responses.
    46  func NewAccountsClient(transport http.RoundTripper, path string) *AccountsClient {
    47  	return &AccountsClient{
    48  		transport: transport,
    49  		path:      path,
    50  	}
    51  }
    52  
    53  // Add creates a request for the 'add' method.
    54  //
    55  // Creates a new account.
    56  func (c *AccountsClient) Add() *AccountsAddRequest {
    57  	return &AccountsAddRequest{
    58  		transport: c.transport,
    59  		path:      c.path,
    60  	}
    61  }
    62  
    63  // List creates a request for the 'list' method.
    64  //
    65  // Retrieves the list of accounts.
    66  func (c *AccountsClient) List() *AccountsListRequest {
    67  	return &AccountsListRequest{
    68  		transport: c.transport,
    69  		path:      c.path,
    70  	}
    71  }
    72  
    73  // Account returns the target 'account' resource for the given identifier.
    74  //
    75  // Reference to the service that manages an specific account.
    76  func (c *AccountsClient) Account(id string) *AccountClient {
    77  	return NewAccountClient(
    78  		c.transport,
    79  		path.Join(c.path, id),
    80  	)
    81  }
    82  
    83  // AccountsAddRequest is the request for the 'add' method.
    84  type AccountsAddRequest struct {
    85  	transport http.RoundTripper
    86  	path      string
    87  	query     url.Values
    88  	header    http.Header
    89  	body      *Account
    90  }
    91  
    92  // Parameter adds a query parameter.
    93  func (r *AccountsAddRequest) Parameter(name string, value interface{}) *AccountsAddRequest {
    94  	helpers.AddValue(&r.query, name, value)
    95  	return r
    96  }
    97  
    98  // Header adds a request header.
    99  func (r *AccountsAddRequest) Header(name string, value interface{}) *AccountsAddRequest {
   100  	helpers.AddHeader(&r.header, name, value)
   101  	return r
   102  }
   103  
   104  // Impersonate wraps requests on behalf of another user.
   105  // Note: Services that do not support this feature may silently ignore this call.
   106  func (r *AccountsAddRequest) Impersonate(user string) *AccountsAddRequest {
   107  	helpers.AddImpersonationHeader(&r.header, user)
   108  	return r
   109  }
   110  
   111  // Body sets the value of the 'body' parameter.
   112  //
   113  // Account data.
   114  func (r *AccountsAddRequest) Body(value *Account) *AccountsAddRequest {
   115  	r.body = value
   116  	return r
   117  }
   118  
   119  // Send sends this request, waits for the response, and returns it.
   120  //
   121  // This is a potentially lengthy operation, as it requires network communication.
   122  // Consider using a context and the SendContext method.
   123  func (r *AccountsAddRequest) Send() (result *AccountsAddResponse, err error) {
   124  	return r.SendContext(context.Background())
   125  }
   126  
   127  // SendContext sends this request, waits for the response, and returns it.
   128  func (r *AccountsAddRequest) SendContext(ctx context.Context) (result *AccountsAddResponse, err error) {
   129  	query := helpers.CopyQuery(r.query)
   130  	header := helpers.CopyHeader(r.header)
   131  	buffer := &bytes.Buffer{}
   132  	err = writeAccountsAddRequest(r, buffer)
   133  	if err != nil {
   134  		return
   135  	}
   136  	uri := &url.URL{
   137  		Path:     r.path,
   138  		RawQuery: query.Encode(),
   139  	}
   140  	request := &http.Request{
   141  		Method: "POST",
   142  		URL:    uri,
   143  		Header: header,
   144  		Body:   io.NopCloser(buffer),
   145  	}
   146  	if ctx != nil {
   147  		request = request.WithContext(ctx)
   148  	}
   149  	response, err := r.transport.RoundTrip(request)
   150  	if err != nil {
   151  		return
   152  	}
   153  	defer response.Body.Close()
   154  	result = &AccountsAddResponse{}
   155  	result.status = response.StatusCode
   156  	result.header = response.Header
   157  	reader := bufio.NewReader(response.Body)
   158  	_, err = reader.Peek(1)
   159  	if err == io.EOF {
   160  		err = nil
   161  		return
   162  	}
   163  	if result.status >= 400 {
   164  		result.err, err = errors.UnmarshalErrorStatus(reader, result.status)
   165  		if err != nil {
   166  			return
   167  		}
   168  		err = result.err
   169  		return
   170  	}
   171  	err = readAccountsAddResponse(result, reader)
   172  	if err != nil {
   173  		return
   174  	}
   175  	return
   176  }
   177  
   178  // AccountsAddResponse is the response for the 'add' method.
   179  type AccountsAddResponse struct {
   180  	status int
   181  	header http.Header
   182  	err    *errors.Error
   183  	body   *Account
   184  }
   185  
   186  // Status returns the response status code.
   187  func (r *AccountsAddResponse) Status() int {
   188  	if r == nil {
   189  		return 0
   190  	}
   191  	return r.status
   192  }
   193  
   194  // Header returns header of the response.
   195  func (r *AccountsAddResponse) Header() http.Header {
   196  	if r == nil {
   197  		return nil
   198  	}
   199  	return r.header
   200  }
   201  
   202  // Error returns the response error.
   203  func (r *AccountsAddResponse) Error() *errors.Error {
   204  	if r == nil {
   205  		return nil
   206  	}
   207  	return r.err
   208  }
   209  
   210  // Body returns the value of the 'body' parameter.
   211  //
   212  // Account data.
   213  func (r *AccountsAddResponse) Body() *Account {
   214  	if r == nil {
   215  		return nil
   216  	}
   217  	return r.body
   218  }
   219  
   220  // GetBody returns the value of the 'body' parameter and
   221  // a flag indicating if the parameter has a value.
   222  //
   223  // Account data.
   224  func (r *AccountsAddResponse) GetBody() (value *Account, ok bool) {
   225  	ok = r != nil && r.body != nil
   226  	if ok {
   227  		value = r.body
   228  	}
   229  	return
   230  }
   231  
   232  // AccountsListRequest is the request for the 'list' method.
   233  type AccountsListRequest struct {
   234  	transport   http.RoundTripper
   235  	path        string
   236  	query       url.Values
   237  	header      http.Header
   238  	fetchLabels *bool
   239  	fields      *string
   240  	order       *string
   241  	page        *int
   242  	search      *string
   243  	size        *int
   244  }
   245  
   246  // Parameter adds a query parameter.
   247  func (r *AccountsListRequest) Parameter(name string, value interface{}) *AccountsListRequest {
   248  	helpers.AddValue(&r.query, name, value)
   249  	return r
   250  }
   251  
   252  // Header adds a request header.
   253  func (r *AccountsListRequest) Header(name string, value interface{}) *AccountsListRequest {
   254  	helpers.AddHeader(&r.header, name, value)
   255  	return r
   256  }
   257  
   258  // Impersonate wraps requests on behalf of another user.
   259  // Note: Services that do not support this feature may silently ignore this call.
   260  func (r *AccountsListRequest) Impersonate(user string) *AccountsListRequest {
   261  	helpers.AddImpersonationHeader(&r.header, user)
   262  	return r
   263  }
   264  
   265  // FetchLabels sets the value of the 'fetch_labels' parameter.
   266  //
   267  // If true, includes the labels on an account in the output. Could slow request response time.
   268  func (r *AccountsListRequest) FetchLabels(value bool) *AccountsListRequest {
   269  	r.fetchLabels = &value
   270  	return r
   271  }
   272  
   273  // Fields sets the value of the 'fields' parameter.
   274  //
   275  // Projection
   276  // This field contains a comma-separated list of fields you'd like to get in
   277  // a result. No new fields can be added, only existing ones can be filtered.
   278  // To specify a field 'id' of a structure 'plan' use 'plan.id'.
   279  // To specify all fields of a structure 'labels' use 'labels.*'.
   280  func (r *AccountsListRequest) Fields(value string) *AccountsListRequest {
   281  	r.fields = &value
   282  	return r
   283  }
   284  
   285  // Order sets the value of the 'order' parameter.
   286  //
   287  // Order criteria.
   288  //
   289  // The syntax of this parameter is similar to the syntax of the _order by_ clause of
   290  // a SQL statement. For example, in order to sort the
   291  // accounts descending by name identifier the value should be:
   292  //
   293  // ```sql
   294  // name desc
   295  // ```
   296  //
   297  // If the parameter isn't provided, or if the value is empty, then the order of the
   298  // results is undefined.
   299  func (r *AccountsListRequest) Order(value string) *AccountsListRequest {
   300  	r.order = &value
   301  	return r
   302  }
   303  
   304  // Page sets the value of the 'page' parameter.
   305  //
   306  // Index of the requested page, where one corresponds to the first page.
   307  func (r *AccountsListRequest) Page(value int) *AccountsListRequest {
   308  	r.page = &value
   309  	return r
   310  }
   311  
   312  // Search sets the value of the 'search' parameter.
   313  //
   314  // Search criteria.
   315  //
   316  // The syntax of this parameter is similar to the syntax of the _where_ clause
   317  // of an SQL statement, but using the names of the attributes of the account
   318  // instead of the names of the columns of a table. For example, in order to
   319  // retrieve accounts with username starting with my:
   320  //
   321  // ```sql
   322  // username like 'my%'
   323  // ```
   324  //
   325  // If the parameter isn't provided, or if the value is empty, then all the
   326  // items that the user has permission to see will be returned.
   327  func (r *AccountsListRequest) Search(value string) *AccountsListRequest {
   328  	r.search = &value
   329  	return r
   330  }
   331  
   332  // Size sets the value of the 'size' parameter.
   333  //
   334  // Maximum number of items that will be contained in the returned page.
   335  func (r *AccountsListRequest) Size(value int) *AccountsListRequest {
   336  	r.size = &value
   337  	return r
   338  }
   339  
   340  // Send sends this request, waits for the response, and returns it.
   341  //
   342  // This is a potentially lengthy operation, as it requires network communication.
   343  // Consider using a context and the SendContext method.
   344  func (r *AccountsListRequest) Send() (result *AccountsListResponse, err error) {
   345  	return r.SendContext(context.Background())
   346  }
   347  
   348  // SendContext sends this request, waits for the response, and returns it.
   349  func (r *AccountsListRequest) SendContext(ctx context.Context) (result *AccountsListResponse, err error) {
   350  	query := helpers.CopyQuery(r.query)
   351  	if r.fetchLabels != nil {
   352  		helpers.AddValue(&query, "fetchLabels", *r.fetchLabels)
   353  	}
   354  	if r.fields != nil {
   355  		helpers.AddValue(&query, "fields", *r.fields)
   356  	}
   357  	if r.order != nil {
   358  		helpers.AddValue(&query, "order", *r.order)
   359  	}
   360  	if r.page != nil {
   361  		helpers.AddValue(&query, "page", *r.page)
   362  	}
   363  	if r.search != nil {
   364  		helpers.AddValue(&query, "search", *r.search)
   365  	}
   366  	if r.size != nil {
   367  		helpers.AddValue(&query, "size", *r.size)
   368  	}
   369  	header := helpers.CopyHeader(r.header)
   370  	uri := &url.URL{
   371  		Path:     r.path,
   372  		RawQuery: query.Encode(),
   373  	}
   374  	request := &http.Request{
   375  		Method: "GET",
   376  		URL:    uri,
   377  		Header: header,
   378  	}
   379  	if ctx != nil {
   380  		request = request.WithContext(ctx)
   381  	}
   382  	response, err := r.transport.RoundTrip(request)
   383  	if err != nil {
   384  		return
   385  	}
   386  	defer response.Body.Close()
   387  	result = &AccountsListResponse{}
   388  	result.status = response.StatusCode
   389  	result.header = response.Header
   390  	reader := bufio.NewReader(response.Body)
   391  	_, err = reader.Peek(1)
   392  	if err == io.EOF {
   393  		err = nil
   394  		return
   395  	}
   396  	if result.status >= 400 {
   397  		result.err, err = errors.UnmarshalErrorStatus(reader, result.status)
   398  		if err != nil {
   399  			return
   400  		}
   401  		err = result.err
   402  		return
   403  	}
   404  	err = readAccountsListResponse(result, reader)
   405  	if err != nil {
   406  		return
   407  	}
   408  	return
   409  }
   410  
   411  // AccountsListResponse is the response for the 'list' method.
   412  type AccountsListResponse struct {
   413  	status int
   414  	header http.Header
   415  	err    *errors.Error
   416  	items  *AccountList
   417  	page   *int
   418  	size   *int
   419  	total  *int
   420  }
   421  
   422  // Status returns the response status code.
   423  func (r *AccountsListResponse) Status() int {
   424  	if r == nil {
   425  		return 0
   426  	}
   427  	return r.status
   428  }
   429  
   430  // Header returns header of the response.
   431  func (r *AccountsListResponse) Header() http.Header {
   432  	if r == nil {
   433  		return nil
   434  	}
   435  	return r.header
   436  }
   437  
   438  // Error returns the response error.
   439  func (r *AccountsListResponse) Error() *errors.Error {
   440  	if r == nil {
   441  		return nil
   442  	}
   443  	return r.err
   444  }
   445  
   446  // Items returns the value of the 'items' parameter.
   447  //
   448  // Retrieved list of accounts.
   449  func (r *AccountsListResponse) Items() *AccountList {
   450  	if r == nil {
   451  		return nil
   452  	}
   453  	return r.items
   454  }
   455  
   456  // GetItems returns the value of the 'items' parameter and
   457  // a flag indicating if the parameter has a value.
   458  //
   459  // Retrieved list of accounts.
   460  func (r *AccountsListResponse) GetItems() (value *AccountList, ok bool) {
   461  	ok = r != nil && r.items != nil
   462  	if ok {
   463  		value = r.items
   464  	}
   465  	return
   466  }
   467  
   468  // Page returns the value of the 'page' parameter.
   469  //
   470  // Index of the requested page, where one corresponds to the first page.
   471  func (r *AccountsListResponse) Page() int {
   472  	if r != nil && r.page != nil {
   473  		return *r.page
   474  	}
   475  	return 0
   476  }
   477  
   478  // GetPage returns the value of the 'page' parameter and
   479  // a flag indicating if the parameter has a value.
   480  //
   481  // Index of the requested page, where one corresponds to the first page.
   482  func (r *AccountsListResponse) GetPage() (value int, ok bool) {
   483  	ok = r != nil && r.page != nil
   484  	if ok {
   485  		value = *r.page
   486  	}
   487  	return
   488  }
   489  
   490  // Size returns the value of the 'size' parameter.
   491  //
   492  // Maximum number of items that will be contained in the returned page.
   493  func (r *AccountsListResponse) Size() int {
   494  	if r != nil && r.size != nil {
   495  		return *r.size
   496  	}
   497  	return 0
   498  }
   499  
   500  // GetSize returns the value of the 'size' parameter and
   501  // a flag indicating if the parameter has a value.
   502  //
   503  // Maximum number of items that will be contained in the returned page.
   504  func (r *AccountsListResponse) GetSize() (value int, ok bool) {
   505  	ok = r != nil && r.size != nil
   506  	if ok {
   507  		value = *r.size
   508  	}
   509  	return
   510  }
   511  
   512  // Total returns the value of the 'total' parameter.
   513  //
   514  // Total number of items of the collection that match the search criteria,
   515  // regardless of the size of the page.
   516  func (r *AccountsListResponse) Total() int {
   517  	if r != nil && r.total != nil {
   518  		return *r.total
   519  	}
   520  	return 0
   521  }
   522  
   523  // GetTotal returns the value of the 'total' parameter and
   524  // a flag indicating if the parameter has a value.
   525  //
   526  // Total number of items of the collection that match the search criteria,
   527  // regardless of the size of the page.
   528  func (r *AccountsListResponse) GetTotal() (value int, ok bool) {
   529  	ok = r != nil && r.total != nil
   530  	if ok {
   531  		value = *r.total
   532  	}
   533  	return
   534  }