@@ -13,9 +13,6 @@ import (
13
13
"go.uber.org/zap"
14
14
)
15
15
16
- // TODO remove collapsable comment
17
-
18
- // region comment
19
16
// DoRequest constructs and executes an HTTP request based on the provided method, endpoint, request body, and output variable.
20
17
// This function serves as a dispatcher, deciding whether to execute the request with or without retry logic based on the
21
18
// idempotency of the HTTP method. Idempotent methods (GET, PUT, DELETE) are executed with retries to handle transient errors
@@ -24,46 +21,43 @@ import (
24
21
// used to log informational messages, warnings, and errors encountered during the execution of the request.
25
22
// It also applies redirect handling to the client if configured, allowing the client to follow redirects up to a maximum
26
23
// number of times.
27
-
28
24
// 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
+ //
38
34
// 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
+ //
46
42
// Usage:
47
43
// This function is the primary entry point for executing HTTP requests using the client. It abstracts away the details of
48
44
// request retries, serialization, and response handling, providing a simplified interface for making HTTP requests. It is
49
45
// suitable for a wide range of HTTP operations, from fetching data with GET requests to submitting data with POST requests.
50
-
51
46
// Example:
52
47
// var result MyResponseType
53
48
// 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
+ //
57
54
// // Use `result` or `resp` as needed
58
-
59
55
// 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.
67
61
func (c * Client ) DoRequest (method , endpoint string , body , out interface {}) (* http.Response , error ) {
68
62
if ! c .config .RetryEligiableRequests {
69
63
return c .requestNoRetries (method , endpoint , body , out )
@@ -73,9 +67,9 @@ func (c *Client) DoRequest(method, endpoint string, body, out interface{}) (*htt
73
67
return c .requestWithRetries (method , endpoint , body , out )
74
68
} else if ! IsIdempotentHTTPMethod (method ) {
75
69
return c .requestNoRetries (method , endpoint , body , out )
76
- } else {
77
- return nil , fmt .Errorf ("unsupported http method: %s" , method )
78
70
}
71
+
72
+ return nil , fmt .Errorf ("unsupported http method: %s" , method )
79
73
}
80
74
81
75
// region comment
@@ -85,7 +79,6 @@ func (c *Client) DoRequest(method, endpoint string, body, out interface{}) (*htt
85
79
// for maximum retry attempts and total retry duration. Each retry attempt uses exponential backoff with jitter to avoid
86
80
// thundering herd problems. An instance of a logger (conforming to the logger.Logger interface) is used for logging the
87
81
// request, retry attempts, and any errors encountered.
88
- //
89
82
// Parameters:
90
83
// - method: The HTTP method to be used for the request (e.g., "GET", "PUT", "DELETE").
91
84
// - 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
94
87
// methods that do not send a payload.
95
88
// - out: A pointer to the variable where the unmarshaled response will be stored. The function expects this to be a
96
89
// pointer to a struct that matches the expected response schema.
97
- //
98
90
// Returns:
99
91
// - *http.Response: The HTTP response from the server, which may be the response from a successful request or the last
100
92
// failed attempt if all retries are exhausted.
@@ -105,7 +97,6 @@ func (c *Client) DoRequest(method, endpoint string, body, out interface{}) (*htt
105
97
// This function should be used for operations that are safe to retry and where the client can tolerate the additional
106
98
// latency introduced by the retry mechanism. It is particularly useful for handling transient errors and rate limiting
107
99
// responses from the server.
108
- //
109
100
// Note:
110
101
// - The caller is responsible for closing the response body to prevent resource leaks.
111
102
// - 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
194
185
return resp , response .HandleAPIErrorResponse (resp , c .Sugar )
195
186
}
196
187
197
- // region comment
198
188
// executeRequest executes an HTTP request using the specified method, endpoint, and request body without implementing
199
189
// retry logic. It is primarily designed for non-idempotent HTTP methods like POST and PATCH, where the request should
200
190
// not be automatically retried within this function due to the potential side effects of re-submitting the same data.
201
- //
202
191
// Parameters:
203
192
// - method: The HTTP method to be used for the request, typically "POST" or "PATCH".
204
193
// - 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
217
206
// This function is suitable for operations where the request should not be retried automatically, such as data submission
218
207
// operations where retrying could result in duplicate data processing. It ensures that the request is executed exactly
219
208
// once and provides detailed logging for debugging purposes.
220
- //
221
209
// Note:
222
210
// - The caller is responsible for closing the response body to prevent resource leaks.
223
211
// - The function ensures concurrency control by acquiring and releasing a concurrency token before and after the request
224
212
// execution.
225
213
// - The function logs detailed information about the request execution, including the method, endpoint, status code, and
226
214
// any errors encountered.
227
- //
228
- // endregion
229
215
func (c * Client ) requestNoRetries (method , endpoint string , body , out interface {}) (* http.Response , error ) {
230
- // TODO review refactor execute Request
231
-
232
216
ctx := context .Background ()
233
217
234
218
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{}
249
233
return nil , response .HandleAPIErrorResponse (resp , c .Sugar )
250
234
}
251
235
236
+ // TODO function comment
252
237
func (c * Client ) request (ctx context.Context , method , endpoint string , body interface {}) (* http.Response , error ) {
253
238
254
239
// TODO Concurrency - Refactor or remove this
0 commit comments