Skip to content
Merged

Dev #51

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions httpclient/httpclient_headers_old.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package httpclient

import (
"net/http"

"github.com/deploymenttheory/go-api-http-client/logger"
"go.uber.org/zap"
)

/*
// SetRequestHeaders sets the necessary HTTP headers for a given request. It configures the Authorization,
// Content-Type, and Accept headers based on the client's current token, the content type specified by the
// caller, and the preferred response formats defined by the APIHandler's GetAcceptHeader method.
Expand Down Expand Up @@ -50,3 +44,4 @@ func (c *Client) SetRequestHeaders(req *http.Request, contentType, acceptHeader
)
}
}
*/
45 changes: 18 additions & 27 deletions httpclient/httpclient_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
}()

// Determine which set of encoding and content-type request rules to use
apiHandler := c.APIHandler
// apiHandler := c.APIHandler

// Marshal Request with correct encoding
requestData, err := apiHandler.MarshalRequest(body, method, endpoint, log)
requestData, err := c.APIHandler.MarshalRequest(body, method, endpoint, log)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -257,11 +257,6 @@ func (c *Client) executeRequest(method, endpoint string, body, out interface{},
// Construct URL using the ConstructAPIResourceEndpoint function
url := c.APIHandler.ConstructAPIResourceEndpoint(c.InstanceName, endpoint, log)

// Initialize total request counter
//c.PerfMetrics.lock.Lock()
//c.PerfMetrics.TotalRequests++
//c.PerfMetrics.lock.Unlock()

// Perform Request
req, err := http.NewRequest(method, url, bytes.NewBuffer(requestData))
if err != nil {
Expand All @@ -273,9 +268,6 @@ func (c *Client) executeRequest(method, endpoint string, body, out interface{},
headerManager.SetRequestHeaders(endpoint)
headerManager.LogHeaders(c)

// Start response time measurement
//responseTimeStart := time.Now()
// Set the context with the request ID
req = req.WithContext(ctx)

// Execute the HTTP request
Expand All @@ -284,19 +276,17 @@ func (c *Client) executeRequest(method, endpoint string, body, out interface{},
return nil, err
}

// After each request, compute and update response time
//responseDuration := time.Since(responseTimeStart)
//c.updatePerformanceMetrics(responseDuration)

// Checks for the presence of a deprecation header in the HTTP response and logs if found.
CheckDeprecationHeader(resp, log)

// Handle the response
if err := c.APIHandler.UnmarshalResponse(resp, out, log); err != nil {
return resp, err
// Check for successful status code
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
// Handle error responses
return nil, c.handleErrorResponse(resp, log, "Failed to process the HTTP request", method, endpoint)
} else {
// Handle successful responses
return resp, c.handleSuccessResponse(resp, out, log, method, endpoint)
}

return resp, nil
}

// executeHTTPRequest sends an HTTP request using the client's HTTP client. It logs the request and error details, if any,
Expand Down Expand Up @@ -428,28 +418,30 @@ func (c *Client) DoMultipartRequest(method, endpoint string, fields map[string]s
}

// Determine which set of encoding and content-type request rules to use
apiHandler := c.APIHandler
//apiHandler := c.APIHandler

// Marshal the multipart form data
requestData, contentType, err := apiHandler.MarshalMultipartRequest(fields, files, log)
requestData, contentType, err := c.APIHandler.MarshalMultipartRequest(fields, files, log)
if err != nil {
return nil, err
}

// Construct URL using the ConstructAPIResourceEndpoint function
url := apiHandler.ConstructAPIResourceEndpoint(c.InstanceName, endpoint, log)
url := c.APIHandler.ConstructAPIResourceEndpoint(c.InstanceName, endpoint, log)

// Create the request
req, err := http.NewRequest(method, url, bytes.NewBuffer(requestData))
if err != nil {
return nil, err
}

// Get Request Headers dynamically based on api handler
acceptHeader := apiHandler.GetAcceptHeader()
// Initialize HeaderManager
headerManager := NewHeaderManager(req, log, c.APIHandler, c.Token)

// Set Request Headers
c.SetRequestHeaders(req, contentType, acceptHeader, log)
// Use HeaderManager to set headers
headerManager.SetContentType(contentType)
headerManager.SetRequestHeaders(endpoint)
headerManager.LogHeaders(c)

// Execute the request
resp, err := c.executeHTTPRequest(req, log, method, endpoint)
Expand All @@ -465,5 +457,4 @@ func (c *Client) DoMultipartRequest(method, endpoint string, fields map[string]s
// Handle successful responses
return resp, c.handleSuccessResponse(resp, out, log, method, endpoint)
}
// TODO refactor to remove dependancy on func (c *Client) SetRequestHeaders
}