settings.go 2.2 KB

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