modified the way HTTPCaller store last resposne
This commit is contained in:
parent
363ac94c47
commit
b81c60a3ce
@ -3,6 +3,7 @@ package tools
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -49,9 +50,9 @@ func ToMethod(str string) METHOD {
|
|||||||
var HTTPCallerInstance = &HTTPCaller{} // Singleton instance of the HTTPCaller
|
var HTTPCallerInstance = &HTTPCaller{} // Singleton instance of the HTTPCaller
|
||||||
|
|
||||||
type HTTPCaller struct {
|
type HTTPCaller struct {
|
||||||
URLS map[DataType]map[METHOD]string // Map of the different methods and their urls
|
URLS map[DataType]map[METHOD]string // Map of the different methods and their urls
|
||||||
Disabled bool // Disabled flag
|
Disabled bool // Disabled flag
|
||||||
LastResults *http.Response // Used to store information regarding the last execution of a given method on a given data type
|
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
|
// 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
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
caller.LastResults = resp
|
err = caller.StoreResp(resp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return io.ReadAll(resp.Body)
|
return io.ReadAll(resp.Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,7 +157,7 @@ func (caller *HTTPCaller) CallRaw(method string, url string, subpath string,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
caller.LastResults = resp
|
caller.LastResults = resp
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
@ -174,3 +179,17 @@ func (caller *HTTPCaller) CallForm(method string, url string, subpath string,
|
|||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
return client.Do(req)
|
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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user