Skip to content

Commit 60eed37

Browse files
authored
Merge pull request #57 from deploymenttheory/dev
Dev
2 parents 41b2748 + d92875a commit 60eed37

File tree

3 files changed

+64
-18
lines changed

3 files changed

+64
-18
lines changed

apihandlers/jamfpro/jamfpro_api_response.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,65 @@ import (
1414
)
1515

1616
// Functions
17+
func (j *JamfAPIHandler) HandleAPISuccessResponse(resp *http.Response, out interface{}, log logger.Logger) error {
18+
// Special handling for DELETE requests
19+
if resp.Request.Method == "DELETE" {
20+
return j.handleDeleteRequest(resp)
21+
}
22+
23+
// Read the response body
24+
bodyBytes, err := j.readResponseBody(resp)
25+
if err != nil {
26+
return err
27+
}
28+
29+
// Log the raw response details for debugging
30+
j.logResponseDetails(resp, bodyBytes)
31+
32+
// Unmarshal the response based on content type
33+
contentType := resp.Header.Get("Content-Type")
34+
35+
// Check for binary data handling
36+
contentDisposition := resp.Header.Get("Content-Disposition")
37+
if err := j.handleBinaryData(contentType, contentDisposition, bodyBytes, out); err != nil {
38+
return err
39+
}
40+
41+
return j.unmarshalResponse(contentType, bodyBytes, out)
42+
}
43+
44+
func (j *JamfAPIHandler) HandleAPIErrorResponse(resp *http.Response, out interface{}, log logger.Logger) error {
45+
// Read the response body
46+
bodyBytes, err := j.readResponseBody(resp)
47+
if err != nil {
48+
return err
49+
}
50+
51+
// Convert bodyBytes to a string to represent the raw response body
52+
rawResponse := string(bodyBytes)
1753

54+
// Log the raw response details for debugging
55+
j.logResponseDetails(resp, bodyBytes)
56+
57+
// Get the content type from the response headers
58+
contentType := resp.Header.Get("Content-Type")
59+
60+
// Handle known error content types (e.g., JSON, HTML)
61+
if strings.Contains(contentType, "application/json") {
62+
return j.handleErrorJSONResponse(bodyBytes, resp.StatusCode, rawResponse)
63+
} else if strings.Contains(contentType, "text/html") {
64+
return j.handleErrorHTMLResponse(bodyBytes, resp.StatusCode)
65+
}
66+
67+
// Generic error handling for unknown content types
68+
j.Logger.Error("Received non-success status code without detailed error response",
69+
zap.Int("status_code", resp.StatusCode),
70+
zap.String("raw_response", rawResponse),
71+
)
72+
return fmt.Errorf("received non-success status code: %d, raw response: %s", resp.StatusCode, rawResponse)
73+
}
74+
75+
/*
1876
// HandleResponse processes an HTTP response for a Jamf API request. It handles different response types and errors accordingly.
1977
func (j *JamfAPIHandler) HandleResponse(resp *http.Response, out interface{}, log logger.Logger) error {
2078
// Special handling for DELETE requests
@@ -62,7 +120,7 @@ func (j *JamfAPIHandler) HandleResponse(resp *http.Response, out interface{}, lo
62120
// Unmarshal the response based on content type
63121
return j.unmarshalResponse(contentType, bodyBytes, out)
64122
}
65-
123+
*/
66124
// handleDeleteRequest handles the special case for DELETE requests, where a successful response might not contain a body.
67125
func (j *JamfAPIHandler) handleDeleteRequest(resp *http.Response) error {
68126
if resp.StatusCode >= 200 && resp.StatusCode < 300 {

httpclient/httpclient_api_handler.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ type APIHandler interface {
1616
ConstructAPIAuthEndpoint(instanceName string, endpointPath string, log logger.Logger) string
1717
MarshalRequest(body interface{}, method string, endpoint string, log logger.Logger) ([]byte, error)
1818
MarshalMultipartRequest(fields map[string]string, files map[string]string, log logger.Logger) ([]byte, string, error)
19-
HandleResponse(resp *http.Response, out interface{}, log logger.Logger) error
19+
//HandleResponse(resp *http.Response, out interface{}, log logger.Logger) error
20+
HandleAPISuccessResponse(resp *http.Response, out interface{}, log logger.Logger) error
21+
HandleAPIErrorResponse(resp *http.Response, out interface{}, log logger.Logger) error
2022
GetContentTypeHeader(method string, log logger.Logger) string
2123
GetAcceptHeader() string
2224
GetDefaultBaseDomain() string

httpclient/httpclient_request.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -350,22 +350,8 @@ func (c *Client) executeHTTPRequest(req *http.Request, log logger.Logger, method
350350
//
351351
// Returns:
352352
// - An error object parsed from the HTTP response, indicating the nature of the failure.
353-
func (c *Client) handleErrorResponseV1(resp *http.Response, log logger.Logger, errorMessage, method, endpoint string) error {
354-
apiErr := errors.HandleAPIError(resp, log)
355-
356-
// Log the provided error message along with method, endpoint, and status code.
357-
log.Error(errorMessage,
358-
zap.String("method", method),
359-
zap.String("endpoint", endpoint),
360-
zap.Int("status_code", resp.StatusCode),
361-
zap.String("error", apiErr.Error()),
362-
)
363-
364-
return apiErr
365-
}
366-
367353
func (c *Client) handleErrorResponse(resp *http.Response, out interface{}, log logger.Logger, method, endpoint string) error {
368-
if err := c.APIHandler.HandleResponse(resp, out, log); err != nil {
354+
if err := c.APIHandler.HandleAPIErrorResponse(resp, out, log); err != nil {
369355
log.Error("Failed to unmarshal HTTP response",
370356
zap.String("method", method),
371357
zap.String("endpoint", endpoint),
@@ -395,7 +381,7 @@ func (c *Client) handleErrorResponse(resp *http.Response, out interface{}, log l
395381
// Returns:
396382
// - nil if the response was successfully unmarshalled into the 'out' parameter, or an error if unmarshalling failed.
397383
func (c *Client) handleSuccessResponse(resp *http.Response, out interface{}, log logger.Logger, method, endpoint string) error {
398-
if err := c.APIHandler.HandleResponse(resp, out, log); err != nil {
384+
if err := c.APIHandler.HandleAPISuccessResponse(resp, out, log); err != nil {
399385
log.Error("Failed to unmarshal HTTP response",
400386
zap.String("method", method),
401387
zap.String("endpoint", endpoint),

0 commit comments

Comments
 (0)