Skip to content

Commit b9ca2b0

Browse files
authored
Merge pull request #256 from deploymenttheory/dev-jl
Tidy Up Operations
2 parents 770e157 + 95c6219 commit b9ca2b0

File tree

5 files changed

+22
-10
lines changed

5 files changed

+22
-10
lines changed

httpclient/request.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func (c *Client) requestWithRetries(method, endpoint string, body, out interface
125125
}
126126

127127
// Success
128+
c.Sugar.Debugf("LOGHERE RETRIES STATUS CODE: %v", resp.StatusCode)
128129
if resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusBadRequest {
129130
if resp.StatusCode == http.StatusPermanentRedirect || resp.StatusCode == http.StatusTemporaryRedirect {
130131
c.Sugar.Warn("Redirect response received", zap.Int("status_code", resp.StatusCode), zap.String("location", resp.Header.Get("Location")))
@@ -217,14 +218,18 @@ func (c *Client) requestWithRetries(method, endpoint string, body, out interface
217218
func (c *Client) requestNoRetries(method, endpoint string, body, out interface{}) (*http.Response, error) {
218219
ctx := context.Background()
219220

220-
c.Sugar.Debug("Executing request without retries", zap.String("method", method), zap.String("endpoint", endpoint))
221+
c.Sugar.Debugw("Executing request without retries", "method", method, "endpoint", endpoint)
221222

222223
resp, err := c.request(ctx, method, endpoint, body)
223224
if err != nil {
224225
return nil, err
225226
}
226227

228+
c.Sugar.Debug("LOGHERE")
229+
c.Sugar.Debugf("Status Code: %v", resp.StatusCode)
230+
227231
if resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusBadRequest {
232+
c.Sugar.Debug("FLAG 1 - Request Considered Successful")
228233
if resp.StatusCode == http.StatusPermanentRedirect || resp.StatusCode == http.StatusTemporaryRedirect {
229234
c.Sugar.Warn("Redirect response received", zap.Int("status_code", resp.StatusCode), zap.String("location", resp.Header.Get("Location")))
230235
}
@@ -233,6 +238,8 @@ func (c *Client) requestNoRetries(method, endpoint string, body, out interface{}
233238
return resp, response.HandleAPISuccessResponse(resp, out, c.Sugar)
234239
}
235240

241+
c.Sugar.Debug("FLAG 2 - Request Considered NOT Successful")
242+
236243
return nil, response.HandleAPIErrorResponse(resp, c.Sugar)
237244
}
238245

response/error.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func HandleAPIErrorResponse(resp *http.Response, sugar *zap.SugaredLogger) *APIE
5454
return apiError
5555
}
5656

57-
mimeType, _ := parseDispositionHeader(resp.Header.Get("Content-Type"))
57+
mimeType, _ := parseHeader(resp.Header.Get("Content-Type"))
5858
switch mimeType {
5959
case "application/json":
6060
parseJSONResponse(bodyBytes, apiError)

response/parse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import "strings"
66
// parseHeader generalizes the parsing of headers like Content-Type and Content-Disposition.
77
// It extracts the main value (e.g., MIME type for Content-Type) and any parameters (like charset).
88
// TODO we need to talk about what this does.
9-
func parseDispositionHeader(header string) (string, map[string]string) {
9+
func parseHeader(header string) (string, map[string]string) {
1010
parts := strings.SplitN(header, ";", 2)
1111
mainValue := strings.TrimSpace(parts[0])
1212

response/success.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func HandleAPISuccessResponse(resp *http.Response, out interface{}, sugar *zap.S
3939
}
4040

4141
// TODO do we need to redact some auth headers here? I think so.
42-
sugar.Debug("HTTP Response Headers", zap.Any("Headers", resp.Header))
43-
sugar.Debug("Raw HTTP Response", zap.String("Body", string(bodyBytes)))
42+
sugar.Debugw("HTTP Response Headers", zap.Any("Headers", resp.Header))
43+
sugar.Debugw("Raw HTTP Response", zap.String("Body", string(bodyBytes)))
4444

4545
bodyReader := bytes.NewReader(bodyBytes)
4646
contentType := resp.Header.Get("Content-Type")
@@ -49,7 +49,12 @@ func HandleAPISuccessResponse(resp *http.Response, out interface{}, sugar *zap.S
4949
var handler contentHandler
5050
var ok bool
5151

52-
if handler, ok = responseUnmarshallers[contentType]; ok {
52+
sugar.Debug("LOGHERE-HandleApiSuccessResponse")
53+
sugar.Debugw("Headers:", "content-type", contentType, "content-disposition", contentDisposition)
54+
55+
contentTypeNoParams, _ := parseHeader(contentType)
56+
57+
if handler, ok = responseUnmarshallers[contentTypeNoParams]; ok {
5358
return handler(bodyReader, out, sugar, contentType)
5459
}
5560

@@ -58,7 +63,7 @@ func HandleAPISuccessResponse(resp *http.Response, out interface{}, sugar *zap.S
5863
}
5964

6065
errMsg := fmt.Sprintf("unexpected MIME type: %s", contentType)
61-
sugar.Error("Unmarshal error", zap.String("content type", contentType), zap.Error(errors.New(errMsg)))
66+
sugar.Errorw("Unmarshal error", zap.String("content type", contentType), zap.Error(errors.New(errMsg)))
6267
return errors.New(errMsg)
6368

6469
}
@@ -122,7 +127,7 @@ func handleBinaryData(reader io.Reader, sugar *zap.SugaredLogger, out interface{
122127
}
123128

124129
if contentDisposition != "" {
125-
_, params := parseDispositionHeader(contentDisposition)
130+
_, params := parseHeader(contentDisposition)
126131
if filename, ok := params["filename"]; ok {
127132
sugar.Debug("Extracted filename from Content-Disposition", zap.String("filename", filename))
128133
}

response/t_parse_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestParseContentTypeHeader(t *testing.T) {
3535

3636
for _, tt := range tests {
3737
t.Run(tt.name, func(t *testing.T) {
38-
gotType, gotParams := parseDispositionHeader(tt.header)
38+
gotType, gotParams := parseHeader(tt.header)
3939
if gotType != tt.wantType {
4040
t.Errorf("ParseContentTypeHeader() gotType = %v, want %v", gotType, tt.wantType)
4141
}
@@ -70,7 +70,7 @@ func TestParseContentDisposition(t *testing.T) {
7070

7171
for _, tt := range tests {
7272
t.Run(tt.name, func(t *testing.T) {
73-
gotType, gotParams := parseDispositionHeader(tt.header)
73+
gotType, gotParams := parseHeader(tt.header)
7474
if gotType != tt.wantType {
7575
t.Errorf("ParseContentDisposition() gotType = %v, want %v", gotType, tt.wantType)
7676
}

0 commit comments

Comments
 (0)