Skip to content

Commit 0871b9b

Browse files
committed
feat: add configurable http client
Signed-off-by: Gergely Brautigam <182850+Skarlso@users.noreply.github.com>
1 parent 587a4e7 commit 0871b9b

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

connect/client.go

+38-8
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type Client interface {
5858
LoadStruct(config interface{}) error
5959
}
6060

61-
type httpClient interface {
61+
type HTTClient interface {
6262
Do(req *http.Request) (*http.Response, error)
6363
}
6464

@@ -67,6 +67,27 @@ const (
6767
envTokenVariable = "OP_CONNECT_TOKEN"
6868
)
6969

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+
7091
// NewClientFromEnvironment Returns a Secret Service client assuming that your
7192
// jwt is set in the OP_TOKEN environment variable
7293
func NewClientFromEnvironment() (Client, error) {
@@ -84,17 +105,26 @@ func NewClientFromEnvironment() (Client, error) {
84105
}
85106

86107
// 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...)
89110
}
90111

91112
// 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+
93123
if !opentracing.IsGlobalTracerRegistered() {
94124
cfg := jaegerClientConfig.Configuration{}
95125
zipkinPropagator := zipkin.NewZipkinB3HTTPHeaderPropagator()
96126
cfg.InitGlobalTracer(
97-
userAgent,
127+
defaultOpts.UserAgent,
98128
jaegerClientConfig.Injector(opentracing.HTTPHeaders, zipkinPropagator),
99129
jaegerClientConfig.Extractor(opentracing.HTTPHeaders, zipkinPropagator),
100130
jaegerClientConfig.ZipkinSharedRPCSpan(true),
@@ -105,10 +135,10 @@ func NewClientWithUserAgent(url string, token string, userAgent string) Client {
105135
URL: url,
106136
Token: token,
107137

108-
userAgent: userAgent,
138+
userAgent: defaultOpts.UserAgent,
109139
tracer: opentracing.GlobalTracer(),
110140

111-
client: http.DefaultClient,
141+
client: defaultOpts.Client,
112142
}
113143
}
114144

@@ -117,7 +147,7 @@ type restClient struct {
117147
Token string
118148
userAgent string
119149
tracer opentracing.Tracer
120-
client httpClient
150+
client HTTClient
121151
}
122152

123153
// GetVaults Get a list of all available vaults

connect/client_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,36 @@ func TestNewClientWithUserAgent(t *testing.T) {
177177

178178
}
179179

180+
type dummyClient struct {
181+
testClient bool
182+
}
183+
184+
func (t *dummyClient) Do(req *http.Request) (*http.Response, error) {
185+
return nil, nil
186+
}
187+
188+
func TestNewClientWithOpts(t *testing.T) {
189+
d := &dummyClient{testClient: true}
190+
client := NewClient(validHost, validToken, WithUserAgent(testUserAgent), WithClient(d))
191+
192+
restClient, ok := client.(*restClient)
193+
if !ok {
194+
t.Log("Unable to cast client to rest client. Was expecting restClient")
195+
t.FailNow()
196+
}
197+
198+
if _, ok := restClient.client.(*dummyClient); !ok {
199+
t.Logf("Expected client to be of type dummyclient, got %T", restClient.client)
200+
t.FailNow()
201+
}
202+
203+
if restClient.userAgent != testUserAgent {
204+
t.Logf("Expected user-agent of %q, got %q", testUserAgent, restClient.userAgent)
205+
t.FailNow()
206+
}
207+
208+
}
209+
180210
func Test_restClient_GetVaults(t *testing.T) {
181211
mockHTTPClient.Dofunc = listVaults
182212
vaults, err := testClient.GetVaults()

0 commit comments

Comments
 (0)