github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/v7/shared/noaa_client.go (about) 1 package shared 2 3 import ( 4 "crypto/tls" 5 "net/http" 6 "time" 7 8 "code.cloudfoundry.org/cli/api/uaa" 9 "code.cloudfoundry.org/cli/api/uaa/noaabridge" 10 "code.cloudfoundry.org/cli/command" 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() 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 &tls.Config{ 45 InsecureSkipVerify: config.SkipSSLValidation(), 46 }, 47 http.ProxyFromEnvironment, 48 ) 49 client.RefreshTokenFrom(noaabridge.NewTokenRefresher(uaaClient, config)) 50 client.SetMaxRetryCount(config.NOAARequestRetryCount()) 51 52 noaaDebugPrinter := DebugPrinter{} 53 54 // if verbose, set debug printer on noaa client 55 verbose, location := config.Verbose() 56 57 client.SetDebugPrinter(&noaaDebugPrinter) 58 59 if verbose { 60 noaaDebugPrinter.addOutput(ui.RequestLoggerTerminalDisplay()) 61 } 62 if location != nil { 63 noaaDebugPrinter.addOutput(ui.RequestLoggerFileWriter(location)) 64 } 65 66 return client 67 }