Skip to content

Commit ded8d78

Browse files
committed
Aligning to Go Style guide as per Google
1 parent 2bdd828 commit ded8d78

File tree

5 files changed

+36
-28
lines changed

5 files changed

+36
-28
lines changed

httpclient/client.go

+28-16
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,33 @@ import (
2222

2323
// Master struct/object
2424
type Client struct {
25+
// Config
2526
config ClientConfig
26-
http *http.Client
2727

28-
AuthToken string
29-
AuthTokenExpiry time.Time
30-
Sugar *zap.SugaredLogger
31-
Concurrency *concurrency.ConcurrencyHandler
32-
Integration *APIIntegration
28+
// Integration
29+
Integration *APIIntegration
30+
31+
// Executor
32+
http *http.Client
33+
34+
// Logger
35+
Sugar *zap.SugaredLogger
36+
37+
// Concurrency Mananger
38+
Concurrency *concurrency.ConcurrencyHandler
3339
}
3440

3541
// Options/Variables for Client
3642
type ClientConfig struct {
3743
// Interface which implements the APIIntegration patterns. Integration handles all server/endpoint specific configuration, auth and vars.
3844
Integration APIIntegration
3945

46+
// TODO
47+
Sugar *zap.SugaredLogger
48+
49+
// Wether or not empty values will be set or an error thrown for missing items.
50+
PopulateDefaultValues bool
51+
4052
// HideSenitiveData controls if sensitive data will be visible in logs. Debug option which should be True in production use.
4153
HideSensitiveData bool `json:"hide_sensitive_data"`
4254

@@ -80,18 +92,17 @@ type ClientConfig struct {
8092
}
8193

8294
// BuildClient creates a new HTTP client with the provided configuration.
83-
func (c ClientConfig) BuildClient(populateDefaultValues bool, sugar *zap.SugaredLogger) (*Client, error) {
84-
85-
if sugar == nil {
95+
func (c ClientConfig) Build() (*Client, error) {
96+
if c.Sugar == nil {
8697
zapLogger, err := zap.NewProduction()
87-
sugar = zapLogger.Sugar()
88-
8998
if err != nil {
9099
return nil, err
91100
}
101+
102+
c.Sugar = zapLogger.Sugar()
92103
}
93104

94-
err := c.validateClientConfig(populateDefaultValues)
105+
err := c.validateClientConfig()
95106
if err != nil {
96107
return nil, fmt.Errorf("invalid configuration: %v", err)
97108
}
@@ -101,16 +112,17 @@ func (c ClientConfig) BuildClient(populateDefaultValues bool, sugar *zap.Sugared
101112
}
102113

103114
// TODO refactor redirects
104-
if err := redirecthandler.SetupRedirectHandler(httpClient, c.FollowRedirects, c.MaxRedirects, sugar); err != nil {
115+
if err := redirecthandler.SetupRedirectHandler(httpClient, c.FollowRedirects, c.MaxRedirects, c.Sugar); err != nil {
105116
return nil, fmt.Errorf("Failed to set up redirect handler: %v", err)
106117
}
107118

119+
// TODO refactor concurrency
108120
var concurrencyHandler *concurrency.ConcurrencyHandler
109121
if c.EnableConcurrencyManagement {
110122
concurrencyMetrics := &concurrency.ConcurrencyMetrics{}
111123
concurrencyHandler = concurrency.NewConcurrencyHandler(
112124
c.MaxConcurrentRequests,
113-
sugar,
125+
c.Sugar,
114126
concurrencyMetrics,
115127
)
116128
} else {
@@ -121,12 +133,12 @@ func (c ClientConfig) BuildClient(populateDefaultValues bool, sugar *zap.Sugared
121133
Integration: &c.Integration,
122134
http: httpClient,
123135
config: c,
124-
Sugar: sugar,
136+
Sugar: c.Sugar,
125137
Concurrency: concurrencyHandler,
126138
}
127139

128140
if len(client.config.CustomCookies) > 0 {
129-
client.loadCustomCookies(c.CustomCookies)
141+
client.loadCustomCookies()
130142
}
131143

132144
return client, nil

httpclient/config_validation.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ func LoadConfigFromEnv() (*ClientConfig, error) {
9696
}
9797

9898
// TODO Review validateClientConfig
99-
func (c ClientConfig) validateClientConfig(populateDefaults bool) error {
99+
func (c ClientConfig) validateClientConfig() error {
100100

101-
if populateDefaults {
101+
if c.PopulateDefaultValues {
102102
c.SetDefaultValuesClientConfig()
103103
}
104104

httpclient/cookies.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
package httpclient
22

33
import (
4-
"net/http"
54
"net/http/cookiejar"
65
"net/url"
76
)
87

9-
func (c *Client) loadCustomCookies(cookiesList []*http.Cookie) error {
8+
func (c *Client) loadCustomCookies() error {
109
cookieJar, err := cookiejar.New(nil)
1110
if err != nil {
1211
return err
1312
}
13+
c.http.Jar = cookieJar
1414

1515
cookieUrl, err := url.Parse((*c.Integration).GetFQDN())
16-
1716
if err != nil {
1817
return err
1918
}
2019

21-
c.http.Jar = cookieJar
22-
c.http.Jar.SetCookies(cookieUrl, cookiesList)
20+
c.http.Jar.SetCookies(cookieUrl, c.config.CustomCookies)
2321

2422
return nil
2523
}

httpclient/headers.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
)
99

1010
// CheckDeprecationHeader checks the response headers for the Deprecation header and logs a warning if present.
11-
func CheckDeprecationHeader(resp *http.Response, sugar *zap.SugaredLogger) {
11+
func (c *Client) CheckDeprecationHeader(resp *http.Response) {
1212
deprecationHeader := resp.Header.Get("Deprecation")
1313
if deprecationHeader != "" {
14-
sugar.Warn("API endpoint is deprecated",
14+
c.Sugar.Warn("API endpoint is deprecated",
1515
zap.String("Date", deprecationHeader),
1616
zap.String("Endpoint", resp.Request.URL.String()),
1717
)

httpclient/request.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,7 @@ func (c *Client) doRequest(ctx context.Context, method, endpoint string, body in
289289
// c.Concurrency.EvaluateAndAdjustConcurrency(resp, duration)
290290
// }
291291

292-
// TODO review LogCookies
293-
294-
CheckDeprecationHeader(resp, c.Sugar)
292+
c.CheckDeprecationHeader(resp)
295293

296294
c.Sugar.Debug("Request sent successfully", zap.String("method", method), zap.String("endpoint", endpoint), zap.Int("status_code", resp.StatusCode))
297295

0 commit comments

Comments
 (0)