From 838e08f5ce36828a3101080210203626845dfc78 Mon Sep 17 00:00:00 2001 From: Shaw Date: Sat, 23 Sep 2023 21:43:04 +0800 Subject: [PATCH] fix: rpc function call --- pkg/client.go | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/pkg/client.go b/pkg/client.go index c71cc20..2a37cc0 100644 --- a/pkg/client.go +++ b/pkg/client.go @@ -5,6 +5,7 @@ import ( "encoding/base64" "encoding/json" "fmt" + "io" "net/http" "net/url" ) @@ -55,24 +56,48 @@ func (c *Client) From(table string) *RequestBuilder { } } -func (c Client) Rpc(f string, params interface{}) (*http.Response, error) { +func (c Client) Rpc(f string, params interface{}, r interface{}) error { b, err := json.Marshal(params) if err != nil { - return nil, err + return err } - req, err := http.NewRequest("POST", "/rpc/"+f, bytes.NewBuffer(b)) + req, err := http.NewRequest("POST", "rpc/"+f, bytes.NewBuffer(b)) + req.URL = c.Transport.baseURL.ResolveReference(req.URL) if err != nil { - return nil, err + return err } req.Header = c.Headers() resp, err := c.session.Do(req) if err != nil { - return nil, err + return err } defer resp.Body.Close() - return resp, nil + body, err := io.ReadAll(resp.Body) + if err != nil { + return err + } + + statusOK := resp.StatusCode >= 200 && resp.StatusCode < 300 + if !statusOK { + reqError := RequestError{HTTPStatusCode: resp.StatusCode} + + if err = json.Unmarshal(body, &reqError); err != nil { + return err + } + + return &reqError + } + + if resp.StatusCode != http.StatusNoContent && r != nil { + if err = json.Unmarshal(body, r); err != nil { + return err + } + } + + return nil + } func (c *Client) CloseIdleConnections() {