github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/pkg/platform/model/buildplanner/buildplanner.go (about)

     1  package buildplanner
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  	"strings"
     7  
     8  	"github.com/ActiveState/cli/internal/constants"
     9  	"github.com/ActiveState/cli/internal/gqlclient"
    10  	"github.com/ActiveState/cli/internal/logging"
    11  	"github.com/ActiveState/cli/pkg/platform/api"
    12  	"github.com/ActiveState/cli/pkg/platform/authentication"
    13  	"github.com/ActiveState/cli/pkg/platform/runtime/buildexpression"
    14  	"github.com/ActiveState/graphql"
    15  )
    16  
    17  const clientDeprecationErrorKey = "CLIENT_DEPRECATION_ERROR"
    18  
    19  type client struct {
    20  	gqlClient *gqlclient.Client
    21  }
    22  
    23  func logRequestVariables(req gqlclient.Request) {
    24  	if !strings.EqualFold(os.Getenv(constants.DebugServiceRequestsEnvVarName), "true") {
    25  		return
    26  	}
    27  
    28  	vars, err := req.Vars()
    29  	if err != nil {
    30  		// Don't fail request because of this errors
    31  		logging.Error("Failed to get request vars: %s", err)
    32  		return
    33  	}
    34  
    35  	for _, v := range vars {
    36  		if _, ok := v.(*buildexpression.BuildExpression); !ok {
    37  			continue
    38  		}
    39  
    40  		beData, err := json.MarshalIndent(v, "", "  ")
    41  		if err != nil {
    42  			logging.Error("Failed to marshal build expression: %s", err)
    43  			return
    44  		}
    45  		logging.Debug("Build Expression: %s", string(beData))
    46  	}
    47  }
    48  
    49  type BuildPlanner struct {
    50  	auth   *authentication.Auth
    51  	client *client
    52  }
    53  
    54  func NewBuildPlannerModel(auth *authentication.Auth) *BuildPlanner {
    55  	bpURL := api.GetServiceURL(api.ServiceBuildPlanner).String()
    56  	logging.Debug("Using build planner at: %s", bpURL)
    57  
    58  	gqlClient := gqlclient.NewWithOpts(bpURL, 0, graphql.WithHTTPClient(api.NewHTTPClient()))
    59  
    60  	if auth != nil && auth.Authenticated() {
    61  		gqlClient.SetTokenProvider(auth)
    62  	}
    63  
    64  	return &BuildPlanner{
    65  		auth: auth,
    66  		client: &client{
    67  			gqlClient: gqlClient,
    68  		},
    69  	}
    70  }