You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// This function serves as a dispatcher, deciding whether to execute the request with or without retry logic based on the
18
19
// idempotency of the HTTP method. Idempotent methods (GET, PUT, DELETE) are executed with retries to handle transient errors
19
20
// and rate limits, while non-idempotent methods (POST, PATCH) are executed without retries to avoid potential side effects
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.
21
+
// of duplicating non-idempotent operations. The function uses an instance of a logger implementing the logger.Logger interface,
22
+
// used to log informational messages, warnings, and errors encountered during the execution of the request.
23
+
// It also applies redirect handling to the client if configured, allowing the client to follow redirects up to a maximum
24
+
// number of times.
22
25
23
26
// Parameters:
24
27
// - method: A string representing the HTTP method to be used for the request. This method determines the execution path
@@ -59,9 +62,12 @@ import (
59
62
// including maximum retry attempts and total retry duration.
http.StatusCreated: "Request to create or update resource successful.",
20
-
http.StatusAccepted: "The request was accepted for processing, but the processing has not completed.",
21
-
http.StatusNoContent: "Request successful. No content to send for this request.",
22
-
http.StatusBadRequest: "Bad request. Verify the syntax of the request.",
23
-
http.StatusUnauthorized: "Authentication failed. Verify the credentials being used for the request.",
24
-
http.StatusPaymentRequired: "Payment required. Access to the requested resource requires payment.",
25
-
http.StatusForbidden: "Invalid permissions. Verify the account has the proper permissions for the resource.",
26
-
http.StatusNotFound: "Resource not found. Verify the URL path is correct.",
27
-
http.StatusMethodNotAllowed: "Method not allowed. The method specified is not allowed for the resource.",
28
-
http.StatusNotAcceptable: "Not acceptable. The server cannot produce a response matching the list of acceptable values.",
29
-
http.StatusProxyAuthRequired: "Proxy authentication required. You must authenticate with a proxy server before this request can be served.",
30
-
http.StatusRequestTimeout: "Request timeout. The server timed out waiting for the request.",
31
-
http.StatusConflict: "Conflict. The request could not be processed because of conflict in the request.",
32
-
http.StatusGone: "Gone. The resource requested is no longer available and will not be available again.",
33
-
http.StatusLengthRequired: "Length required. The request did not specify the length of its content, which is required by the requested resource.",
34
-
http.StatusPreconditionFailed: "Precondition failed. The server does not meet one of the preconditions specified in the request.",
35
-
http.StatusRequestEntityTooLarge: "Payload too large. The request is larger than the server is willing or able to process.",
36
-
http.StatusRequestURITooLong: "Request-URI too long. The URI provided was too long for the server to process.",
37
-
http.StatusUnsupportedMediaType: "Unsupported media type. The request entity has a media type which the server or resource does not support.",
38
-
http.StatusRequestedRangeNotSatisfiable: "Requested range not satisfiable. The client has asked for a portion of the file, but the server cannot supply that portion.",
39
-
http.StatusExpectationFailed: "Expectation failed. The server cannot meet the requirements of the Expect request-header field.",
40
-
http.StatusUnprocessableEntity: "Unprocessable entity. The server understands the content type and syntax of the request but was unable to process the contained instructions.",
41
-
http.StatusLocked: "Locked. The resource that is being accessed is locked.",
42
-
http.StatusFailedDependency: "Failed dependency. The request failed because it depended on another request and that request failed.",
43
-
http.StatusUpgradeRequired: "Upgrade required. The client should switch to a different protocol.",
44
-
http.StatusPreconditionRequired: "Precondition required. The server requires that the request be conditional.",
45
-
http.StatusTooManyRequests: "Too many requests. The user has sent too many requests in a given amount of time.",
46
-
http.StatusRequestHeaderFieldsTooLarge: "Request header fields too large. The server is unwilling to process the request because its header fields are too large.",
47
-
http.StatusUnavailableForLegalReasons: "Unavailable for legal reasons. The server is denying access to the resource as a consequence of a legal demand.",
19
+
// Successful responses (200-299)
20
+
http.StatusOK: "Request successful.",
21
+
http.StatusCreated: "Request to create or update resource successful.",
22
+
http.StatusAccepted: "The request was accepted for processing, but the processing has not completed.",
23
+
http.StatusNoContent: "Request successful. No content to send for this request.",
24
+
25
+
// Redirect status codes (300-399)
26
+
http.StatusMovedPermanently: "Moved Permanently. The requested resource has been assigned a new permanent URI. Future references should use the returned URI.",
27
+
http.StatusFound: "Found. The requested resource resides temporarily under a different URI. The client should use the Request-URI for future requests.",
28
+
http.StatusSeeOther: "See Other. The response to the request can be found under a different URI. A GET method should be used to retrieve the resource.",
29
+
http.StatusTemporaryRedirect: "Temporary Redirect. The requested resource resides temporarily under a different URI. The request method should not change.",
30
+
http.StatusPermanentRedirect: "Permanent Redirect. The requested resource has been permanently moved to a new URI. The request method should not change.",
31
+
32
+
// Client error responses (400-499)
33
+
http.StatusBadRequest: "Bad request. Verify the syntax of the request.",
34
+
http.StatusUnauthorized: "Authentication failed. Verify the credentials being used for the request.",
35
+
http.StatusPaymentRequired: "Payment required. Access to the requested resource requires payment.",
36
+
http.StatusForbidden: "Invalid permissions. Verify the account has the proper permissions for the resource.",
37
+
http.StatusNotFound: "Resource not found. Verify the URL path is correct.",
38
+
http.StatusMethodNotAllowed: "Method not allowed. The method specified is not allowed for the resource.",
39
+
http.StatusNotAcceptable: "Not acceptable. The server cannot produce a response matching the list of acceptable values.",
40
+
http.StatusProxyAuthRequired: "Proxy authentication required. You must authenticate with a proxy server before this request can be served.",
41
+
http.StatusRequestTimeout: "Request timeout. The server timed out waiting for the request.",
42
+
http.StatusConflict: "Conflict. The request could not be processed because of conflict in the request.",
43
+
http.StatusGone: "Gone. The resource requested is no longer available and will not be available again.",
44
+
http.StatusLengthRequired: "Length required. The request did not specify the length of its content, which is required by the requested resource.",
45
+
http.StatusPreconditionFailed: "Precondition failed. The server does not meet one of the preconditions specified in the request.",
46
+
http.StatusRequestEntityTooLarge: "Payload too large. The request is larger than the server is willing or able to process.",
47
+
http.StatusRequestURITooLong: "Request-URI too long. The URI provided was too long for the server to process.",
48
+
http.StatusUnsupportedMediaType: "Unsupported media type. The request entity has a media type which the server or resource does not support.",
49
+
http.StatusRequestedRangeNotSatisfiable: "Requested range not satisfiable. The client has asked for a portion of the file, but the server cannot supply that portion.",
50
+
http.StatusExpectationFailed: "Expectation failed. The server cannot meet the requirements of the Expect request-header field.",
51
+
http.StatusUnprocessableEntity: "Unprocessable entity. The server understands the content type and syntax of the request but was unable to process the contained instructions.",
52
+
http.StatusLocked: "Locked. The resource that is being accessed is locked.",
53
+
http.StatusFailedDependency: "Failed dependency. The request failed because it depended on another request and that request failed.",
54
+
http.StatusUpgradeRequired: "Upgrade required. The client should switch to a different protocol.",
55
+
http.StatusPreconditionRequired: "Precondition required. The server requires that the request be conditional.",
56
+
http.StatusTooManyRequests: "Too many requests. The user has sent too many requests in a given amount of time.",
57
+
http.StatusRequestHeaderFieldsTooLarge: "Request header fields too large. The server is unwilling to process the request because its header fields are too large.",
58
+
http.StatusUnavailableForLegalReasons: "Unavailable for legal reasons. The server is denying access to the resource as a consequence of a legal demand.",
59
+
60
+
// Server error responses (500-599)
48
61
http.StatusInternalServerError: "Internal server error. The server encountered an unexpected condition that prevented it from fulfilling the request.",
49
62
http.StatusNotImplemented: "Not implemented. The server does not support the functionality required to fulfill the request.",
50
63
http.StatusBadGateway: "Bad gateway. The server received an invalid response from the upstream server while trying to fulfill the request.",
returnfmt.Sprintf("Unknown status code: %d", resp.StatusCode)
62
75
}
63
76
77
+
// IsRedirectStatusCode checks if the provided HTTP status code is one of the redirect codes.
78
+
// Redirect status codes instruct the client to make a new request to a different URI, as defined in the response's Location header.
79
+
//
80
+
// - 301 Moved Permanently: The requested resource has been assigned a new permanent URI and any future references to this resource should use one of the returned URIs.
81
+
// - 302 Found: The requested resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client should continue to use the Request-URI for future requests.
82
+
// - 303 See Other: The response to the request can be found under a different URI and should be retrieved using a GET method on that resource. This method exists primarily to allow the output of a POST-activated script to redirect the user agent to a selected resource.
83
+
// - 307 Temporary Redirect: The requested resource resides temporarily under a different URI. The client should not change the request method if it performs an automatic redirection to that URI.
84
+
// - 308 Permanent Redirect: The request and all future requests should be repeated using another URI. 308 parallel the behavior of 301 but do not allow the HTTP method to change. So, for example, submitting a form to a permanently redirected resource may continue smoothly.
85
+
//
86
+
// The function returns true if the statusCode is one of the above redirect statuses, indicating that the client should follow the redirection as specified in the Location header of the response.
87
+
funcIsRedirectStatusCode(statusCodeint) bool {
88
+
switchstatusCode {
89
+
casehttp.StatusMovedPermanently, // 301
90
+
http.StatusFound, // 302
91
+
http.StatusSeeOther, // 303
92
+
http.StatusTemporaryRedirect, // 307
93
+
http.StatusPermanentRedirect: // 308
94
+
returntrue
95
+
default:
96
+
returnfalse
97
+
}
98
+
}
99
+
64
100
// IsNonRetryableStatusCode checks if the provided response indicates a non-retryable error.
0 commit comments