From d9cb0aae90e7b6f2db9d42c2f46052fd2c5e6f32 Mon Sep 17 00:00:00 2001 From: Sayan Biswas Date: Thu, 28 Oct 2021 17:18:03 +0530 Subject: [PATCH] Add a simple RPC request helper. Signed-off-by: Sayan Biswas --- client/constants.go | 2 +- client/helpers/request.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 client/helpers/request.go diff --git a/client/constants.go b/client/constants.go index ba69c91..2432fb4 100644 --- a/client/constants.go +++ b/client/constants.go @@ -1,6 +1,6 @@ package client const ( - RPC_ENDPOINT = "https://socialvoid.qlg1.com:5601/" + RPC_ENDPOINT = "http://socialvoid.qlg1.com:5601/" VERSION = "0.0.1" ) diff --git a/client/helpers/request.go b/client/helpers/request.go new file mode 100644 index 0000000..666d9bc --- /dev/null +++ b/client/helpers/request.go @@ -0,0 +1,30 @@ +package helpers + +import ( + "bytes" + "encoding/json" + "io/ioutil" + "net/http" +) + +type RPCMethod struct { + Method string `json:"method"` + Params interface{} `json:"params"` +} + +func DoRequest(url string, r *RPCMethod) (interface{}, error) { + j, err := json.Marshal(r.Params) + if err != nil { + return nil, err + } + resp, err := http.Post(url, "application/json-rpc", bytes.NewBuffer(j)) + if err != nil { + return nil, err + } + defer resp.Body.Close() + b, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + return b, nil +}