@@ -153,7 +153,7 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
153
153
c .ConcurrencyHandler .Metrics .TotalRequests ++
154
154
c .ConcurrencyHandler .Metrics .Lock .Unlock ()
155
155
156
- // Perform Request
156
+ // Create a new HTTP request with the provided method, URL, and body
157
157
req , err := http .NewRequest (method , url , bytes .NewBuffer (requestData ))
158
158
if err != nil {
159
159
return nil , err
@@ -314,7 +314,7 @@ func (c *Client) executeRequest(method, endpoint string, body, out interface{})
314
314
// Construct URL using the ConstructAPIResourceEndpoint function
315
315
url := c .APIHandler .ConstructAPIResourceEndpoint (c .InstanceName , endpoint , log )
316
316
317
- // Perform Request
317
+ // Create a new HTTP request with the provided method, URL, and body
318
318
req , err := http .NewRequest (method , url , bytes .NewBuffer (requestData ))
319
319
if err != nil {
320
320
return nil , err
@@ -336,7 +336,7 @@ func (c *Client) executeRequest(method, endpoint string, body, out interface{})
336
336
return nil , err
337
337
}
338
338
339
- // Log incoming cookies
339
+ // Log outgoing cookies
340
340
log .LogCookies ("incoming" , req , method , endpoint )
341
341
342
342
// Checks for the presence of a deprecation header in the HTTP response and logs if found.
@@ -352,7 +352,8 @@ func (c *Client) executeRequest(method, endpoint string, body, out interface{})
352
352
}
353
353
354
354
// Handle error responses for status codes outside the successful range
355
- return nil , c .handleErrorResponse (resp , out , log , method , endpoint )
355
+ //return nil, c.handleErrorResponse(resp, out, log, method, endpoint)
356
+ return nil , response .HandleAPIErrorResponse (resp , log )
356
357
}
357
358
358
359
// do sends an HTTP request using the client's HTTP client. It logs the request and error details, if any,
@@ -378,20 +379,12 @@ func (c *Client) do(req *http.Request, log logger.Logger, method, endpoint strin
378
379
379
380
if err != nil {
380
381
// Log the error with structured logging, including method, endpoint, and the error itself
381
- log .Error ("Failed to send request" ,
382
- zap .String ("method" , method ),
383
- zap .String ("endpoint" , endpoint ),
384
- zap .Error (err ),
385
- )
382
+ log .Error ("Failed to send request" , zap .String ("method" , method ), zap .String ("endpoint" , endpoint ), zap .Error (err ))
386
383
return nil , err
387
384
}
388
385
389
386
// Log the response status code for successful requests
390
- log .Debug ("Request sent successfully" ,
391
- zap .String ("method" , method ),
392
- zap .String ("endpoint" , endpoint ),
393
- zap .Int ("status_code" , resp .StatusCode ),
394
- )
387
+ log .Debug ("Request sent successfully" , zap .String ("method" , method ), zap .String ("endpoint" , endpoint ), zap .Int ("status_code" , resp .StatusCode ))
395
388
396
389
return resp , nil
397
390
}
@@ -453,96 +446,3 @@ func (c *Client) handleSuccessResponse(resp *http.Response, out interface{}, log
453
446
)
454
447
return nil
455
448
}
456
-
457
- // DoMultipartRequest creates and executes a multipart HTTP request. It is used for sending files
458
- // and form fields in a single request. This method handles the construction of the multipart
459
- // message body, setting the appropriate headers, and sending the request to the given endpoint.
460
- //
461
- // Parameters:
462
- // - method: The HTTP method to use (e.g., POST, PUT).
463
- // - endpoint: The API endpoint to which the request will be sent.
464
- // - fields: A map of form fields and their values to include in the multipart message.
465
- // - files: A map of file field names to file paths that will be included as file attachments.
466
- // - out: A pointer to a variable where the unmarshaled response will be stored.
467
- //
468
- // Returns:
469
- // - A pointer to the http.Response received from the server.
470
- // - An error if the request could not be sent or the response could not be processed.
471
- //
472
- // The function first validates the authentication token, then constructs the multipart
473
- // request body based on the provided fields and files. It then constructs the full URL for
474
- // the request, sets the required headers (including Authorization and Content-Type), and
475
- // sends the request.
476
- //
477
- // If debug mode is enabled, the function logs all the request headers before sending the request.
478
- // After the request is sent, the function checks the response status code. If the response is
479
- // not within the success range (200-299), it logs an error and returns the response and an error.
480
- // If the response is successful, it attempts to unmarshal the response body into the 'out' parameter.
481
- //
482
- // Note:
483
- // The caller should handle closing the response body when successful.
484
- func (c * Client ) DoMultipartRequest (method , endpoint string , fields map [string ]string , files map [string ]string , out interface {}) (* http.Response , error ) {
485
- log := c .Logger
486
-
487
- // Auth Token validation check
488
- // valid, err := c.ValidAuthTokenCheck()
489
- // if err != nil || !valid {
490
- // return nil, err
491
- //}
492
-
493
- // Auth Token validation check
494
- clientCredentials := authenticationhandler.ClientCredentials {
495
- Username : c .clientConfig .Auth .Username ,
496
- Password : c .clientConfig .Auth .Password ,
497
- ClientID : c .clientConfig .Auth .ClientID ,
498
- ClientSecret : c .clientConfig .Auth .ClientSecret ,
499
- }
500
-
501
- valid , err := c .AuthTokenHandler .ValidAuthTokenCheck (c .APIHandler , c .httpClient , clientCredentials , c .clientConfig .ClientOptions .TokenRefreshBufferPeriod )
502
- if err != nil || ! valid {
503
- return nil , err
504
- }
505
-
506
- // Determine which set of encoding and content-type request rules to use
507
- //apiHandler := c.APIHandler
508
-
509
- // Marshal the multipart form data
510
- requestData , contentType , err := c .APIHandler .MarshalMultipartRequest (fields , files , log )
511
- if err != nil {
512
- return nil , err
513
- }
514
-
515
- // Construct URL using the ConstructAPIResourceEndpoint function
516
- url := c .APIHandler .ConstructAPIResourceEndpoint (c .InstanceName , endpoint , log )
517
-
518
- // Create the request
519
- req , err := http .NewRequest (method , url , bytes .NewBuffer (requestData ))
520
- if err != nil {
521
- return nil , err
522
- }
523
-
524
- // Initialize HeaderManager
525
- //log.Debug("Setting Authorization header with token", zap.String("Token", c.Token))
526
- headerHandler := headers .NewHeaderHandler (req , c .Logger , c .APIHandler , c .AuthTokenHandler )
527
-
528
- // Use HeaderManager to set headers
529
- headerHandler .SetContentType (contentType )
530
- headerHandler .SetRequestHeaders (endpoint )
531
- headerHandler .LogHeaders (c .clientConfig .ClientOptions .HideSensitiveData )
532
-
533
- // Execute the request
534
- resp , err := c .do (req , log , method , endpoint )
535
- if err != nil {
536
- return nil , err
537
- }
538
-
539
- // Check for successful status code
540
- if resp .StatusCode < 200 || resp .StatusCode >= 300 {
541
- // Handle error responses
542
- //return nil, c.handleErrorResponse(resp, log, "Failed to process the HTTP request", method, endpoint)
543
- return nil , c .handleErrorResponse (resp , out , log , method , endpoint )
544
- } else {
545
- // Handle successful responses
546
- return resp , c .handleSuccessResponse (resp , out , log , method , endpoint )
547
- }
548
- }
0 commit comments