@@ -9,40 +9,21 @@ package httpclient
9
9
10
10
import (
11
11
"fmt"
12
- "io"
13
12
"net/http"
14
13
"net/http/cookiejar"
15
- "net/url"
16
14
"time"
17
15
18
16
"github.com/deploymenttheory/go-api-http-client/concurrency"
19
17
"go.uber.org/zap"
20
18
)
21
19
22
- // HTTPExecutor is an interface which wraps http.Client to allow mocking.
23
- type HTTPExecutor interface {
24
-
25
- // Inherited
26
- CloseIdleConnections ()
27
- Do (req * http.Request ) (* http.Response , error )
28
- Get (url string ) (resp * http.Response , err error )
29
- Head (url string ) (resp * http.Response , err error )
30
- Post (url string , contentType string , body io.Reader ) (resp * http.Response , err error )
31
- PostForm (url string , data url.Values ) (resp * http.Response , err error )
32
-
33
- // Additional
34
- SetCookieJar (jar http.CookieJar )
35
- SetCookies (url * url.URL , cookies []* http.Cookie )
36
- SetCustomTimeout (time.Duration )
37
- Cookies (* url.URL ) []* http.Cookie
38
- SetRedirectPolicy (* func (req * http.Request , via []* http.Request ) error )
39
- }
20
+ const DefaultTimeout time.Duration = 5 * time .Second
40
21
41
22
// Master struct/object
42
23
type Client struct {
43
24
config * ClientConfig
44
25
Integration * APIIntegration
45
- http HTTPExecutor
26
+ http * http. Client
46
27
Sugar * zap.SugaredLogger
47
28
Concurrency * concurrency.ConcurrencyHandler
48
29
}
@@ -100,7 +81,7 @@ type ClientConfig struct {
100
81
// RetryEligiableRequests when false bypasses any retry logic for a simpler request flow.
101
82
RetryEligiableRequests bool `json:"retry_eligiable_requests"`
102
83
103
- HTTPExecutor HTTPExecutor
84
+ HTTP http. Client
104
85
}
105
86
106
87
// BuildClient creates a new HTTP client with the provided configuration.
@@ -124,17 +105,23 @@ func (c *ClientConfig) Build() (*Client, error) {
124
105
125
106
c .Sugar .Debug ("configuration valid" )
126
107
127
- httpClient := c .HTTPExecutor
108
+ httpClient := c .HTTP
109
+
110
+ if c .CustomTimeout == 0 {
111
+ c .CustomTimeout = DefaultTimeout
112
+ }
113
+
114
+ httpClient .Timeout = c .CustomTimeout
128
115
129
116
cookieJar , err := cookiejar .New (nil )
130
117
if err != nil {
131
118
return nil , err
132
119
}
133
120
134
- httpClient .SetCookieJar ( cookieJar )
121
+ httpClient .Jar = cookieJar
135
122
136
123
if c .CustomRedirectPolicy != nil {
137
- httpClient .SetRedirectPolicy ( c .CustomRedirectPolicy )
124
+ httpClient .CheckRedirect = * c .CustomRedirectPolicy
138
125
}
139
126
140
127
// TODO refactor concurrency
@@ -148,9 +135,11 @@ func (c *ClientConfig) Build() (*Client, error) {
148
135
)
149
136
}
150
137
138
+
139
+
151
140
client := & Client {
152
141
Integration : & c .Integration ,
153
- http : httpClient ,
142
+ http : & httpClient ,
154
143
config : c ,
155
144
Sugar : c .Sugar ,
156
145
Concurrency : concurrencyHandler ,
0 commit comments