modified the way HTTPCaller store last resposne

This commit is contained in:
pb 2025-03-12 12:00:32 +01:00
parent 363ac94c47
commit b81c60a3ce

View File

@ -3,6 +3,7 @@ package tools
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
@ -49,9 +50,9 @@ func ToMethod(str string) METHOD {
var HTTPCallerInstance = &HTTPCaller{} // Singleton instance of the HTTPCaller
type HTTPCaller struct {
URLS map[DataType]map[METHOD]string // Map of the different methods and their urls
Disabled bool // Disabled flag
LastResults *http.Response // Used to store information regarding the last execution of a given method on a given data type
URLS map[DataType]map[METHOD]string // Map of the different methods and their urls
Disabled bool // Disabled flag
LastResults map[string]interface{} // Used to store information regarding the last execution of a given method on a given data type
}
// NewHTTPCaller creates a new instance of the HTTP Caller
@ -77,7 +78,11 @@ func (caller *HTTPCaller) CallGet(url string, subpath string, types ...string) (
return nil, err
}
defer resp.Body.Close()
caller.LastResults = resp
err = caller.StoreResp(resp)
if err != nil {
return nil, err
}
return io.ReadAll(resp.Body)
}
@ -152,7 +157,7 @@ func (caller *HTTPCaller) CallRaw(method string, url string, subpath string,
if err != nil {
return nil, err
}
caller.LastResults = resp
return resp, nil
}
@ -174,3 +179,17 @@ func (caller *HTTPCaller) CallForm(method string, url string, subpath string,
client := &http.Client{}
return client.Do(req)
}
func (caller *HTTPCaller) StoreResp(resp *http.Response) error {
caller.LastResults["header"] = resp.Header
caller.LastResults["code"] = resp.StatusCode
data, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading the body of the last request")
return err
}
caller.LastResults["body"] = data
return nil
}