rpc-go/protocol/json/client.go

40 lines
914 B
Go
Raw Normal View History

2018-04-03 08:58:26 +00:00
package json
import (
"encoding/json"
"git.loafle.net/commons/rpc-go/protocol"
)
// ----------------------------------------------------------------------------
// Codec
// ----------------------------------------------------------------------------
// NewClientCodec returns a new JSON Codec.
func NewClientCodec() protocol.ClientCodec {
return &ClientCodec{}
}
// ClientCodec creates a ClientCodecRequest to process each request.
type ClientCodec struct {
}
func (cc *ClientCodec) NewRequest(method string, args []interface{}, id interface{}) ([]byte, error) {
params, err := convertParamsToStringArray(args)
if nil != err {
return nil, err
}
req := &clientRequest{
Version: Version,
Method: method,
Params: params,
ID: id,
}
return json.Marshal(req)
}
func (cc *ClientCodec) NewResponse(buf []byte) (protocol.ClientResponseCodec, error) {
return newClientResponseCodec(buf)
}