Skip to content

Commit dca1614

Browse files
committed
simplification
1 parent 1e6e504 commit dca1614

File tree

4 files changed

+12
-14
lines changed

4 files changed

+12
-14
lines changed

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

response/parse.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import "strings"
55

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).
8-
func parseHeader(header string) (string, map[string]string) {
8+
// TODO we need to talk about what this does.
9+
func parseDispositionHeader(header string) (string, map[string]string) {
910
parts := strings.SplitN(header, ";", 2)
1011
mainValue := strings.TrimSpace(parts[0])
1112

response/success.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,22 @@ func HandleAPISuccessResponse(resp *http.Response, out interface{}, sugar *zap.S
4343
sugar.Debug("Raw HTTP Response", zap.String("Body", string(bodyBytes)))
4444

4545
bodyReader := bytes.NewReader(bodyBytes)
46-
mimeType, _ := parseHeader(resp.Header.Get("Content-Type"))
46+
contentType := resp.Header.Get("Content-Type")
4747
contentDisposition := resp.Header.Get("Content-Disposition")
4848

49-
sugar.Debugf("MIMETYPE: %s", mimeType)
50-
sugar.Debugf("NORMAL-HEADER: %s", resp.Header.Get("Content-Type"))
51-
5249
var handler contentHandler
5350
var ok bool
5451

55-
if handler, ok = responseUnmarshallers[mimeType]; ok {
56-
return handler(bodyReader, out, sugar, mimeType)
52+
if handler, ok = responseUnmarshallers[contentType]; ok {
53+
return handler(bodyReader, out, sugar, contentType)
5754
}
5855

59-
if isBinaryData(mimeType, contentDisposition) {
56+
if isBinaryData(contentType, contentDisposition) {
6057
return handleBinaryData(bodyReader, sugar, out, contentDisposition)
6158
}
6259

63-
errMsg := fmt.Sprintf("unexpected MIME type: %s", mimeType)
64-
sugar.Error("Unmarshal error", zap.String("content type", mimeType), zap.Error(errors.New(errMsg)))
60+
errMsg := fmt.Sprintf("unexpected MIME type: %s", contentType)
61+
sugar.Error("Unmarshal error", zap.String("content type", contentType), zap.Error(errors.New(errMsg)))
6562
return errors.New(errMsg)
6663

6764
}
@@ -125,7 +122,7 @@ func handleBinaryData(reader io.Reader, sugar *zap.SugaredLogger, out interface{
125122
}
126123

127124
if contentDisposition != "" {
128-
_, params := parseHeader(contentDisposition)
125+
_, params := parseDispositionHeader(contentDisposition)
129126
if filename, ok := params["filename"]; ok {
130127
sugar.Debug("Extracted filename from Content-Disposition", zap.String("filename", filename))
131128
}

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 := parseHeader(tt.header)
38+
gotType, gotParams := parseDispositionHeader(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 := parseHeader(tt.header)
73+
gotType, gotParams := parseDispositionHeader(tt.header)
7474
if gotType != tt.wantType {
7575
t.Errorf("ParseContentDisposition() gotType = %v, want %v", gotType, tt.wantType)
7676
}

0 commit comments

Comments
 (0)