implement remote call for remote action
This commit is contained in:
44
tools/remote_caller.go
Normal file
44
tools/remote_caller.go
Normal file
@@ -0,0 +1,44 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user