github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/account/accountv2/api_service.go (about)

     1  package accountv2
     2  
     3  import (
     4  	gohttp "net/http"
     5  
     6  	bluemix "github.com/IBM-Cloud/bluemix-go"
     7  	"github.com/IBM-Cloud/bluemix-go/authentication"
     8  	"github.com/IBM-Cloud/bluemix-go/client"
     9  	"github.com/IBM-Cloud/bluemix-go/http"
    10  	"github.com/IBM-Cloud/bluemix-go/rest"
    11  	"github.com/IBM-Cloud/bluemix-go/session"
    12  )
    13  
    14  //AccountServiceAPI is the accountv2 client ...
    15  type AccountServiceAPI interface {
    16  	Accounts() Accounts
    17  }
    18  
    19  //ErrCodeNoAccountExists ...
    20  const ErrCodeNoAccountExists = "NoAccountExists"
    21  
    22  //MccpService holds the client
    23  type accountService struct {
    24  	*client.Client
    25  }
    26  
    27  //New ...
    28  func New(sess *session.Session) (AccountServiceAPI, error) {
    29  	config := sess.Config.Copy()
    30  	err := config.ValidateConfigForService(bluemix.AccountService)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	if config.HTTPClient == nil {
    35  		config.HTTPClient = http.NewHTTPClient(config)
    36  	}
    37  	tokenRefreher, err := authentication.NewUAARepository(config, &rest.Client{
    38  		DefaultHeader: gohttp.Header{
    39  			"X-Original-User-Agent": []string{config.UserAgent},
    40  			"User-Agent":            []string{http.UserAgent()},
    41  		},
    42  		HTTPClient: config.HTTPClient,
    43  	})
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	if config.UAAAccessToken == "" {
    48  		err := authentication.PopulateTokens(tokenRefreher, config)
    49  		if err != nil {
    50  			return nil, err
    51  		}
    52  	}
    53  	if config.Endpoint == nil {
    54  		ep, err := config.EndpointLocator.AccountManagementEndpoint()
    55  		if err != nil {
    56  			return nil, err
    57  		}
    58  		config.Endpoint = &ep
    59  	}
    60  	return &accountService{
    61  		Client: client.New(config, bluemix.AccountService, tokenRefreher),
    62  	}, nil
    63  }
    64  
    65  //Accounts API
    66  func (a *accountService) Accounts() Accounts {
    67  	return newAccountAPI(a.Client)
    68  }