From b24bb636eec1c79ed5bd0e6ca9df0033330a75a9 Mon Sep 17 00:00:00 2001 From: Seiji Date: Thu, 22 Aug 2024 03:13:34 -0400 Subject: [PATCH] add view query --- aptosclient/rest_client.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/aptosclient/rest_client.go b/aptosclient/rest_client.go index 109e297..d6c6d0b 100644 --- a/aptosclient/rest_client.go +++ b/aptosclient/rest_client.go @@ -1,6 +1,7 @@ package aptosclient import ( + "bytes" "context" "encoding/json" "io" @@ -100,6 +101,34 @@ func (c *RestClient) RawQuery(urlWithoutVersion string, params map[string]string return } +func (c *RestClient) ViewQuery(payload map[string]string) (res []byte, err error) { + data, err := json.Marshal(payload) + if err != nil { + return + } + req, err := http.NewRequest("POST", c.GetVersionedRpcUrl()+"/view", bytes.NewReader(data)) + if err != nil { + return + } + req.Header["Content-Type"] = []string{"application/json"} + + resp, err := c.c.Do(req) + if err != nil { + return + } + res, err = io.ReadAll(resp.Body) + if err != nil { + return + } + if resp.StatusCode >= http.StatusBadRequest { + restError := &aptostypes.RestError{} + json.Unmarshal(res, &restError) + restError.Code = resp.StatusCode + return nil, restError + } + return +} + func (c *RestClient) setChainId() (err error) { ledger, err := c.LedgerInfo() if err != nil {