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