88 "time"
99
1010 "github.com/deploymenttheory/go-api-http-client/authenticationhandler"
11- "github.com/deploymenttheory/go-api-http-client/cookiejar"
1211 "github.com/deploymenttheory/go-api-http-client/headers"
1312 "github.com/deploymenttheory/go-api-http-client/httpmethod"
1413 "github.com/deploymenttheory/go-api-http-client/logger"
@@ -154,7 +153,7 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
154153 c .ConcurrencyHandler .Metrics .TotalRequests ++
155154 c .ConcurrencyHandler .Metrics .Lock .Unlock ()
156155
157- // Perform Request
156+ // Create a new HTTP request with the provided method, URL, and body
158157 req , err := http .NewRequest (method , url , bytes .NewBuffer (requestData ))
159158 if err != nil {
160159 return nil , err
@@ -315,7 +314,7 @@ func (c *Client) executeRequest(method, endpoint string, body, out interface{})
315314 // Construct URL using the ConstructAPIResourceEndpoint function
316315 url := c .APIHandler .ConstructAPIResourceEndpoint (c .InstanceName , endpoint , log )
317316
318- // Perform Request
317+ // Create a new HTTP request with the provided method, URL, and body
319318 req , err := http .NewRequest (method , url , bytes .NewBuffer (requestData ))
320319 if err != nil {
321320 return nil , err
@@ -337,11 +336,8 @@ func (c *Client) executeRequest(method, endpoint string, body, out interface{})
337336 return nil , err
338337 }
339338
340- // Get Cookies
341- cookiejar .GetCookies (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
342- // Log outgoing cookies
343- log .LogCookies ("incoming" , r , method , endpoint )
344- }), c .Logger ).ServeHTTP (nil , req )
339+ // Log outgoing cookies
340+ log .LogCookies ("incoming" , req , method , endpoint )
345341
346342 // Checks for the presence of a deprecation header in the HTTP response and logs if found.
347343 headers .CheckDeprecationHeader (resp , log )
@@ -457,96 +453,3 @@ func (c *Client) handleSuccessResponse(resp *http.Response, out interface{}, log
457453 )
458454 return nil
459455}
460-
461- // DoMultipartRequest creates and executes a multipart HTTP request. It is used for sending files
462- // and form fields in a single request. This method handles the construction of the multipart
463- // message body, setting the appropriate headers, and sending the request to the given endpoint.
464- //
465- // Parameters:
466- // - method: The HTTP method to use (e.g., POST, PUT).
467- // - endpoint: The API endpoint to which the request will be sent.
468- // - fields: A map of form fields and their values to include in the multipart message.
469- // - files: A map of file field names to file paths that will be included as file attachments.
470- // - out: A pointer to a variable where the unmarshaled response will be stored.
471- //
472- // Returns:
473- // - A pointer to the http.Response received from the server.
474- // - An error if the request could not be sent or the response could not be processed.
475- //
476- // The function first validates the authentication token, then constructs the multipart
477- // request body based on the provided fields and files. It then constructs the full URL for
478- // the request, sets the required headers (including Authorization and Content-Type), and
479- // sends the request.
480- //
481- // If debug mode is enabled, the function logs all the request headers before sending the request.
482- // After the request is sent, the function checks the response status code. If the response is
483- // not within the success range (200-299), it logs an error and returns the response and an error.
484- // If the response is successful, it attempts to unmarshal the response body into the 'out' parameter.
485- //
486- // Note:
487- // The caller should handle closing the response body when successful.
488- func (c * Client ) DoMultipartRequest (method , endpoint string , fields map [string ]string , files map [string ]string , out interface {}) (* http.Response , error ) {
489- log := c .Logger
490-
491- // Auth Token validation check
492- // valid, err := c.ValidAuthTokenCheck()
493- // if err != nil || !valid {
494- // return nil, err
495- //}
496-
497- // Auth Token validation check
498- clientCredentials := authenticationhandler.ClientCredentials {
499- Username : c .clientConfig .Auth .Username ,
500- Password : c .clientConfig .Auth .Password ,
501- ClientID : c .clientConfig .Auth .ClientID ,
502- ClientSecret : c .clientConfig .Auth .ClientSecret ,
503- }
504-
505- valid , err := c .AuthTokenHandler .ValidAuthTokenCheck (c .APIHandler , c .httpClient , clientCredentials , c .clientConfig .ClientOptions .TokenRefreshBufferPeriod )
506- if err != nil || ! valid {
507- return nil , err
508- }
509-
510- // Determine which set of encoding and content-type request rules to use
511- //apiHandler := c.APIHandler
512-
513- // Marshal the multipart form data
514- requestData , contentType , err := c .APIHandler .MarshalMultipartRequest (fields , files , log )
515- if err != nil {
516- return nil , err
517- }
518-
519- // Construct URL using the ConstructAPIResourceEndpoint function
520- url := c .APIHandler .ConstructAPIResourceEndpoint (c .InstanceName , endpoint , log )
521-
522- // Create the request
523- req , err := http .NewRequest (method , url , bytes .NewBuffer (requestData ))
524- if err != nil {
525- return nil , err
526- }
527-
528- // Initialize HeaderManager
529- //log.Debug("Setting Authorization header with token", zap.String("Token", c.Token))
530- headerHandler := headers .NewHeaderHandler (req , c .Logger , c .APIHandler , c .AuthTokenHandler )
531-
532- // Use HeaderManager to set headers
533- headerHandler .SetContentType (contentType )
534- headerHandler .SetRequestHeaders (endpoint )
535- headerHandler .LogHeaders (c .clientConfig .ClientOptions .HideSensitiveData )
536-
537- // Execute the request
538- resp , err := c .do (req , log , method , endpoint )
539- if err != nil {
540- return nil , err
541- }
542-
543- // Check for successful status code
544- if resp .StatusCode < 200 || resp .StatusCode >= 300 {
545- // Handle error responses
546- //return nil, c.handleErrorResponse(resp, log, "Failed to process the HTTP request", method, endpoint)
547- return nil , c .handleErrorResponse (resp , out , log , method , endpoint )
548- } else {
549- // Handle successful responses
550- return resp , c .handleSuccessResponse (resp , out , log , method , endpoint )
551- }
552- }
0 commit comments