@@ -100,33 +100,37 @@ func logRequestAndResponse(req *http.Request, resp *http.Response) {
100100 log .Printf ("[DEBUG] Request: [Method=%s] [URL=%s] [Headers=%s]. Response: [Status=%s]\n " , req .Method , req .URL , maskedHeader , resp .Status )
101101}
102102
103- func (s * Client ) Post (urlPath string , payload interface {}) ([]byte , error ) {
104- body , err := json .Marshal (payload )
103+ func (s * Client ) handleSumoResponse (resp * http.Response ) ([]byte , error ) {
104+ d , err := io .ReadAll (resp .Body )
105+ defer resp .Body .Close ()
105106 if err != nil {
106107 return nil , err
107108 }
108109
109- req , err := s .createSumoRequest (http .MethodPost , urlPath , bytes .NewBuffer (body ))
110- if err != nil {
111- return nil , err
110+ if resp .StatusCode >= 400 {
111+ return nil , errors .New (string (d ))
112112 }
113113
114- resp , err := s .doSumoRequest (req )
114+ return d , nil
115+ }
116+
117+ func (s * Client ) Post (urlPath string , payload interface {}) ([]byte , error ) {
118+ body , err := json .Marshal (payload )
115119 if err != nil {
116120 return nil , err
117121 }
118122
119- d , err := io .ReadAll (resp .Body )
120- defer resp .Body .Close ()
123+ req , err := s .createSumoRequest (http .MethodPost , urlPath , bytes .NewBuffer (body ))
121124 if err != nil {
122125 return nil , err
123126 }
124127
125- if resp .StatusCode >= 400 {
126- return nil , errors .New (string (d ))
128+ resp , err := s .doSumoRequest (req )
129+ if err != nil {
130+ return nil , err
127131 }
128132
129- return d , nil
133+ return s . handleSumoResponse ( resp )
130134}
131135
132136func (s * Client ) PostRawPayload (urlPath string , payload string ) ([]byte , error ) {
@@ -140,17 +144,7 @@ func (s *Client) PostRawPayload(urlPath string, payload string) ([]byte, error)
140144 return nil , err
141145 }
142146
143- d , err := io .ReadAll (resp .Body )
144- defer resp .Body .Close ()
145- if err != nil {
146- return nil , err
147- }
148-
149- if resp .StatusCode >= 400 {
150- return nil , errors .New (string (d ))
151- }
152-
153- return d , nil
147+ return s .handleSumoResponse (resp )
154148}
155149
156150func (s * Client ) Put (urlPath string , payload interface {}) ([]byte , error ) {
@@ -172,17 +166,7 @@ func (s *Client) Put(urlPath string, payload interface{}) ([]byte, error) {
172166 return nil , err
173167 }
174168
175- d , err := io .ReadAll (resp .Body )
176- defer resp .Body .Close ()
177- if err != nil {
178- return nil , err
179- }
180-
181- if resp .StatusCode >= 400 {
182- return nil , errors .New (string (d ))
183- }
184-
185- return d , nil
169+ return s .handleSumoResponse (resp )
186170}
187171
188172func (s * Client ) Get (urlPath string ) ([]byte , string , error ) {
@@ -244,17 +228,7 @@ func (s *Client) Delete(urlPath string) ([]byte, error) {
244228 return nil , err
245229 }
246230
247- d , err := io .ReadAll (resp .Body )
248- defer resp .Body .Close ()
249- if err != nil {
250- return nil , err
251- }
252-
253- if resp .StatusCode >= 400 {
254- return nil , errors .New (string (d ))
255- }
256-
257- return d , nil
231+ return s .handleSumoResponse (resp )
258232}
259233
260234func ErrorHandler (resp * http.Response , err error , numTries int ) (* http.Response , error ) {
0 commit comments