isle/go/daemon/jsonrpc2/client_rw.go

40 lines
794 B
Go
Raw Normal View History

package jsonrpc2
import (
"context"
"encoding/json"
"fmt"
"io"
)
type rwClient struct {
enc *json.Encoder
dec *json.Decoder
}
// NewReadWriterClient returns a Client which will use the given ReadWriter to
// perform requests.
func NewReadWriterClient(rw io.ReadWriter) Client {
return rwClient{json.NewEncoder(rw), json.NewDecoder(rw)}
}
func (c rwClient) Call(
ctx context.Context, rcv any, method string, params ...any,
) error {
id, err := encodeRequest(c.enc, method, params)
if err != nil {
return fmt.Errorf("encoding request: %w", err)
}
resID, err := decodeResponse(c.dec, rcv)
if err != nil {
return fmt.Errorf("decoding response: %w", err)
} else if resID != id {
return fmt.Errorf(
"expected response with ID %q, got %q", id, resID,
)
}
return nil
}