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

     1  package functions
     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  //FunctionServiceAPI ..
    18  type FunctionServiceAPI interface {
    19  	Namespaces() Functions
    20  }
    21  
    22  //fnService holds the client
    23  type fnService struct {
    24  	*client.Client
    25  }
    26  
    27  //New ...
    28  func New(sess *session.Session) (FunctionServiceAPI, error) {
    29  	config := sess.Config.Copy()
    30  	err := config.ValidateConfigForService(bluemix.FunctionsService)
    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  
    54  	if config.Endpoint == nil {
    55  		ep, err := config.EndpointLocator.FunctionsEndpoint()
    56  		if err != nil {
    57  			return nil, err
    58  		}
    59  		config.Endpoint = &ep
    60  	}
    61  
    62  	return &fnService{
    63  		Client: client.New(config, bluemix.FunctionsService, tokenRefreher),
    64  	}, nil
    65  }
    66  
    67  //NewCF ...
    68  func NewCF(sess *session.Session) (FunctionServiceAPI, error) {
    69  	config := sess.Config.Copy()
    70  	err := config.ValidateConfigForService(bluemix.FunctionsService)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  	if config.HTTPClient == nil {
    75  		config.HTTPClient = http.NewHTTPClient(config)
    76  	}
    77  	tokenRefreher, err := authentication.NewUAARepository(config, &rest.Client{
    78  		DefaultHeader: gohttp.Header{
    79  			"X-Original-User-Agent": []string{config.UserAgent},
    80  			"User-Agent":            []string{http.UserAgent()},
    81  		},
    82  		HTTPClient: config.HTTPClient,
    83  	})
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  
    88  	if config.UAAAccessToken == "" {
    89  		err := authentication.PopulateTokens(tokenRefreher, config)
    90  		if err != nil {
    91  			return nil, err
    92  		}
    93  	}
    94  
    95  	if config.Endpoint == nil {
    96  		ep, err := config.EndpointLocator.FunctionsEndpoint()
    97  		if err != nil {
    98  			return nil, err
    99  		}
   100  		config.Endpoint = &ep
   101  	}
   102  
   103  	return &fnService{
   104  		Client: client.New(config, bluemix.FunctionsService, tokenRefreher),
   105  	}, nil
   106  }
   107  
   108  //Namespaces ..
   109  func (ns *fnService) Namespaces() Functions {
   110  	return newFunctionsAPI(ns.Client)
   111  }