Skip to content

Commit 99f092e

Browse files
Merge pull request #282 from deploymenttheory/dev-loadbalance
feat: removed http executor functionality
2 parents 74c575b + f9d0df9 commit 99f092e

File tree

4 files changed

+18
-134
lines changed

4 files changed

+18
-134
lines changed

httpclient/client.go

+15-26
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,21 @@ package httpclient
99

1010
import (
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
4223
type 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,

httpclient/cookies.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ func (c *Client) loadCustomCookies() error {
1212
return err
1313
}
1414

15-
c.http.SetCookies(cookieUrl, c.config.CustomCookies)
15+
c.http.Jar.SetCookies(cookieUrl, c.config.CustomCookies)
1616

1717
if c.config.HideSensitiveData {
1818
c.Sugar.Debug("[REDACTED] cookies set successfully")
1919
} else {
20-
c.Sugar.Debug("custom cookies set: %v", c.http.Cookies(cookieUrl))
20+
c.Sugar.Debug("custom cookies set: %v", c.http.Jar.Cookies(cookieUrl))
2121
}
2222

2323
return nil

httpclient/http.go

-106
This file was deleted.

httpclient/request.go

+1
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ func (c *Client) request(ctx context.Context, method, endpoint string, body inte
271271
startTime := time.Now()
272272

273273
req = req.WithContext(ctx)
274+
274275
resp, err := c.http.Do(req)
275276
if err != nil {
276277
c.Sugar.Error("Failed to send request", zap.String("method", method), zap.String("endpoint", endpoint), zap.Error(err))

0 commit comments

Comments
 (0)