From b4c60e8e90c90edbfe97b838ebdce9a49e2ae9ed Mon Sep 17 00:00:00 2001
From: a <a@tuxpa.in>
Date: Sat, 13 May 2023 00:59:49 -0500
Subject: [PATCH] ok

---
 codec/http/client.go      | 17 ++++++++++++++++-
 codec/http/http_client.go | 40 ---------------------------------------
 2 files changed, 16 insertions(+), 41 deletions(-)

diff --git a/codec/http/client.go b/codec/http/client.go
index e89799c..89bde9f 100644
--- a/codec/http/client.go
+++ b/codec/http/client.go
@@ -67,8 +67,19 @@ func (c *Client) Do(ctx context.Context, result any, method string, params any)
 		return err
 	}
 	defer resp.Body.Close()
+	msg := &codec.Message{}
+	err = json.NewDecoder(resp.Body).Decode(&msg)
+	if err != nil {
+		return err
+	}
+	if msg.Error != nil {
+		return err
+	}
 	if result != nil {
-		json.NewDecoder(resp.Body).Decode(&result)
+		err = json.Unmarshal(msg.Result, &msg)
+		if err != nil {
+			return err
+		}
 	}
 	return nil
 }
@@ -125,6 +136,10 @@ func (c *Client) BatchCall(ctx context.Context, b ...*jrpc.BatchElem) error {
 			b[idx].Error = fmt.Errorf("No response found")
 			continue
 		}
+		if ans.Error != nil {
+			b[idx].Error = ans.Error
+			continue
+		}
 		if b[idx].Result == nil {
 			continue
 		}
diff --git a/codec/http/http_client.go b/codec/http/http_client.go
index 605155d..f418615 100644
--- a/codec/http/http_client.go
+++ b/codec/http/http_client.go
@@ -1,7 +1,6 @@
 package jrpc
 
 import (
-	"bytes"
 	"context"
 	"io"
 	"net/http"
@@ -11,45 +10,6 @@ import (
 	"github.com/goccy/go-json"
 )
 
-func (hc *httpConn) doRequest(ctx context.Context, msg any) (io.ReadCloser, error) {
-	// TODO:
-	// the jsoniter encoder performs a lot better here, not sure why. (nearly 10%? maybe more)
-	body, err := jzon.Marshal(msg)
-	if err != nil {
-		return nil, err
-	}
-	req, err := http.NewRequestWithContext(ctx, "POST", hc.url, io.NopCloser(bytes.NewReader(body)))
-	if err != nil {
-		return nil, err
-	}
-	req.ContentLength = int64(len(body))
-	req.GetBody = func() (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(body)), nil }
-
-	// set headers
-	hc.mu.Lock()
-	req.Header = hc.headers.Clone()
-	hc.mu.Unlock()
-
-	// do request
-	resp, err := hc.client.Do(req)
-	if err != nil {
-		return nil, err
-	}
-	if resp.StatusCode < 200 || resp.StatusCode >= 300 {
-		var buf bytes.Buffer
-		var body []byte
-		if _, err := buf.ReadFrom(resp.Body); err == nil {
-			body = buf.Bytes()
-		}
-		return nil, HTTPError{
-			Status:     resp.Status,
-			StatusCode: resp.StatusCode,
-			Body:       body,
-		}
-	}
-	return resp.Body, nil
-}
-
 // DialHTTPWithClient creates a new RPC client that connects to an RPC server over HTTP
 // using the provided HTTP Client.
 func DialHTTPWithClient(endpoint string, client *http.Client) (*Client, error) {
-- 
GitLab