Skip to content

Commit acd1c53

Browse files
authored
Merge pull request #51 from deploymenttheory/dev
Dev
2 parents 9a4108a + d83706d commit acd1c53

File tree

2 files changed

+20
-34
lines changed

2 files changed

+20
-34
lines changed

httpclient/httpclient_headers_old.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
package httpclient
22

3-
import (
4-
"net/http"
5-
6-
"github.com/deploymenttheory/go-api-http-client/logger"
7-
"go.uber.org/zap"
8-
)
9-
3+
/*
104
// SetRequestHeaders sets the necessary HTTP headers for a given request. It configures the Authorization,
115
// Content-Type, and Accept headers based on the client's current token, the content type specified by the
126
// caller, and the preferred response formats defined by the APIHandler's GetAcceptHeader method.
@@ -50,3 +44,4 @@ func (c *Client) SetRequestHeaders(req *http.Request, contentType, acceptHeader
5044
)
5145
}
5246
}
47+
*/

httpclient/httpclient_request.go

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
125125
}()
126126

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

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

260-
// Initialize total request counter
261-
//c.PerfMetrics.lock.Lock()
262-
//c.PerfMetrics.TotalRequests++
263-
//c.PerfMetrics.lock.Unlock()
264-
265260
// Perform Request
266261
req, err := http.NewRequest(method, url, bytes.NewBuffer(requestData))
267262
if err != nil {
@@ -273,9 +268,6 @@ func (c *Client) executeRequest(method, endpoint string, body, out interface{},
273268
headerManager.SetRequestHeaders(endpoint)
274269
headerManager.LogHeaders(c)
275270

276-
// Start response time measurement
277-
//responseTimeStart := time.Now()
278-
// Set the context with the request ID
279271
req = req.WithContext(ctx)
280272

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

287-
// After each request, compute and update response time
288-
//responseDuration := time.Since(responseTimeStart)
289-
//c.updatePerformanceMetrics(responseDuration)
290-
291279
// Checks for the presence of a deprecation header in the HTTP response and logs if found.
292280
CheckDeprecationHeader(resp, log)
293281

294-
// Handle the response
295-
if err := c.APIHandler.UnmarshalResponse(resp, out, log); err != nil {
296-
return resp, err
282+
// Check for successful status code
283+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
284+
// Handle error responses
285+
return nil, c.handleErrorResponse(resp, log, "Failed to process the HTTP request", method, endpoint)
286+
} else {
287+
// Handle successful responses
288+
return resp, c.handleSuccessResponse(resp, out, log, method, endpoint)
297289
}
298-
299-
return resp, nil
300290
}
301291

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

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

433423
// Marshal the multipart form data
434-
requestData, contentType, err := apiHandler.MarshalMultipartRequest(fields, files, log)
424+
requestData, contentType, err := c.APIHandler.MarshalMultipartRequest(fields, files, log)
435425
if err != nil {
436426
return nil, err
437427
}
438428

439429
// Construct URL using the ConstructAPIResourceEndpoint function
440-
url := apiHandler.ConstructAPIResourceEndpoint(c.InstanceName, endpoint, log)
430+
url := c.APIHandler.ConstructAPIResourceEndpoint(c.InstanceName, endpoint, log)
441431

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

448-
// Get Request Headers dynamically based on api handler
449-
acceptHeader := apiHandler.GetAcceptHeader()
438+
// Initialize HeaderManager
439+
headerManager := NewHeaderManager(req, log, c.APIHandler, c.Token)
450440

451-
// Set Request Headers
452-
c.SetRequestHeaders(req, contentType, acceptHeader, log)
441+
// Use HeaderManager to set headers
442+
headerManager.SetContentType(contentType)
443+
headerManager.SetRequestHeaders(endpoint)
444+
headerManager.LogHeaders(c)
453445

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

0 commit comments

Comments
 (0)