@@ -48,6 +48,9 @@ import (
4848 "strings"
4949
5050 _ "embed"
51+
52+ "github.com/deploymenttheory/go-api-http-client/internal/logger"
53+ "go.uber.org/zap"
5154)
5255
5356// Endpoint constants represent the URL suffixes used for Jamf API token interactions.
@@ -124,19 +127,19 @@ func (j *JamfAPIHandler) GetBaseDomain() string {
124127 return DefaultBaseDomain
125128}
126129
127- // ConstructAPIResourceEndpoint returns the full URL for a Jamf API resource endpoint path.
128- func (j * JamfAPIHandler ) ConstructAPIResourceEndpoint (endpointPath string , logger Logger ) string {
130+ // ConstructAPIResourceEndpoint constructs the full URL for a Jamf API resource endpoint path and logs the URL .
131+ func (j * JamfAPIHandler ) ConstructAPIResourceEndpoint (endpointPath string , log logger. Logger ) string {
129132 baseDomain := j .GetBaseDomain ()
130133 url := fmt .Sprintf ("https://%s%s%s" , j .InstanceName , baseDomain , endpointPath )
131- logger .Info ("Request will be made to API URL: " , "URL" , url )
134+ log .Info ("Constructed API resource endpoint URL" , zap . String ( "URL" , url ) )
132135 return url
133136}
134137
135- // ConstructAPIAuthEndpoint returns the full URL for a Jamf API auth endpoint path.
136- func (j * JamfAPIHandler ) ConstructAPIAuthEndpoint (endpointPath string , logger Logger ) string {
138+ // ConstructAPIAuthEndpoint constructs the full URL for a Jamf API auth endpoint path and logs the URL .
139+ func (j * JamfAPIHandler ) ConstructAPIAuthEndpoint (endpointPath string , log logger. Logger ) string {
137140 baseDomain := j .GetBaseDomain ()
138141 url := fmt .Sprintf ("https://%s%s%s" , j .InstanceName , baseDomain , endpointPath )
139- logger .Info ("Request will be made to API authentication URL: " , "URL" , url )
142+ log .Info ("Constructed API authentication URL" , zap . String ( "URL" , url ) )
140143 return url
141144}
142145
@@ -148,36 +151,36 @@ func (j *JamfAPIHandler) ConstructAPIAuthEndpoint(endpointPath string, logger Lo
148151// - For url endpoints starting with "/api", it defaults to "application/json" for the JamfPro API.
149152// If the endpoint does not match any of the predefined patterns, "application/json" is used as a fallback.
150153// This method logs the decision process at various stages for debugging purposes.
151- func (u * JamfAPIHandler ) GetContentTypeHeader (endpoint string , logger Logger ) string {
154+ func (u * JamfAPIHandler ) GetContentTypeHeader (endpoint string , log logger. Logger ) string {
152155 // Dynamic lookup from configuration should be the first priority
153156 for key , config := range configMap {
154157 if strings .HasPrefix (endpoint , key ) {
155158 if config .ContentType != nil {
156- logger .Debug ("Content-Type for endpoint found in configMap" , "endpoint" , endpoint , "content_type" , * config .ContentType )
159+ log .Debug ("Content-Type for endpoint found in configMap" , zap . String ( "endpoint" , endpoint ), zap . String ( "content_type" , * config .ContentType ) )
157160 return * config .ContentType
158161 }
159- logger .Debug ("Content-Type for endpoint is nil in configMap, handling as special case" , "endpoint" , endpoint )
162+ log .Debug ("Content-Type for endpoint is nil in configMap, handling as special case" , zap . String ( "endpoint" , endpoint ) )
160163 // If a nil ContentType is an expected case, do not set Content-Type header.
161164 return "" // Return empty to indicate no Content-Type should be set.
162165 }
163166 }
164167
165168 // If no specific configuration is found, then check for standard URL patterns.
166169 if strings .Contains (endpoint , "/JSSResource" ) {
167- logger .Debug ("Content-Type for endpoint defaulting to XML for Classic API" , "endpoint" , endpoint )
170+ log .Debug ("Content-Type for endpoint defaulting to XML for Classic API" , zap . String ( "endpoint" , endpoint ) )
168171 return "application/xml" // Classic API uses XML
169172 } else if strings .Contains (endpoint , "/api" ) {
170- logger .Debug ("Content-Type for endpoint defaulting to JSON for JamfPro API" , "endpoint" , endpoint )
173+ log .Debug ("Content-Type for endpoint defaulting to JSON for JamfPro API" , zap . String ( "endpoint" , endpoint ) )
171174 return "application/json" // JamfPro API uses JSON
172175 }
173176
174177 // Fallback to JSON if no other match is found.
175- logger .Debug ("Content-Type for endpoint not found in configMap or standard patterns, using default JSON" , "endpoint" , endpoint )
178+ log .Debug ("Content-Type for endpoint not found in configMap or standard patterns, using default JSON" , zap . String ( "endpoint" , endpoint ) )
176179 return "application/json"
177180}
178181
179182// MarshalRequest encodes the request body according to the endpoint for the API.
180- func (u * JamfAPIHandler ) MarshalRequest (body interface {}, method string , endpoint string , logger Logger ) ([]byte , error ) {
183+ func (u * JamfAPIHandler ) MarshalRequest (body interface {}, method string , endpoint string , log logger. Logger ) ([]byte , error ) {
181184 var (
182185 data []byte
183186 err error
@@ -199,47 +202,47 @@ func (u *JamfAPIHandler) MarshalRequest(body interface{}, method string, endpoin
199202 }
200203
201204 if method == "POST" || method == "PUT" {
202- logger .Debug ("XML Request Body: " , "Body" , string (data ))
205+ log .Debug ("XML Request Body" , zap . String ( "Body" , string (data ) ))
203206 }
204207
205208 case "json" :
206209 data , err = json .Marshal (body )
207210 if err != nil {
208- logger .Error ("Failed marshaling JSON request" , "error" , err )
211+ log .Error ("Failed marshaling JSON request" , zap . Error ( err ) )
209212 return nil , err
210213 }
211214
212215 if method == "POST" || method == "PUT" || method == "PATCH" {
213- logger .Debug ("JSON Request Body: " , string (data ))
216+ log .Debug ("JSON Request Body" , zap . String ( "Body" , string (data ) ))
214217 }
215218 }
216219
217220 return data , nil
218221}
219222
220223// UnmarshalResponse decodes the response body from XML or JSON format depending on the Content-Type header.
221- func (u * JamfAPIHandler ) UnmarshalResponse (resp * http.Response , out interface {}, logger Logger ) error {
224+ func (u * JamfAPIHandler ) UnmarshalResponse (resp * http.Response , out interface {}, log logger. Logger ) error {
222225 // Handle DELETE method
223226 if resp .Request .Method == "DELETE" {
224227 if resp .StatusCode >= 200 && resp .StatusCode < 300 {
225228 return nil
226229 } else {
227- return fmt . Errorf ("DELETE request failed with status code: %d " , resp .StatusCode )
230+ return log . Error ("DELETE request failed" , zap . Int ( "Status Code " , resp .StatusCode ) )
228231 }
229232 }
230233
231234 bodyBytes , err := io .ReadAll (resp .Body )
232235 if err != nil {
233- logger .Error ("Failed reading response body" , "error" , err )
236+ log .Error ("Failed reading response body" , zap . Error ( err ) )
234237 return err
235238 }
236239
237240 // Log the raw response body and headers
238- logger . Trace ("Raw HTTP Response: " , string (bodyBytes ))
239- logger .Debug ("Unmarshaling response" , "status" , resp .Status )
241+ log . Debug ("Raw HTTP Response" , zap . String ( "Body" , string (bodyBytes ) ))
242+ log .Debug ("Unmarshaling response" , zap . String ( "status" , resp .Status ) )
240243
241244 // Log headers when in debug mode
242- logger .Debug ("HTTP Response Headers: " , resp .Header )
245+ log .Debug ("HTTP Response Headers" , zap . Any ( "Headers" , resp .Header ) )
243246
244247 // Check the Content-Type and Content-Disposition headers
245248 contentType := resp .Header .Get ("Content-Type" )
@@ -253,7 +256,7 @@ func (u *JamfAPIHandler) UnmarshalResponse(resp *http.Response, out interface{},
253256 // If content type is HTML, extract the error message
254257 if strings .Contains (contentType , "text/html" ) {
255258 errMsg := ExtractErrorMessageFromHTML (string (bodyBytes ))
256- logger .Warn ("Received HTML content" , "error_message" , errMsg , "status_code" , resp .StatusCode )
259+ log .Warn ("Received HTML content" , "error_message" , errMsg , "status_code" , resp .StatusCode )
257260 return & APIError {
258261 StatusCode : resp .StatusCode ,
259262 Message : errMsg ,
@@ -266,14 +269,14 @@ func (u *JamfAPIHandler) UnmarshalResponse(resp *http.Response, out interface{},
266269 if strings .Contains (contentType , "application/json" ) {
267270 description , err := ParseJSONErrorResponse (bodyBytes )
268271 if err != nil {
269- u . logger .Error ("Failed to parse JSON error response" , "error" , err )
272+ log .Error ("Failed to parse JSON error response" , "error" , err )
270273 return fmt .Errorf ("received non-success status code: %d and failed to parse error response" , resp .StatusCode )
271274 }
272275 return fmt .Errorf ("received non-success status code: %d, error: %s" , resp .StatusCode , description )
273276 }
274277
275278 // If the response is not JSON or another error occurs, return a generic error message
276- u . logger .Error ("Received non-success status code" , "status_code" , resp .StatusCode )
279+ log .Error ("Received non-success status code" , "status_code" , resp .StatusCode )
277280 return fmt .Errorf ("received non-success status code: %d" , resp .StatusCode )
278281 }
279282
@@ -293,12 +296,12 @@ func (u *JamfAPIHandler) UnmarshalResponse(resp *http.Response, out interface{},
293296 // If unmarshalling fails, check if the content might be HTML
294297 if strings .Contains (string (bodyBytes ), "<html>" ) {
295298 errMsg := ExtractErrorMessageFromHTML (string (bodyBytes ))
296- logger .Warn ("Received HTML content instead of expected format" , "error_message" , errMsg , "status_code" , resp .StatusCode )
299+ log .Warn ("Received HTML content instead of expected format" , "error_message" , errMsg , "status_code" , resp .StatusCode )
297300 return fmt .Errorf (errMsg )
298301 }
299302
300303 // Log the error and return it
301- logger .Error ("Failed to unmarshal response" , "error" , err )
304+ log .Error ("Failed to unmarshal response" , "error" , err )
302305 return fmt .Errorf ("failed to unmarshal response: %v" , err )
303306 }
304307
@@ -331,7 +334,7 @@ func (u *JamfAPIHandler) GetAcceptHeader() string {
331334}
332335
333336// MarshalMultipartFormData takes a map with form fields and file paths and returns the encoded body and content type.
334- func (u * JamfAPIHandler ) MarshalMultipartRequest (fields map [string ]string , files map [string ]string ) ([]byte , string , error ) {
337+ func (u * JamfAPIHandler ) MarshalMultipartRequest (fields map [string ]string , files map [string ]string , log logger. Logger ) ([]byte , string , error ) {
335338 body := & bytes.Buffer {}
336339 writer := multipart .NewWriter (body )
337340
0 commit comments