Skip to content

Commit 931c4fe

Browse files
authored
Merge pull request #81 from deploymenttheory/dev
Refactor HTTP client request function to use logger.Logger interface
2 parents 5886ab7 + a52dd5e commit 931c4fe

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

httpclient/httpclient_request.go

+17-11
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import (
1717
// This function serves as a dispatcher, deciding whether to execute the request with or without retry logic based on the
1818
// idempotency of the HTTP method. Idempotent methods (GET, PUT, DELETE) are executed with retries to handle transient errors
1919
// and rate limits, while non-idempotent methods (POST, PATCH) are executed without retries to avoid potential side effects
20-
// of duplicating non-idempotent operations.
20+
// of duplicating non-idempotent operations. function uses an instance of a logger implementing the logger.Logger interface, used to log informational messages, warnings, and
21+
// errors encountered during the execution of the request.
2122

2223
// Parameters:
2324
// - method: A string representing the HTTP method to be used for the request. This method determines the execution path
@@ -28,8 +29,6 @@ import (
2829
// is determined by the content-type header and the specific implementation of the API handler used by the client.
2930
// - out: A pointer to an output variable where the response will be deserialized. The function expects this to be a pointer to
3031
// a struct that matches the expected response schema.
31-
// - log: An instance of a logger implementing the logger.Logger interface, used to log informational messages, warnings, and
32-
// errors encountered during the execution of the request.
3332

3433
// Returns:
3534
// - *http.Response: The HTTP response received from the server. In case of successful execution, this response contains
@@ -59,11 +58,14 @@ import (
5958
// - The decision to retry requests is based on the idempotency of the HTTP method and the client's retry configuration,
6059
// including maximum retry attempts and total retry duration.
6160

62-
func (c *Client) DoRequest(method, endpoint string, body, out interface{}, log logger.Logger) (*http.Response, error) {
61+
func (c *Client) DoRequest(method, endpoint string, body, out interface{}) (*http.Response, error) {
62+
63+
log := c.Logger
64+
6365
if IsIdempotentHTTPMethod(method) {
64-
return c.executeRequestWithRetries(method, endpoint, body, out, log)
66+
return c.executeRequestWithRetries(method, endpoint, body, out)
6567
} else if IsNonIdempotentHTTPMethod(method) {
66-
return c.executeRequest(method, endpoint, body, out, log)
68+
return c.executeRequest(method, endpoint, body, out)
6769
} else {
6870
return nil, log.Error("HTTP method not supported", zap.String("method", method))
6971
}
@@ -73,7 +75,8 @@ func (c *Client) DoRequest(method, endpoint string, body, out interface{}, log l
7375
// It is designed for idempotent HTTP methods (GET, PUT, DELETE), where the request can be safely retried in case of
7476
// transient errors or rate limiting. The function implements a retry mechanism that respects the client's configuration
7577
// for maximum retry attempts and total retry duration. Each retry attempt uses exponential backoff with jitter to avoid
76-
// thundering herd problems.
78+
// thundering herd problems. An instance of a logger (conforming to the logger.Logger interface) is used for logging the
79+
// request, retry attempts, and any errors encountered.
7780
//
7881
// Parameters:
7982
// - method: The HTTP method to be used for the request (e.g., "GET", "PUT", "DELETE").
@@ -83,8 +86,7 @@ func (c *Client) DoRequest(method, endpoint string, body, out interface{}, log l
8386
// methods that do not send a payload.
8487
// - out: A pointer to the variable where the unmarshaled response will be stored. The function expects this to be a
8588
// pointer to a struct that matches the expected response schema.
86-
// - log: An instance of a logger (conforming to the logger.Logger interface) used for logging the request, retry
87-
// attempts, and any errors encountered.
89+
// - log:
8890
//
8991
// Returns:
9092
// - *http.Response: The HTTP response from the server, which may be the response from a successful request or the last
@@ -102,7 +104,9 @@ func (c *Client) DoRequest(method, endpoint string, body, out interface{}, log l
102104
// - The function respects the client's concurrency token, acquiring and releasing it as needed to ensure safe concurrent
103105
// operations.
104106
// - The retry mechanism employs exponential backoff with jitter to mitigate the impact of retries on the server.
105-
func (c *Client) executeRequestWithRetries(method, endpoint string, body, out interface{}, log logger.Logger) (*http.Response, error) {
107+
func (c *Client) executeRequestWithRetries(method, endpoint string, body, out interface{}) (*http.Response, error) {
108+
log := c.Logger
109+
106110
// Include the core logic for handling non-idempotent requests with retries here.
107111
log.Debug("Executing request with retries", zap.String("method", method), zap.String("endpoint", endpoint))
108112

@@ -244,7 +248,9 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
244248
// execution.
245249
// - The function logs detailed information about the request execution, including the method, endpoint, status code, and
246250
// any errors encountered.
247-
func (c *Client) executeRequest(method, endpoint string, body, out interface{}, log logger.Logger) (*http.Response, error) {
251+
func (c *Client) executeRequest(method, endpoint string, body, out interface{}) (*http.Response, error) {
252+
log := c.Logger
253+
248254
// Include the core logic for handling idempotent requests here.
249255
log.Debug("Executing request without retries", zap.String("method", method), zap.String("endpoint", endpoint))
250256

0 commit comments

Comments
 (0)