Skip to content

Commit a91aad7

Browse files
committed
optimising the responsep package
1 parent c8c9707 commit a91aad7

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

httpclient/request.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ func (c *Client) requestWithRetries(method, endpoint string, body, out interface
130130
c.Sugar.Warn("Redirect response received", zap.Int("status_code", resp.StatusCode), zap.String("location", resp.Header.Get("Location")))
131131
}
132132
c.Sugar.Infof("%s request successful at %v", resp.Request.Method, resp.Request.URL)
133+
133134
return resp, response.HandleAPISuccessResponse(resp, out, c.Sugar)
134135
}
135136

@@ -142,6 +143,7 @@ func (c *Client) requestWithRetries(method, endpoint string, body, out interface
142143
// Non Retry
143144
if response.IsNonRetryableStatusCode(resp.StatusCode) {
144145
c.Sugar.Warn("Non-retryable error received", zap.Int("status_code", resp.StatusCode), zap.String("status_message", statusMessage))
146+
145147
return resp, response.HandleAPIErrorResponse(resp, c.Sugar)
146148
}
147149

@@ -227,6 +229,7 @@ func (c *Client) requestNoRetries(method, endpoint string, body, out interface{}
227229
c.Sugar.Warn("Redirect response received", zap.Int("status_code", resp.StatusCode), zap.String("location", resp.Header.Get("Location")))
228230
}
229231
c.Sugar.Infof("%s request successful at %v", resp.Request.Method, resp.Request.URL)
232+
230233
return resp, response.HandleAPISuccessResponse(resp, out, c.Sugar)
231234
}
232235

response/success.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,31 @@ func HandleAPISuccessResponse(resp *http.Response, out interface{}, sugar *zap.S
3838
return err
3939
}
4040

41+
// TODO do we need to redact some auth headers here? I think so.
4142
sugar.Debug("HTTP Response Headers", zap.Any("Headers", resp.Header))
4243
sugar.Debug("Raw HTTP Response", zap.String("Body", string(bodyBytes)))
4344

4445
bodyReader := bytes.NewReader(bodyBytes)
4546
mimeType, _ := parseHeader(resp.Header.Get("Content-Type"))
4647
contentDisposition := resp.Header.Get("Content-Disposition")
4748

48-
if handler, ok := responseUnmarshallers[mimeType]; ok {
49+
sugar.Debugf("MIMETYPE-%s", mimeType)
50+
51+
var handler contentHandler
52+
var ok bool
53+
54+
if handler, ok = responseUnmarshallers[mimeType]; ok {
4955
return handler(bodyReader, out, sugar, mimeType)
50-
} else if isBinaryData(mimeType, contentDisposition) {
56+
}
57+
58+
if isBinaryData(mimeType, contentDisposition) {
5159
return handleBinaryData(bodyReader, sugar, out, contentDisposition)
52-
} else {
53-
errMsg := fmt.Sprintf("unexpected MIME type: %s", mimeType)
54-
sugar.Error("Unmarshal error", zap.String("content type", mimeType), zap.Error(errors.New(errMsg)))
55-
return errors.New(errMsg)
5660
}
61+
62+
errMsg := fmt.Sprintf("unexpected MIME type: %s", mimeType)
63+
sugar.Error("Unmarshal error", zap.String("content type", mimeType), zap.Error(errors.New(errMsg)))
64+
return errors.New(errMsg)
65+
5766
}
5867

5968
// handleDeleteRequest handles the special case for DELETE requests, where a successful response might not contain a body.
@@ -94,10 +103,8 @@ func isBinaryData(contentType, contentDisposition string) bool {
94103

95104
// handleBinaryData reads binary data from an io.Reader and stores it in *[]byte or streams it to an io.Writer.
96105
func handleBinaryData(reader io.Reader, sugar *zap.SugaredLogger, out interface{}, contentDisposition string) error {
97-
// Check if the output interface is either *[]byte or io.Writer
98106
switch out := out.(type) {
99107
case *[]byte:
100-
// Read all data from reader and store it in *[]byte
101108
data, err := io.ReadAll(reader)
102109
if err != nil {
103110
sugar.Error("Failed to read binary data", zap.Error(err))
@@ -106,7 +113,6 @@ func handleBinaryData(reader io.Reader, sugar *zap.SugaredLogger, out interface{
106113
*out = data
107114

108115
case io.Writer:
109-
// Stream data directly to the io.Writer
110116
_, err := io.Copy(out, reader)
111117
if err != nil {
112118
sugar.Error("Failed to stream binary data to io.Writer", zap.Error(err))
@@ -117,12 +123,10 @@ func handleBinaryData(reader io.Reader, sugar *zap.SugaredLogger, out interface{
117123
return errors.New("output parameter is not suitable for binary data (*[]byte or io.Writer)")
118124
}
119125

120-
// Handle Content-Disposition if present
121126
if contentDisposition != "" {
122127
_, params := parseHeader(contentDisposition)
123128
if filename, ok := params["filename"]; ok {
124129
sugar.Debug("Extracted filename from Content-Disposition", zap.String("filename", filename))
125-
// Additional processing for the filename can be done here if needed
126130
}
127131
}
128132

0 commit comments

Comments
 (0)