@@ -9,40 +9,21 @@ package httpclient
99
1010import (
1111 "fmt"
12- "io"
1312 "net/http"
1413 "net/http/cookiejar"
15- "net/url"
1614 "time"
1715
1816 "github.com/deploymenttheory/go-api-http-client/concurrency"
1917 "go.uber.org/zap"
2018)
2119
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
4021
4122// Master struct/object
4223type Client struct {
4324 config * ClientConfig
4425 Integration * APIIntegration
45- http HTTPExecutor
26+ http * http. Client
4627 Sugar * zap.SugaredLogger
4728 Concurrency * concurrency.ConcurrencyHandler
4829}
@@ -100,7 +81,7 @@ type ClientConfig struct {
10081 // RetryEligiableRequests when false bypasses any retry logic for a simpler request flow.
10182 RetryEligiableRequests bool `json:"retry_eligiable_requests"`
10283
103- HTTPExecutor HTTPExecutor
84+ HTTP http. Client
10485}
10586
10687// BuildClient creates a new HTTP client with the provided configuration.
@@ -124,17 +105,23 @@ func (c *ClientConfig) Build() (*Client, error) {
124105
125106 c .Sugar .Debug ("configuration valid" )
126107
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
128115
129116 cookieJar , err := cookiejar .New (nil )
130117 if err != nil {
131118 return nil , err
132119 }
133120
134- httpClient .SetCookieJar ( cookieJar )
121+ httpClient .Jar = cookieJar
135122
136123 if c .CustomRedirectPolicy != nil {
137- httpClient .SetRedirectPolicy ( c .CustomRedirectPolicy )
124+ httpClient .CheckRedirect = * c .CustomRedirectPolicy
138125 }
139126
140127 // TODO refactor concurrency
@@ -148,9 +135,11 @@ func (c *ClientConfig) Build() (*Client, error) {
148135 )
149136 }
150137
138+
139+
151140 client := & Client {
152141 Integration : & c .Integration ,
153- http : httpClient ,
142+ http : & httpClient ,
154143 config : c ,
155144 Sugar : c .Sugar ,
156145 Concurrency : concurrencyHandler ,
0 commit comments