@@ -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.
1977func (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.
67125func (j * JamfAPIHandler ) handleDeleteRequest (resp * http.Response ) error {
68126 if resp .StatusCode >= 200 && resp .StatusCode < 300 {
0 commit comments