Skip to content

Commit 9247c8a

Browse files
committed
Remove unused import and update comments in httpclient/request.go
1 parent e8b933b commit 9247c8a

File tree

2 files changed

+98
-101
lines changed

2 files changed

+98
-101
lines changed

httpclient/multipartrequest.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// httprequest/multipartrequest.go
2+
package httpclient
3+
4+
import (
5+
"bytes"
6+
"net/http"
7+
8+
"github.com/deploymenttheory/go-api-http-client/authenticationhandler"
9+
"github.com/deploymenttheory/go-api-http-client/headers"
10+
)
11+
12+
// DoMultipartRequest creates and executes a multipart HTTP request. It is used for sending files
13+
// and form fields in a single request. This method handles the construction of the multipart
14+
// message body, setting the appropriate headers, and sending the request to the given endpoint.
15+
//
16+
// Parameters:
17+
// - method: The HTTP method to use (e.g., POST, PUT).
18+
// - endpoint: The API endpoint to which the request will be sent.
19+
// - fields: A map of form fields and their values to include in the multipart message.
20+
// - files: A map of file field names to file paths that will be included as file attachments.
21+
// - out: A pointer to a variable where the unmarshaled response will be stored.
22+
//
23+
// Returns:
24+
// - A pointer to the http.Response received from the server.
25+
// - An error if the request could not be sent or the response could not be processed.
26+
//
27+
// The function first validates the authentication token, then constructs the multipart
28+
// request body based on the provided fields and files. It then constructs the full URL for
29+
// the request, sets the required headers (including Authorization and Content-Type), and
30+
// sends the request.
31+
//
32+
// If debug mode is enabled, the function logs all the request headers before sending the request.
33+
// After the request is sent, the function checks the response status code. If the response is
34+
// not within the success range (200-299), it logs an error and returns the response and an error.
35+
// If the response is successful, it attempts to unmarshal the response body into the 'out' parameter.
36+
//
37+
// Note:
38+
// The caller should handle closing the response body when successful.
39+
func (c *Client) DoMultipartRequest(method, endpoint string, fields map[string]string, files map[string]string, out interface{}) (*http.Response, error) {
40+
log := c.Logger
41+
42+
// Auth Token validation check
43+
clientCredentials := authenticationhandler.ClientCredentials{
44+
Username: c.clientConfig.Auth.Username,
45+
Password: c.clientConfig.Auth.Password,
46+
ClientID: c.clientConfig.Auth.ClientID,
47+
ClientSecret: c.clientConfig.Auth.ClientSecret,
48+
}
49+
50+
valid, err := c.AuthTokenHandler.ValidAuthTokenCheck(c.APIHandler, c.httpClient, clientCredentials, c.clientConfig.ClientOptions.TokenRefreshBufferPeriod)
51+
if err != nil || !valid {
52+
return nil, err
53+
}
54+
55+
// Marshal the multipart form data
56+
requestData, contentType, err := c.APIHandler.MarshalMultipartRequest(fields, files, log)
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
// Construct URL using the ConstructAPIResourceEndpoint function
62+
url := c.APIHandler.ConstructAPIResourceEndpoint(c.InstanceName, endpoint, log)
63+
64+
// Create the request
65+
req, err := http.NewRequest(method, url, bytes.NewBuffer(requestData))
66+
if err != nil {
67+
return nil, err
68+
}
69+
70+
// Initialize HeaderManager
71+
//log.Debug("Setting Authorization header with token", zap.String("Token", c.Token))
72+
headerHandler := headers.NewHeaderHandler(req, c.Logger, c.APIHandler, c.AuthTokenHandler)
73+
74+
// Use HeaderManager to set headers
75+
headerHandler.SetContentType(contentType)
76+
headerHandler.SetRequestHeaders(endpoint)
77+
headerHandler.LogHeaders(c.clientConfig.ClientOptions.HideSensitiveData)
78+
79+
// Execute the request
80+
resp, err := c.do(req, log, method, endpoint)
81+
if err != nil {
82+
return nil, err
83+
}
84+
85+
// Check for successful status code
86+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
87+
// Handle error responses
88+
//return nil, c.handleErrorResponse(resp, log, "Failed to process the HTTP request", method, endpoint)
89+
return nil, c.handleErrorResponse(resp, out, log, method, endpoint)
90+
} else {
91+
// Handle successful responses
92+
return resp, c.handleSuccessResponse(resp, out, log, method, endpoint)
93+
}
94+
}

httpclient/request.go

Lines changed: 4 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
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

Comments
 (0)