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

     1  package hpcs
     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  //HPCSV2 is the resource client ...
    15  type HPCSV2 interface {
    16  	Endpoint() EndpointRepository
    17  }
    18  
    19  //ErrCodeAPICreation ...
    20  const ErrCodeAPICreation = "APICreationError"
    21  
    22  //hpcsService holds the client
    23  type hpcsService struct {
    24  	*client.Client
    25  }
    26  
    27  //New ...
    28  func New(sess *session.Session) (HPCSV2, error) {
    29  	config := sess.Config.Copy()
    30  	err := config.ValidateConfigForService(bluemix.HPCService)
    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.NewIAMAuthRepository(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.IAMAccessToken == "" {
    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.HpcsEndpoint()
    55  		if err != nil {
    56  			return nil, err
    57  		}
    58  		config.Endpoint = &ep
    59  	}
    60  
    61  	return &hpcsService{
    62  		Client: client.New(config, bluemix.HPCService, tokenRefreher),
    63  	}, nil
    64  }
    65  
    66  //Hpcs API
    67  func (a *hpcsService) Endpoint() EndpointRepository {
    68  	return NewHpcsEndpointRepository(a.Client)
    69  }