Skip to content

Commit 7a832b6

Browse files
committed
tidying up, dir rename
1 parent 533117c commit 7a832b6

File tree

2 files changed

+29
-44
lines changed

2 files changed

+29
-44
lines changed

httpclient/request.go

+29-44
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ import (
1313
"go.uber.org/zap"
1414
)
1515

16-
// TODO remove collapsable comment
17-
18-
// region comment
1916
// DoRequest constructs and executes an HTTP request based on the provided method, endpoint, request body, and output variable.
2017
// This function serves as a dispatcher, deciding whether to execute the request with or without retry logic based on the
2118
// idempotency of the HTTP method. Idempotent methods (GET, PUT, DELETE) are executed with retries to handle transient errors
@@ -24,46 +21,43 @@ import (
2421
// used to log informational messages, warnings, and errors encountered during the execution of the request.
2522
// It also applies redirect handling to the client if configured, allowing the client to follow redirects up to a maximum
2623
// number of times.
27-
2824
// Parameters:
29-
// - method: A string representing the HTTP method to be used for the request. This method determines the execution path
30-
// and whether the request will be retried in case of failures.
31-
// - endpoint: The target API endpoint for the request. This should be a relative path that will be appended to the base URL
32-
// configured for the HTTP client.
33-
// - body: The payload for the request, which will be serialized into the request body. The serialization format (e.g., JSON, XML)
34-
// is determined by the content-type header and the specific implementation of the API handler used by the client.
35-
// - out: A pointer to an output variable where the response will be deserialized. The function expects this to be a pointer to
36-
// a struct that matches the expected response schema.
37-
25+
// - method: A string representing the HTTP method to be used for the request. This method determines the execution path
26+
// and whether the request will be retried in case of failures.
27+
// - endpoint: The target API endpoint for the request. This should be a relative path that will be appended to the base URL
28+
// configured for the HTTP client.
29+
// - body: The payload for the request, which will be serialized into the request body. The serialization format (e.g., JSON, XML)
30+
// is determined by the content-type header and the specific implementation of the API handler used by the client.
31+
// - out: A pointer to an output variable where the response will be deserialized. The function expects this to be a pointer to
32+
// a struct that matches the expected response schema.
33+
//
3834
// Returns:
39-
// - *http.Response: The HTTP response received from the server. In case of successful execution, this response contains
40-
// the status code, headers, and body of the response. In case of errors, particularly after exhausting retries for
41-
// idempotent methods, this response may contain the last received HTTP response that led to the failure.
42-
// - error: An error object indicating failure during request execution. This could be due to network issues, server errors,
43-
// or a failure in request serialization/deserialization. For idempotent methods, an error is returned if all retries are
44-
// exhausted without success.
45-
35+
// - *http.Response: The HTTP response received from the server. In case of successful execution, this response contains
36+
// the status code, headers, and body of the response. In case of errors, particularly after exhausting retries for
37+
// idempotent methods, this response may contain the last received HTTP response that led to the failure.
38+
// - error: An error object indicating failure during request execution. This could be due to network issues, server errors,
39+
// or a failure in request serialization/deserialization. For idempotent methods, an error is returned if all retries are
40+
// exhausted without success.
41+
//
4642
// Usage:
4743
// This function is the primary entry point for executing HTTP requests using the client. It abstracts away the details of
4844
// request retries, serialization, and response handling, providing a simplified interface for making HTTP requests. It is
4945
// suitable for a wide range of HTTP operations, from fetching data with GET requests to submitting data with POST requests.
50-
5146
// Example:
5247
// var result MyResponseType
5348
// resp, err := client.DoRequest("GET", "/api/resource", nil, &result, logger)
54-
// if err != nil {
55-
// // Handle error
56-
// }
49+
//
50+
// if err != nil {
51+
// // Handle error
52+
// }
53+
//
5754
// // Use `result` or `resp` as needed
58-
5955
// Note:
60-
// - The caller is responsible for closing the response body when not nil to avoid resource leaks.
61-
// - The function ensures concurrency control by managing concurrency tokens internally, providing safe concurrent operations
62-
// within the client's concurrency model.
63-
// - The decision to retry requests is based on the idempotency of the HTTP method and the client's retry configuration,
64-
// including maximum retry attempts and total retry duration.
65-
// endregion
66-
56+
// - The caller is responsible for closing the response body when not nil to avoid resource leaks.
57+
// - The function ensures concurrency control by managing concurrency tokens internally, providing safe concurrent operations
58+
// within the client's concurrency model.
59+
// - The decision to retry requests is based on the idempotency of the HTTP method and the client's retry configuration,
60+
// including maximum retry attempts and total retry duration.
6761
func (c *Client) DoRequest(method, endpoint string, body, out interface{}) (*http.Response, error) {
6862
if !c.config.RetryEligiableRequests {
6963
return c.requestNoRetries(method, endpoint, body, out)
@@ -73,9 +67,9 @@ func (c *Client) DoRequest(method, endpoint string, body, out interface{}) (*htt
7367
return c.requestWithRetries(method, endpoint, body, out)
7468
} else if !IsIdempotentHTTPMethod(method) {
7569
return c.requestNoRetries(method, endpoint, body, out)
76-
} else {
77-
return nil, fmt.Errorf("unsupported http method: %s", method)
7870
}
71+
72+
return nil, fmt.Errorf("unsupported http method: %s", method)
7973
}
8074

8175
// region comment
@@ -85,7 +79,6 @@ func (c *Client) DoRequest(method, endpoint string, body, out interface{}) (*htt
8579
// for maximum retry attempts and total retry duration. Each retry attempt uses exponential backoff with jitter to avoid
8680
// thundering herd problems. An instance of a logger (conforming to the logger.Logger interface) is used for logging the
8781
// request, retry attempts, and any errors encountered.
88-
//
8982
// Parameters:
9083
// - method: The HTTP method to be used for the request (e.g., "GET", "PUT", "DELETE").
9184
// - endpoint: The API endpoint to which the request will be sent. This should be a relative path that will be appended
@@ -94,7 +87,6 @@ func (c *Client) DoRequest(method, endpoint string, body, out interface{}) (*htt
9487
// methods that do not send a payload.
9588
// - out: A pointer to the variable where the unmarshaled response will be stored. The function expects this to be a
9689
// pointer to a struct that matches the expected response schema.
97-
//
9890
// Returns:
9991
// - *http.Response: The HTTP response from the server, which may be the response from a successful request or the last
10092
// failed attempt if all retries are exhausted.
@@ -105,7 +97,6 @@ func (c *Client) DoRequest(method, endpoint string, body, out interface{}) (*htt
10597
// This function should be used for operations that are safe to retry and where the client can tolerate the additional
10698
// latency introduced by the retry mechanism. It is particularly useful for handling transient errors and rate limiting
10799
// responses from the server.
108-
//
109100
// Note:
110101
// - The caller is responsible for closing the response body to prevent resource leaks.
111102
// - The function respects the client's concurrency token, acquiring and releasing it as needed to ensure safe concurrent
@@ -194,11 +185,9 @@ func (c *Client) requestWithRetries(method, endpoint string, body, out interface
194185
return resp, response.HandleAPIErrorResponse(resp, c.Sugar)
195186
}
196187

197-
// region comment
198188
// executeRequest executes an HTTP request using the specified method, endpoint, and request body without implementing
199189
// retry logic. It is primarily designed for non-idempotent HTTP methods like POST and PATCH, where the request should
200190
// not be automatically retried within this function due to the potential side effects of re-submitting the same data.
201-
//
202191
// Parameters:
203192
// - method: The HTTP method to be used for the request, typically "POST" or "PATCH".
204193
// - endpoint: The API endpoint to which the request will be sent. This should be a relative path that will be appended
@@ -217,18 +206,13 @@ func (c *Client) requestWithRetries(method, endpoint string, body, out interface
217206
// This function is suitable for operations where the request should not be retried automatically, such as data submission
218207
// operations where retrying could result in duplicate data processing. It ensures that the request is executed exactly
219208
// once and provides detailed logging for debugging purposes.
220-
//
221209
// Note:
222210
// - The caller is responsible for closing the response body to prevent resource leaks.
223211
// - The function ensures concurrency control by acquiring and releasing a concurrency token before and after the request
224212
// execution.
225213
// - The function logs detailed information about the request execution, including the method, endpoint, status code, and
226214
// any errors encountered.
227-
//
228-
// endregion
229215
func (c *Client) requestNoRetries(method, endpoint string, body, out interface{}) (*http.Response, error) {
230-
// TODO review refactor execute Request
231-
232216
ctx := context.Background()
233217

234218
c.Sugar.Debug("Executing request without retries", zap.String("method", method), zap.String("endpoint", endpoint))
@@ -249,6 +233,7 @@ func (c *Client) requestNoRetries(method, endpoint string, body, out interface{}
249233
return nil, response.HandleAPIErrorResponse(resp, c.Sugar)
250234
}
251235

236+
// TODO function comment
252237
func (c *Client) request(ctx context.Context, method, endpoint string, body interface{}) (*http.Response, error) {
253238

254239
// TODO Concurrency - Refactor or remove this
File renamed without changes.

0 commit comments

Comments
 (0)