mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 04:36:42 +00:00
Update http.go
This commit is contained in:
parent
a9eaf2ffd8
commit
3a5d53d5d5
1 changed files with 16 additions and 18 deletions
34
rpc/http.go
34
rpc/http.go
|
|
@ -177,35 +177,33 @@ func cleanlyCloseBody(body io.ReadCloser) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) sendHTTP(ctx context.Context, op *requestOp, msg interface{}) error {
|
func (c *Client) sendHTTP(ctx context.Context, op *requestOp, msg interface{}) error {
|
||||||
hc := c.writeConn.(*httpConn)
|
return c.sendHTTPWithDecoder(ctx, op, msg, func(dec *json.Decoder) ([]*jsonrpcMessage, error) {
|
||||||
respBody, err := hc.doRequest(ctx, msg)
|
var resp jsonrpcMessage
|
||||||
if err != nil {
|
return []*jsonrpcMessage{&resp}, dec.Decode(&resp)
|
||||||
return err
|
})
|
||||||
}
|
|
||||||
defer cleanlyCloseBody(respBody)
|
|
||||||
|
|
||||||
var resp jsonrpcMessage
|
|
||||||
batch := [1]*jsonrpcMessage{&resp}
|
|
||||||
if err := json.NewDecoder(respBody).Decode(&resp); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
op.resp <- batch[:]
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) sendBatchHTTP(ctx context.Context, op *requestOp, msgs []*jsonrpcMessage) error {
|
func (c *Client) sendBatchHTTP(ctx context.Context, op *requestOp, msgs []*jsonrpcMessage) error {
|
||||||
|
return c.sendHTTPWithDecoder(ctx, op, msgs, func(dec *json.Decoder) ([]*jsonrpcMessage, error) {
|
||||||
|
var respmsgs []*jsonrpcMessage
|
||||||
|
return respmsgs, dec.Decode(&respmsgs)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// sendHTTPWithDecoder performs an HTTP request and delegates response decoding to the provided function.
|
||||||
|
func (c *Client) sendHTTPWithDecoder(ctx context.Context, op *requestOp, payload interface{}, decode func(*json.Decoder) ([]*jsonrpcMessage, error)) error {
|
||||||
hc := c.writeConn.(*httpConn)
|
hc := c.writeConn.(*httpConn)
|
||||||
respBody, err := hc.doRequest(ctx, msgs)
|
respBody, err := hc.doRequest(ctx, payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer cleanlyCloseBody(respBody)
|
defer cleanlyCloseBody(respBody)
|
||||||
|
|
||||||
var respmsgs []*jsonrpcMessage
|
resp, err := decode(json.NewDecoder(respBody))
|
||||||
if err := json.NewDecoder(respBody).Decode(&respmsgs); err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
op.resp <- respmsgs
|
op.resp <- resp
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue