diff --git a/httpclient/httpclient_headers_old.go b/httpclient/httpclient_headers_old.go deleted file mode 100644 index 9a73795..0000000 --- a/httpclient/httpclient_headers_old.go +++ /dev/null @@ -1,47 +0,0 @@ -package httpclient - -/* -// SetRequestHeaders sets the necessary HTTP headers for a given request. It configures the Authorization, -// Content-Type, and Accept headers based on the client's current token, the content type specified by the -// caller, and the preferred response formats defined by the APIHandler's GetAcceptHeader method. -// Additionally, it sets a User-Agent header to identify the client making the request. -// If debug logging is enabled, the function logs all set headers, with sensitive information such as the -// Authorization token being redacted for security purposes. -// -// Parameters: -// - req: The *http.Request object to which headers will be added. This request is prepared by the caller -// and passed to SetRequestHeaders for header configuration. -// -// - contentType: A string specifying the content type of the request, typically determined by the APIHandler's -// logic based on the request's nature and the endpoint being accessed. -// -// - acceptHeader: A string specifying the Accept header value, which is obtained from the APIHandler's -// GetAcceptHeader method. This header indicates the MIME types that the client can process, with preferences -// expressed through quality factors (q-values). -// -// - log: A logger.Logger instance used for logging header information when debug logging is enabled. The -// logger's level controls whether headers are logged, with logging occurring only at LogLevelDebug or lower. -// -// The function leverages the APIHandler interface to dynamically determine the appropriate Accept header, -// ensuring compatibility with the API's supported response formats. This approach allows for flexible and -// context-aware setting of request headers, facilitating effective communication with diverse APIs managed -// by different handlers, such as the JamfAPIHandler example provided in the api_handler.go file. -func (c *Client) SetRequestHeaders(req *http.Request, contentType, acceptHeader string, log logger.Logger) { - // Set Headers - req.Header.Add("Authorization", "Bearer "+c.Token) - req.Header.Add("Content-Type", contentType) - req.Header.Add("Accept", acceptHeader) - req.Header.Set("User-Agent", GetUserAgentHeader()) - - // Debug: Print request headers if debug logging is enabled - if log.GetLogLevel() <= logger.LogLevelDebug { - redactedAuthorization := RedactSensitiveData(c, "Authorization", req.Header.Get("Authorization")) - log.Debug("HTTP Request Headers", - zap.String("Authorization", redactedAuthorization), - zap.String("Content-Type", req.Header.Get("Content-Type")), - zap.String("Accept", req.Header.Get("Accept")), - zap.String("User-Agent", req.Header.Get("User-Agent")), - ) - } -} -*/ diff --git a/httpclient/httpclient_request.go b/httpclient/httpclient_request.go index 5577b66..c810bb2 100644 --- a/httpclient/httpclient_request.go +++ b/httpclient/httpclient_request.go @@ -124,16 +124,13 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in } }() - // Determine which set of encoding and content-type request rules to use - // apiHandler := c.APIHandler - - // Marshal Request with correct encoding + // Marshal Request with correct encoding defined in api handler requestData, err := c.APIHandler.MarshalRequest(body, method, endpoint, log) if err != nil { return nil, err } - // Construct URL using the ConstructAPIResourceEndpoint function + // Construct URL with correct structure defined in api handler url := c.APIHandler.ConstructAPIResourceEndpoint(c.InstanceName, endpoint, log) // Initialize total request counter @@ -160,10 +157,16 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in for time.Now().Before(totalRetryDeadline) { // Check if the current time is before the total retry deadline req = req.WithContext(ctx) resp, err = c.executeHTTPRequest(req, log, method, endpoint) + // Check for successful status code if err == nil && resp.StatusCode >= 200 && resp.StatusCode < 300 { return resp, c.handleSuccessResponse(resp, out, log, method, endpoint) } - + // Check for non-retryable errors + if resp != nil && errors.IsNonRetryableError(resp) { + log.Info("Non-retryable error received", zap.Int("status_code", resp.StatusCode)) + return resp, errors.HandleAPIError(resp, log) + } + // Check for retryable errors if errors.IsRateLimitError(resp) || errors.IsTransientError(resp) { retryCount++ if retryCount > c.clientConfig.ClientOptions.MaxRetryAttempts { @@ -175,7 +178,7 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in time.Sleep(waitDuration) continue } - + // Handle error responses if err != nil || !errors.IsRetryableStatusCode(resp.StatusCode) { if apiErr := errors.HandleAPIError(resp, log); apiErr != nil { err = apiErr @@ -183,7 +186,7 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in break } } - + // Handles final non-API error. if err != nil { return nil, err }