@@ -16,17 +16,17 @@ import (
16
16
"go.uber.org/zap"
17
17
)
18
18
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 .
20
20
type contentHandler func (io.Reader , interface {}, logger.Logger , string ) error
21
21
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 {
24
24
"application/json" : unmarshalJSON ,
25
25
"application/xml" : unmarshalXML ,
26
26
"text/xml" : unmarshalXML ,
27
27
}
28
28
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.
30
30
func HandleAPISuccessResponse (resp * http.Response , out interface {}, log logger.Logger ) error {
31
31
if resp .Request .Method == "DELETE" {
32
32
return handleDeleteRequest (resp , log )
@@ -38,7 +38,7 @@ func HandleAPISuccessResponse(resp *http.Response, out interface{}, log logger.L
38
38
mimeType , _ := ParseContentTypeHeader (resp .Header .Get ("Content-Type" ))
39
39
contentDisposition := resp .Header .Get ("Content-Disposition" )
40
40
41
- if handler , ok := handlers [mimeType ]; ok {
41
+ if handler , ok := responseUnmarshallers [mimeType ]; ok {
42
42
// Pass resp.Body directly to the handler for streaming.
43
43
return handler (resp .Body , out , log , mimeType )
44
44
} else if isBinaryData (mimeType , contentDisposition ) {
@@ -54,10 +54,15 @@ func HandleAPISuccessResponse(resp *http.Response, out interface{}, log logger.L
54
54
// handleDeleteRequest handles the special case for DELETE requests, where a successful response might not contain a body.
55
55
func handleDeleteRequest (resp * http.Response , log logger.Logger ) error {
56
56
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
+ }
58
60
return nil
59
61
}
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 )
61
66
}
62
67
63
68
// Adjusted logResponseDetails to handle a potential nil bodyBytes.
0 commit comments