@@ -16,17 +16,17 @@ import (
1616 "go.uber.org/zap"
1717)
1818
19- // Refactored contentHandler to accept io.Reader instead of []byte for streaming support .
19+ // contentHandler defines the signature for unmarshaling content from an io.Reader .
2020type contentHandler func (io.Reader , interface {}, logger.Logger , string ) error
2121
22- // Updated handlers map to use the new contentHandler signature .
23- var handlers = map [string ]contentHandler {
22+ // responseUnmarshallers maps MIME types to the corresponding contentHandler functions .
23+ var responseUnmarshallers = map [string ]contentHandler {
2424 "application/json" : unmarshalJSON ,
2525 "application/xml" : unmarshalXML ,
2626 "text/xml" : unmarshalXML ,
2727}
2828
29- // HandleAPISuccessResponse reads the response body and unmarshals it based on the content type.
29+ // HandleAPISuccessResponse reads the response body, logs the raw response details, and unmarshals the response based on the content type.
3030func HandleAPISuccessResponse (resp * http.Response , out interface {}, log logger.Logger ) error {
3131 if resp .Request .Method == "DELETE" {
3232 return handleDeleteRequest (resp , log )
@@ -38,7 +38,7 @@ func HandleAPISuccessResponse(resp *http.Response, out interface{}, log logger.L
3838 mimeType , _ := ParseContentTypeHeader (resp .Header .Get ("Content-Type" ))
3939 contentDisposition := resp .Header .Get ("Content-Disposition" )
4040
41- if handler , ok := handlers [mimeType ]; ok {
41+ if handler , ok := responseUnmarshallers [mimeType ]; ok {
4242 // Pass resp.Body directly to the handler for streaming.
4343 return handler (resp .Body , out , log , mimeType )
4444 } else if isBinaryData (mimeType , contentDisposition ) {
@@ -54,10 +54,15 @@ func HandleAPISuccessResponse(resp *http.Response, out interface{}, log logger.L
5454// handleDeleteRequest handles the special case for DELETE requests, where a successful response might not contain a body.
5555func handleDeleteRequest (resp * http.Response , log logger.Logger ) error {
5656 if resp .StatusCode >= 200 && resp .StatusCode < 300 {
57- log .Info ("Successfully processed DELETE request" , zap .String ("URL" , resp .Request .URL .String ()), zap .Int ("Status Code" , resp .StatusCode ))
57+ if log != nil {
58+ log .Info ("Successfully processed DELETE request" , zap .String ("URL" , resp .Request .URL .String ()), zap .Int ("Status Code" , resp .StatusCode ))
59+ }
5860 return nil
5961 }
60- return log .Error ("DELETE request failed" , zap .String ("URL" , resp .Request .URL .String ()), zap .Int ("Status Code" , resp .StatusCode ))
62+ if log != nil {
63+ return log .Error ("DELETE request failed" , zap .String ("URL" , resp .Request .URL .String ()), zap .Int ("Status Code" , resp .StatusCode ))
64+ }
65+ return fmt .Errorf ("DELETE request failed, status code: %d" , resp .StatusCode )
6166}
6267
6368// Adjusted logResponseDetails to handle a potential nil bodyBytes.
0 commit comments