Skip to content
This repository was archived by the owner on Jun 21, 2022. It is now read-only.

Commit 9ec501a

Browse files
committed
enable passing in a custom http client
1 parent 9da83de commit 9ec501a

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

digest_auth_client.go

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ import (
99
)
1010

1111
type DigestRequest struct {
12-
Body string
13-
Method string
14-
Password string
15-
URI string
16-
Username string
17-
Header http.Header
18-
Auth *authorization
19-
Wa *wwwAuthenticate
20-
CertVal bool
12+
Body string
13+
Method string
14+
Password string
15+
URI string
16+
Username string
17+
Header http.Header
18+
Auth *authorization
19+
Wa *wwwAuthenticate
20+
CertVal bool
21+
HTTPClient *http.Client
2122
}
2223

2324
type DigestTransport struct {
@@ -41,6 +42,23 @@ func NewTransport(username, password string) DigestTransport {
4142
return dt
4243
}
4344

45+
func (dr *DigestRequest) getHTTPClient() *http.Client {
46+
if dr.HTTPClient != nil {
47+
return dr.HTTPClient
48+
}
49+
tlsConfig := tls.Config{}
50+
if !dr.CertVal {
51+
tlsConfig.InsecureSkipVerify = true
52+
}
53+
54+
return &http.Client{
55+
Timeout: 30 * time.Second,
56+
Transport: &http.Transport{
57+
TLSClientConfig: &tlsConfig,
58+
},
59+
}
60+
}
61+
4462
// UpdateRequest is called when you want to reuse an existing
4563
// DigestRequest connection with new request information
4664
func (dr *DigestRequest) UpdateRequest(username, password, method, uri, body string) *DigestRequest {
@@ -84,16 +102,7 @@ func (dr *DigestRequest) Execute() (resp *http.Response, err error) {
84102
}
85103
req.Header = dr.Header
86104

87-
client := &http.Client{
88-
Timeout: 30 * time.Second,
89-
}
90-
91-
if !dr.CertVal {
92-
tr := &http.Transport{
93-
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
94-
}
95-
client.Transport = tr
96-
}
105+
client := dr.getHTTPClient()
97106

98107
if resp, err = client.Do(req); err != nil {
99108
return nil, err
@@ -155,16 +164,6 @@ func (dr *DigestRequest) executeRequest(authString string) (resp *http.Response,
155164
req.Header = dr.Header
156165
req.Header.Add("Authorization", authString)
157166

158-
client := &http.Client{
159-
Timeout: 30 * time.Second,
160-
}
161-
162-
if !dr.CertVal {
163-
tr := &http.Transport{
164-
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
165-
}
166-
client.Transport = tr
167-
}
168-
167+
client := dr.getHTTPClient()
169168
return client.Do(req)
170169
}

0 commit comments

Comments
 (0)