Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 15 additions & 26 deletions httpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,21 @@ package httpclient

import (
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"time"

"github.com/deploymenttheory/go-api-http-client/concurrency"
"go.uber.org/zap"
)

// HTTPExecutor is an interface which wraps http.Client to allow mocking.
type HTTPExecutor interface {

// Inherited
CloseIdleConnections()
Do(req *http.Request) (*http.Response, error)
Get(url string) (resp *http.Response, err error)
Head(url string) (resp *http.Response, err error)
Post(url string, contentType string, body io.Reader) (resp *http.Response, err error)
PostForm(url string, data url.Values) (resp *http.Response, err error)

// Additional
SetCookieJar(jar http.CookieJar)
SetCookies(url *url.URL, cookies []*http.Cookie)
SetCustomTimeout(time.Duration)
Cookies(*url.URL) []*http.Cookie
SetRedirectPolicy(*func(req *http.Request, via []*http.Request) error)
}
const DefaultTimeout time.Duration = 5 * time.Second

// Master struct/object
type Client struct {
config *ClientConfig
Integration *APIIntegration
http HTTPExecutor
http *http.Client
Sugar *zap.SugaredLogger
Concurrency *concurrency.ConcurrencyHandler
}
Expand Down Expand Up @@ -100,7 +81,7 @@ type ClientConfig struct {
// RetryEligiableRequests when false bypasses any retry logic for a simpler request flow.
RetryEligiableRequests bool `json:"retry_eligiable_requests"`

HTTPExecutor HTTPExecutor
HTTP http.Client
}

// BuildClient creates a new HTTP client with the provided configuration.
Expand All @@ -124,17 +105,23 @@ func (c *ClientConfig) Build() (*Client, error) {

c.Sugar.Debug("configuration valid")

httpClient := c.HTTPExecutor
httpClient := c.HTTP

if c.CustomTimeout == 0 {
c.CustomTimeout = DefaultTimeout
}

httpClient.Timeout = c.CustomTimeout

cookieJar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}

httpClient.SetCookieJar(cookieJar)
httpClient.Jar = cookieJar

if c.CustomRedirectPolicy != nil {
httpClient.SetRedirectPolicy(c.CustomRedirectPolicy)
httpClient.CheckRedirect = *c.CustomRedirectPolicy
}

// TODO refactor concurrency
Expand All @@ -148,9 +135,11 @@ func (c *ClientConfig) Build() (*Client, error) {
)
}



client := &Client{
Integration: &c.Integration,
http: httpClient,
http: &httpClient,
config: c,
Sugar: c.Sugar,
Concurrency: concurrencyHandler,
Expand Down
4 changes: 2 additions & 2 deletions httpclient/cookies.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ func (c *Client) loadCustomCookies() error {
return err
}

c.http.SetCookies(cookieUrl, c.config.CustomCookies)
c.http.Jar.SetCookies(cookieUrl, c.config.CustomCookies)

if c.config.HideSensitiveData {
c.Sugar.Debug("[REDACTED] cookies set successfully")
} else {
c.Sugar.Debug("custom cookies set: %v", c.http.Cookies(cookieUrl))
c.Sugar.Debug("custom cookies set: %v", c.http.Jar.Cookies(cookieUrl))
}

return nil
Expand Down
106 changes: 0 additions & 106 deletions httpclient/http.go

This file was deleted.

1 change: 1 addition & 0 deletions httpclient/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ func (c *Client) request(ctx context.Context, method, endpoint string, body inte
startTime := time.Now()

req = req.WithContext(ctx)

resp, err := c.http.Do(req)
if err != nil {
c.Sugar.Error("Failed to send request", zap.String("method", method), zap.String("endpoint", endpoint), zap.Error(err))
Expand Down
Loading