@@ -58,7 +58,7 @@ type Client interface {
58
58
LoadStruct (config interface {}) error
59
59
}
60
60
61
- type httpClient interface {
61
+ type HTTClient interface {
62
62
Do (req * http.Request ) (* http.Response , error )
63
63
}
64
64
@@ -67,6 +67,27 @@ const (
67
67
envTokenVariable = "OP_CONNECT_TOKEN"
68
68
)
69
69
70
+ type Opts struct {
71
+ UserAgent string
72
+ Client HTTClient
73
+ }
74
+
75
+ type ClientOptsFn func (opts * Opts )
76
+
77
+ // WithUserAgent configures the userAgent for the client.
78
+ func WithUserAgent (userAgent string ) ClientOptsFn {
79
+ return func (opts * Opts ) {
80
+ opts .UserAgent = userAgent
81
+ }
82
+ }
83
+
84
+ // WithClient configures the underlying http connection for the client.
85
+ func WithClient (client HTTClient ) ClientOptsFn {
86
+ return func (opts * Opts ) {
87
+ opts .Client = client
88
+ }
89
+ }
90
+
70
91
// NewClientFromEnvironment Returns a Secret Service client assuming that your
71
92
// jwt is set in the OP_TOKEN environment variable
72
93
func NewClientFromEnvironment () (Client , error ) {
@@ -84,17 +105,26 @@ func NewClientFromEnvironment() (Client, error) {
84
105
}
85
106
86
107
// NewClient Returns a Secret Service client for a given url and jwt
87
- func NewClient (url string , token string ) Client {
88
- return NewClientWithUserAgent (url , token , fmt .Sprintf (defaultUserAgent , SDKVersion ))
108
+ func NewClient (url string , token string , opts ... ClientOptsFn ) Client {
109
+ return NewClientWithUserAgent (url , token , fmt .Sprintf (defaultUserAgent , SDKVersion ), opts ... )
89
110
}
90
111
91
112
// NewClientWithUserAgent Returns a Secret Service client for a given url and jwt and identifies with userAgent
92
- func NewClientWithUserAgent (url string , token string , userAgent string ) Client {
113
+ func NewClientWithUserAgent (url string , token string , userAgent string , opts ... ClientOptsFn ) Client {
114
+ defaultOpts := & Opts {
115
+ UserAgent : userAgent ,
116
+ Client : http .DefaultClient ,
117
+ }
118
+
119
+ for _ , opt := range opts {
120
+ opt (defaultOpts )
121
+ }
122
+
93
123
if ! opentracing .IsGlobalTracerRegistered () {
94
124
cfg := jaegerClientConfig.Configuration {}
95
125
zipkinPropagator := zipkin .NewZipkinB3HTTPHeaderPropagator ()
96
126
cfg .InitGlobalTracer (
97
- userAgent ,
127
+ defaultOpts . UserAgent ,
98
128
jaegerClientConfig .Injector (opentracing .HTTPHeaders , zipkinPropagator ),
99
129
jaegerClientConfig .Extractor (opentracing .HTTPHeaders , zipkinPropagator ),
100
130
jaegerClientConfig .ZipkinSharedRPCSpan (true ),
@@ -105,10 +135,10 @@ func NewClientWithUserAgent(url string, token string, userAgent string) Client {
105
135
URL : url ,
106
136
Token : token ,
107
137
108
- userAgent : userAgent ,
138
+ userAgent : defaultOpts . UserAgent ,
109
139
tracer : opentracing .GlobalTracer (),
110
140
111
- client : http . DefaultClient ,
141
+ client : defaultOpts . Client ,
112
142
}
113
143
}
114
144
@@ -117,7 +147,7 @@ type restClient struct {
117
147
Token string
118
148
userAgent string
119
149
tracer opentracing.Tracer
120
- client httpClient
150
+ client HTTClient
121
151
}
122
152
123
153
// GetVaults Get a list of all available vaults
0 commit comments