@@ -70,7 +70,7 @@ var jamfpro_api_exceptions_configuration []byte
70
70
71
71
// init is invoked automatically on package initialization and is responsible for
72
72
// setting up the default state of the package by loading the default configuration.
73
- // If an error occurs during the loading process, the program will terminate with a fatal error log .
73
+ // If an error occurs during the loading process, the program will terminate with a fatal error j.Logger .
74
74
func init () {
75
75
// Load the default configuration from an embedded resource.
76
76
err := loadDefaultConfig ()
@@ -163,15 +163,15 @@ func (j *JamfAPIHandler) SetBaseDomain() string {
163
163
func (j * JamfAPIHandler ) ConstructAPIResourceEndpoint (instanceName string , endpointPath string , log logger.Logger ) string {
164
164
urlBaseDomain := j .SetBaseDomain ()
165
165
url := fmt .Sprintf ("https://%s%s%s" , instanceName , urlBaseDomain , endpointPath )
166
- log .Info (fmt .Sprintf ("Constructed %s API resource endpoint URL" , APIName ), zap .String ("URL" , url ))
166
+ j . Logger .Info (fmt .Sprintf ("Constructed %s API resource endpoint URL" , APIName ), zap .String ("URL" , url ))
167
167
return url
168
168
}
169
169
170
170
// ConstructAPIAuthEndpoint constructs the full URL for a Jamf API auth endpoint path and logs the URL.
171
171
func (j * JamfAPIHandler ) ConstructAPIAuthEndpoint (instanceName string , endpointPath string , log logger.Logger ) string {
172
172
urlBaseDomain := j .SetBaseDomain ()
173
173
url := fmt .Sprintf ("https://%s%s%s" , instanceName , urlBaseDomain , endpointPath )
174
- log .Info (fmt .Sprintf ("Constructed %s API authentication URL" , APIName ), zap .String ("URL" , url ))
174
+ j . Logger .Info (fmt .Sprintf ("Constructed %s API authentication URL" , APIName ), zap .String ("URL" , url ))
175
175
return url
176
176
}
177
177
@@ -183,36 +183,36 @@ func (j *JamfAPIHandler) ConstructAPIAuthEndpoint(instanceName string, endpointP
183
183
// - For url endpoints starting with "/api", it defaults to "application/json" for the JamfPro API.
184
184
// If the endpoint does not match any of the predefined patterns, "application/json" is used as a fallback.
185
185
// This method logs the decision process at various stages for debugging purposes.
186
- func (u * JamfAPIHandler ) GetContentTypeHeader (endpoint string , log logger.Logger ) string {
186
+ func (j * JamfAPIHandler ) GetContentTypeHeader (endpoint string , log logger.Logger ) string {
187
187
// Dynamic lookup from configuration should be the first priority
188
188
for key , config := range configMap {
189
189
if strings .HasPrefix (endpoint , key ) {
190
190
if config .ContentType != nil {
191
- log .Debug ("Content-Type for endpoint found in configMap" , zap .String ("endpoint" , endpoint ), zap .String ("content_type" , * config .ContentType ))
191
+ j . Logger .Debug ("Content-Type for endpoint found in configMap" , zap .String ("endpoint" , endpoint ), zap .String ("content_type" , * config .ContentType ))
192
192
return * config .ContentType
193
193
}
194
- log .Debug ("Content-Type for endpoint is nil in configMap, handling as special case" , zap .String ("endpoint" , endpoint ))
194
+ j . Logger .Debug ("Content-Type for endpoint is nil in configMap, handling as special case" , zap .String ("endpoint" , endpoint ))
195
195
// If a nil ContentType is an expected case, do not set Content-Type header.
196
196
return "" // Return empty to indicate no Content-Type should be set.
197
197
}
198
198
}
199
199
200
200
// If no specific configuration is found, then check for standard URL patterns.
201
201
if strings .Contains (endpoint , "/JSSResource" ) {
202
- log .Debug ("Content-Type for endpoint defaulting to XML for Classic API" , zap .String ("endpoint" , endpoint ))
202
+ j . Logger .Debug ("Content-Type for endpoint defaulting to XML for Classic API" , zap .String ("endpoint" , endpoint ))
203
203
return "application/xml" // Classic API uses XML
204
204
} else if strings .Contains (endpoint , "/api" ) {
205
- log .Debug ("Content-Type for endpoint defaulting to JSON for JamfPro API" , zap .String ("endpoint" , endpoint ))
205
+ j . Logger .Debug ("Content-Type for endpoint defaulting to JSON for JamfPro API" , zap .String ("endpoint" , endpoint ))
206
206
return "application/json" // JamfPro API uses JSON
207
207
}
208
208
209
209
// Fallback to JSON if no other match is found.
210
- log .Debug ("Content-Type for endpoint not found in configMap or standard patterns, using default JSON" , zap .String ("endpoint" , endpoint ))
210
+ j . Logger .Debug ("Content-Type for endpoint not found in configMap or standard patterns, using default JSON" , zap .String ("endpoint" , endpoint ))
211
211
return "application/json"
212
212
}
213
213
214
214
// MarshalRequest encodes the request body according to the endpoint for the API.
215
- func (u * JamfAPIHandler ) MarshalRequest (body interface {}, method string , endpoint string , log logger.Logger ) ([]byte , error ) {
215
+ func (j * JamfAPIHandler ) MarshalRequest (body interface {}, method string , endpoint string , log logger.Logger ) ([]byte , error ) {
216
216
var (
217
217
data []byte
218
218
err error
@@ -234,54 +234,54 @@ func (u *JamfAPIHandler) MarshalRequest(body interface{}, method string, endpoin
234
234
}
235
235
236
236
if method == "POST" || method == "PUT" {
237
- log .Debug ("XML Request Body" , zap .String ("Body" , string (data )))
237
+ j . Logger .Debug ("XML Request Body" , zap .String ("Body" , string (data )))
238
238
}
239
239
240
240
case "json" :
241
241
data , err = json .Marshal (body )
242
242
if err != nil {
243
- log .Error ("Failed marshaling JSON request" , zap .Error (err ))
243
+ j . Logger .Error ("Failed marshaling JSON request" , zap .Error (err ))
244
244
return nil , err
245
245
}
246
246
247
247
if method == "POST" || method == "PUT" || method == "PATCH" {
248
- log .Debug ("JSON Request Body" , zap .String ("Body" , string (data )))
248
+ j . Logger .Debug ("JSON Request Body" , zap .String ("Body" , string (data )))
249
249
}
250
250
}
251
251
252
252
return data , nil
253
253
}
254
254
255
255
// UnmarshalResponse decodes the response body from XML or JSON format depending on the Content-Type header.
256
- func (u * JamfAPIHandler ) UnmarshalResponse (resp * http.Response , out interface {}, log logger.Logger ) error {
256
+ func (j * JamfAPIHandler ) UnmarshalResponse (resp * http.Response , out interface {}, log logger.Logger ) error {
257
257
// Handle DELETE method
258
258
if resp .Request .Method == "DELETE" {
259
259
if resp .StatusCode >= 200 && resp .StatusCode < 300 {
260
260
return nil
261
261
} else {
262
- return log .Error ("DELETE request failed" , zap .Int ("Status Code" , resp .StatusCode ))
262
+ return j . Logger .Error ("DELETE request failed" , zap .Int ("Status Code" , resp .StatusCode ))
263
263
}
264
264
}
265
265
266
266
bodyBytes , err := io .ReadAll (resp .Body )
267
267
if err != nil {
268
- log .Error ("Failed reading response body" , zap .Error (err ))
268
+ j . Logger .Error ("Failed reading response body" , zap .Error (err ))
269
269
return err
270
270
}
271
271
272
272
// Log the raw response body and headers
273
- log .Debug ("Raw HTTP Response" , zap .String ("Body" , string (bodyBytes )))
274
- log .Debug ("Unmarshaling response" , zap .String ("status" , resp .Status ))
273
+ j . Logger .Debug ("Raw HTTP Response" , zap .String ("Body" , string (bodyBytes )))
274
+ j . Logger .Debug ("Unmarshaling response" , zap .String ("status" , resp .Status ))
275
275
276
276
// Log headers when in debug mode
277
- log .Debug ("HTTP Response Headers" , zap .Any ("Headers" , resp .Header ))
277
+ j . Logger .Debug ("HTTP Response Headers" , zap .Any ("Headers" , resp .Header ))
278
278
279
279
// Check the Content-Type and Content-Disposition headers
280
280
contentType := resp .Header .Get ("Content-Type" )
281
281
contentDisposition := resp .Header .Get ("Content-Disposition" )
282
282
283
283
// Handle binary data if necessary
284
- if err := u .handleBinaryData (contentType , contentDisposition , bodyBytes , out ); err != nil {
284
+ if err := j .handleBinaryData (contentType , contentDisposition , bodyBytes , out ); err != nil {
285
285
return err
286
286
}
287
287
@@ -292,13 +292,13 @@ func (u *JamfAPIHandler) UnmarshalResponse(resp *http.Response, out interface{},
292
292
htmlErrorMessage := ExtractErrorMessageFromHTML (string (bodyBytes ))
293
293
294
294
// Log the HTML error message using Zap
295
- log .Error ("Received HTML error content" ,
295
+ j . Logger .Error ("Received HTML error content" ,
296
296
zap .String ("error_message" , htmlErrorMessage ),
297
297
zap .Int ("status_code" , resp .StatusCode ),
298
298
)
299
299
} else {
300
300
// Log a generic error message if the response is not HTML
301
- log .Error ("Received non-success status code without detailed error response" ,
301
+ j . Logger .Error ("Received non-success status code without detailed error response" ,
302
302
zap .Int ("status_code" , resp .StatusCode ),
303
303
)
304
304
}
@@ -311,22 +311,22 @@ func (u *JamfAPIHandler) UnmarshalResponse(resp *http.Response, out interface{},
311
311
description , err := ParseJSONErrorResponse (bodyBytes )
312
312
if err != nil {
313
313
// Log the error using the structured logger and return the error
314
- log .Error ("Failed to parse JSON error response" ,
314
+ j . Logger .Error ("Failed to parse JSON error response" ,
315
315
zap .Error (err ),
316
316
zap .Int ("status_code" , resp .StatusCode ),
317
317
)
318
318
return err
319
319
}
320
320
// Log the error with description using the structured logger and return the error
321
- log .Error ("Received non-success status code with JSON response" ,
321
+ j . Logger .Error ("Received non-success status code with JSON response" ,
322
322
zap .Int ("status_code" , resp .StatusCode ),
323
323
zap .String ("error_description" , description ),
324
324
)
325
325
return fmt .Errorf ("received non-success status code with JSON response: %s" , description )
326
326
}
327
327
328
328
// If the response is not JSON or another error occurs, log a generic error message and return an error
329
- log .Error ("Received non-success status code without JSON response" ,
329
+ j . Logger .Error ("Received non-success status code without JSON response" ,
330
330
zap .Int ("status_code" , resp .StatusCode ),
331
331
)
332
332
return fmt .Errorf ("received non-success status code without JSON response: %d" , resp .StatusCode )
@@ -350,19 +350,19 @@ func (u *JamfAPIHandler) UnmarshalResponse(resp *http.Response, out interface{},
350
350
htmlErrorMessage := ExtractErrorMessageFromHTML (string (bodyBytes ))
351
351
352
352
// Log the HTML error message
353
- log .Warn ("Received HTML content instead of expected format" ,
353
+ j . Logger .Warn ("Received HTML content instead of expected format" ,
354
354
zap .String ("error_message" , htmlErrorMessage ),
355
355
zap .Int ("status_code" , resp .StatusCode ),
356
356
)
357
357
358
358
// Use the HTML error message for logging the error
359
- log .Error ("Unmarshal error with HTML content" ,
359
+ j . Logger .Error ("Unmarshal error with HTML content" ,
360
360
zap .String ("error_message" , htmlErrorMessage ),
361
361
zap .Int ("status_code" , resp .StatusCode ),
362
362
)
363
363
} else {
364
364
// If the error is not due to HTML content, log the original error
365
- log .Error ("Unmarshal error" ,
365
+ j . Logger .Error ("Unmarshal error" ,
366
366
zap .Error (err ),
367
367
zap .Int ("status_code" , resp .StatusCode ),
368
368
)
@@ -379,7 +379,7 @@ func (u *JamfAPIHandler) UnmarshalResponse(resp *http.Response, out interface{},
379
379
// the server is informed of the client's versatile content handling capabilities while
380
380
// indicating a preference for XML. The specified MIME types cover common content formats like
381
381
// images, JSON, XML, HTML, plain text, and certificates, with a fallback option for all other types.
382
- func (u * JamfAPIHandler ) GetAcceptHeader () string {
382
+ func (j * JamfAPIHandler ) GetAcceptHeader () string {
383
383
weightedAcceptHeader := "application/x-x509-ca-cert;q=0.95," +
384
384
"application/pkix-cert;q=0.94," +
385
385
"application/pem-certificate-chain;q=0.93," +
@@ -399,7 +399,7 @@ func (u *JamfAPIHandler) GetAcceptHeader() string {
399
399
}
400
400
401
401
// MarshalMultipartFormData takes a map with form fields and file paths and returns the encoded body and content type.
402
- func (u * JamfAPIHandler ) MarshalMultipartRequest (fields map [string ]string , files map [string ]string , log logger.Logger ) ([]byte , string , error ) {
402
+ func (j * JamfAPIHandler ) MarshalMultipartRequest (fields map [string ]string , files map [string ]string , log logger.Logger ) ([]byte , string , error ) {
403
403
body := & bytes.Buffer {}
404
404
writer := multipart .NewWriter (body )
405
405
@@ -437,7 +437,7 @@ func (u *JamfAPIHandler) MarshalMultipartRequest(fields map[string]string, files
437
437
}
438
438
439
439
// handleBinaryData checks if the response should be treated as binary data and assigns to out if so.
440
- func (u * JamfAPIHandler ) handleBinaryData (contentType , contentDisposition string , bodyBytes []byte , out interface {}) error {
440
+ func (j * JamfAPIHandler ) handleBinaryData (contentType , contentDisposition string , bodyBytes []byte , out interface {}) error {
441
441
if strings .Contains (contentType , "application/octet-stream" ) || strings .HasPrefix (contentDisposition , "attachment" ) {
442
442
if outPointer , ok := out .(* []byte ); ok {
443
443
* outPointer = bodyBytes
0 commit comments