From dca16145c82a39de5c609af19203166ce36fce13 Mon Sep 17 00:00:00 2001 From: Joseph Little Date: Tue, 9 Jul 2024 16:26:50 +0100 Subject: [PATCH] simplification --- response/error.go | 2 +- response/parse.go | 3 ++- response/success.go | 17 +++++++---------- response/t_parse_test.go | 4 ++-- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/response/error.go b/response/error.go index 15368ea..50e25fe 100644 --- a/response/error.go +++ b/response/error.go @@ -54,7 +54,7 @@ func HandleAPIErrorResponse(resp *http.Response, sugar *zap.SugaredLogger) *APIE return apiError } - mimeType, _ := parseHeader(resp.Header.Get("Content-Type")) + mimeType, _ := parseDispositionHeader(resp.Header.Get("Content-Type")) switch mimeType { case "application/json": parseJSONResponse(bodyBytes, apiError) diff --git a/response/parse.go b/response/parse.go index b772241..8e6f644 100644 --- a/response/parse.go +++ b/response/parse.go @@ -5,7 +5,8 @@ import "strings" // parseHeader generalizes the parsing of headers like Content-Type and Content-Disposition. // It extracts the main value (e.g., MIME type for Content-Type) and any parameters (like charset). -func parseHeader(header string) (string, map[string]string) { +// TODO we need to talk about what this does. +func parseDispositionHeader(header string) (string, map[string]string) { parts := strings.SplitN(header, ";", 2) mainValue := strings.TrimSpace(parts[0]) diff --git a/response/success.go b/response/success.go index deb1851..d92aeed 100644 --- a/response/success.go +++ b/response/success.go @@ -43,25 +43,22 @@ func HandleAPISuccessResponse(resp *http.Response, out interface{}, sugar *zap.S sugar.Debug("Raw HTTP Response", zap.String("Body", string(bodyBytes))) bodyReader := bytes.NewReader(bodyBytes) - mimeType, _ := parseHeader(resp.Header.Get("Content-Type")) + contentType := resp.Header.Get("Content-Type") contentDisposition := resp.Header.Get("Content-Disposition") - sugar.Debugf("MIMETYPE: %s", mimeType) - sugar.Debugf("NORMAL-HEADER: %s", resp.Header.Get("Content-Type")) - var handler contentHandler var ok bool - if handler, ok = responseUnmarshallers[mimeType]; ok { - return handler(bodyReader, out, sugar, mimeType) + if handler, ok = responseUnmarshallers[contentType]; ok { + return handler(bodyReader, out, sugar, contentType) } - if isBinaryData(mimeType, contentDisposition) { + if isBinaryData(contentType, contentDisposition) { return handleBinaryData(bodyReader, sugar, out, contentDisposition) } - errMsg := fmt.Sprintf("unexpected MIME type: %s", mimeType) - sugar.Error("Unmarshal error", zap.String("content type", mimeType), zap.Error(errors.New(errMsg))) + errMsg := fmt.Sprintf("unexpected MIME type: %s", contentType) + sugar.Error("Unmarshal error", zap.String("content type", contentType), zap.Error(errors.New(errMsg))) return errors.New(errMsg) } @@ -125,7 +122,7 @@ func handleBinaryData(reader io.Reader, sugar *zap.SugaredLogger, out interface{ } if contentDisposition != "" { - _, params := parseHeader(contentDisposition) + _, params := parseDispositionHeader(contentDisposition) if filename, ok := params["filename"]; ok { sugar.Debug("Extracted filename from Content-Disposition", zap.String("filename", filename)) } diff --git a/response/t_parse_test.go b/response/t_parse_test.go index 8cc2ae7..332720c 100644 --- a/response/t_parse_test.go +++ b/response/t_parse_test.go @@ -35,7 +35,7 @@ func TestParseContentTypeHeader(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - gotType, gotParams := parseHeader(tt.header) + gotType, gotParams := parseDispositionHeader(tt.header) if gotType != tt.wantType { t.Errorf("ParseContentTypeHeader() gotType = %v, want %v", gotType, tt.wantType) } @@ -70,7 +70,7 @@ func TestParseContentDisposition(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - gotType, gotParams := parseHeader(tt.header) + gotType, gotParams := parseDispositionHeader(tt.header) if gotType != tt.wantType { t.Errorf("ParseContentDisposition() gotType = %v, want %v", gotType, tt.wantType) }