@@ -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.
96105func 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