khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 1 | package client |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "log" |
| 7 | ) |
| 8 | |
| 9 | // Settings holds optional client settings. |
| 10 | type Settings struct { |
| 11 | disablePAFXFast bool |
| 12 | assumePreAuthentication bool |
| 13 | preAuthEType int32 |
| 14 | logger *log.Logger |
| 15 | } |
| 16 | |
| 17 | // jsonSettings is used when marshaling the Settings details to JSON format. |
| 18 | type jsonSettings struct { |
| 19 | DisablePAFXFast bool |
| 20 | AssumePreAuthentication bool |
| 21 | } |
| 22 | |
| 23 | // NewSettings creates a new client settings struct. |
| 24 | func NewSettings(settings ...func(*Settings)) *Settings { |
| 25 | s := new(Settings) |
| 26 | for _, set := range settings { |
| 27 | set(s) |
| 28 | } |
| 29 | return s |
| 30 | } |
| 31 | |
| 32 | // DisablePAFXFAST used to configure the client to not use PA_FX_FAST. |
| 33 | // |
| 34 | // s := NewSettings(DisablePAFXFAST(true)) |
| 35 | func DisablePAFXFAST(b bool) func(*Settings) { |
| 36 | return func(s *Settings) { |
| 37 | s.disablePAFXFast = b |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // DisablePAFXFAST indicates is the client should disable the use of PA_FX_FAST. |
| 42 | func (s *Settings) DisablePAFXFAST() bool { |
| 43 | return s.disablePAFXFast |
| 44 | } |
| 45 | |
| 46 | // AssumePreAuthentication used to configure the client to assume pre-authentication is required. |
| 47 | // |
| 48 | // s := NewSettings(AssumePreAuthentication(true)) |
| 49 | func AssumePreAuthentication(b bool) func(*Settings) { |
| 50 | return func(s *Settings) { |
| 51 | s.assumePreAuthentication = b |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // AssumePreAuthentication indicates if the client should proactively assume using pre-authentication. |
| 56 | func (s *Settings) AssumePreAuthentication() bool { |
| 57 | return s.assumePreAuthentication |
| 58 | } |
| 59 | |
| 60 | // Logger used to configure client with a logger. |
| 61 | // |
| 62 | // s := NewSettings(kt, Logger(l)) |
| 63 | func Logger(l *log.Logger) func(*Settings) { |
| 64 | return func(s *Settings) { |
| 65 | s.logger = l |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Logger returns the client logger instance. |
| 70 | func (s *Settings) Logger() *log.Logger { |
| 71 | return s.logger |
| 72 | } |
| 73 | |
| 74 | // Log will write to the service's logger if it is configured. |
| 75 | func (cl *Client) Log(format string, v ...interface{}) { |
| 76 | if cl.settings.Logger() != nil { |
| 77 | cl.settings.Logger().Output(2, fmt.Sprintf(format, v...)) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // JSON returns a JSON representation of the settings. |
| 82 | func (s *Settings) JSON() (string, error) { |
| 83 | js := jsonSettings{ |
| 84 | DisablePAFXFast: s.disablePAFXFast, |
| 85 | AssumePreAuthentication: s.assumePreAuthentication, |
| 86 | } |
| 87 | b, err := json.MarshalIndent(js, "", " ") |
| 88 | if err != nil { |
| 89 | return "", err |
| 90 | } |
| 91 | return string(b), nil |
| 92 | |
| 93 | } |