github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/command/v6/shared/noaa_client.go (about)

     1  package shared
     2  
     3  import (
     4  	"net/http"
     5  	"time"
     6  
     7  	"code.cloudfoundry.org/cli/api/uaa"
     8  	"code.cloudfoundry.org/cli/api/uaa/noaabridge"
     9  	"code.cloudfoundry.org/cli/command"
    10  	"code.cloudfoundry.org/cli/util"
    11  	"github.com/cloudfoundry/noaa/consumer"
    12  )
    13  
    14  type RequestLoggerOutput interface {
    15  	Start() error
    16  	Stop() error
    17  	DisplayType(name string, requestDate time.Time) error
    18  	DisplayDump(dump string) error
    19  }
    20  
    21  type DebugPrinter struct {
    22  	outputs []RequestLoggerOutput
    23  }
    24  
    25  func (p *DebugPrinter) addOutput(output RequestLoggerOutput) {
    26  	p.outputs = append(p.outputs, output)
    27  }
    28  
    29  func (p DebugPrinter) Print(title string, dump string) {
    30  	for _, output := range p.outputs {
    31  		_ = output.Start()
    32  		defer output.Stop() // nolint:errcheck
    33  
    34  		_ = output.DisplayType(title, time.Now())
    35  		_ = output.DisplayDump(dump)
    36  	}
    37  
    38  }
    39  
    40  // NewNOAAClient returns back a configured NOAA Client.
    41  func NewNOAAClient(apiURL string, config command.Config, uaaClient *uaa.Client, ui command.UI) *consumer.Consumer {
    42  	client := consumer.New(
    43  		apiURL,
    44  		util.NewTLSConfig(nil, config.SkipSSLValidation()),
    45  		http.ProxyFromEnvironment,
    46  	)
    47  	client.RefreshTokenFrom(noaabridge.NewTokenRefresher(uaaClient, config))
    48  	client.SetMaxRetryCount(config.NOAARequestRetryCount())
    49  
    50  	noaaDebugPrinter := DebugPrinter{}
    51  
    52  	// if verbose, set debug printer on noaa client
    53  	verbose, location := config.Verbose()
    54  
    55  	client.SetDebugPrinter(&noaaDebugPrinter)
    56  
    57  	if verbose {
    58  		noaaDebugPrinter.addOutput(ui.RequestLoggerTerminalDisplay())
    59  	}
    60  	if location != nil {
    61  		noaaDebugPrinter.addOutput(ui.RequestLoggerFileWriter(location))
    62  	}
    63  
    64  	return client
    65  }