Skip to content

Commit

Permalink
simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoeker12 committed Jul 9, 2024
1 parent 1e6e504 commit dca1614
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion response/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion response/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down
17 changes: 7 additions & 10 deletions response/success.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
Expand Down Expand Up @@ -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))
}
Expand Down
4 changes: 2 additions & 2 deletions response/t_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit dca1614

Please sign in to comment.