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

     1  package schematics
     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  //ErrCodeAPICreation ...
    15  const ErrCodeAPICreation = "APICreationError"
    16  
    17  //SchematicsServiceAPI is the Aramda K8s client ...
    18  type SchematicsServiceAPI interface {
    19  	Workspaces() Workspaces
    20  
    21  	//TODO Add other services
    22  }
    23  
    24  //VpcContainerService holds the client
    25  type scService struct {
    26  	*client.Client
    27  }
    28  
    29  //New ...
    30  func New(sess *session.Session) (SchematicsServiceAPI, error) {
    31  	config := sess.Config.Copy()
    32  	err := config.ValidateConfigForService(bluemix.SchematicsService)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	if config.HTTPClient == nil {
    37  		config.HTTPClient = http.NewHTTPClient(config)
    38  	}
    39  	tokenRefreher, err := authentication.NewIAMAuthRepository(config, &rest.Client{
    40  		DefaultHeader: gohttp.Header{
    41  			"X-Original-User-Agent": []string{config.UserAgent},
    42  			"User-Agent":            []string{http.UserAgent()},
    43  		},
    44  		HTTPClient: config.HTTPClient,
    45  	})
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	if config.IAMAccessToken == "" {
    50  		err := authentication.PopulateTokens(tokenRefreher, config)
    51  		if err != nil {
    52  			return nil, err
    53  		}
    54  	}
    55  	if config.Endpoint == nil {
    56  		ep, err := config.EndpointLocator.SchematicsEndpoint()
    57  		if err != nil {
    58  			return nil, err
    59  		}
    60  		config.Endpoint = &ep
    61  	}
    62  
    63  	return &scService{
    64  		Client: client.New(config, bluemix.SchematicsService, tokenRefreher),
    65  	}, nil
    66  }
    67  
    68  //Clusters implements Clusters API
    69  func (c scService) Workspaces() Workspaces {
    70  	return newWorkspaceAPI(c.Client)
    71  }