45 lines
989 B
Go
45 lines
989 B
Go
package tools
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
var HTTPCallerInstance = &HTTPCaller{}
|
|
|
|
type HTTPCaller struct {
|
|
Origin string
|
|
OriginSubPath string
|
|
DestSubPath string
|
|
}
|
|
|
|
func NewHTTPCaller(url string, origin string, originSubPath string, destSubPath string) *HTTPCaller {
|
|
return &HTTPCaller{
|
|
Origin: origin,
|
|
OriginSubPath: originSubPath,
|
|
DestSubPath: destSubPath,
|
|
}
|
|
}
|
|
|
|
func (caller *HTTPCaller) CallGet(url string, subpath string) ([]byte, error) {
|
|
resp, err := http.Get(url + subpath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
return io.ReadAll(resp.Body)
|
|
}
|
|
|
|
func (caller *HTTPCaller) CallPost(url string, subpath string, body map[string]interface{}) ([]byte, error) {
|
|
postBody, _ := json.Marshal(body)
|
|
responseBody := bytes.NewBuffer(postBody)
|
|
resp, err := http.Post(url+subpath, "application/json", responseBody)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
return io.ReadAll(resp.Body)
|
|
}
|