lotus/api/client/client.go

111 lines
2.8 KiB
Go
Raw Normal View History

package client
import (
"context"
2019-07-23 18:49:09 +00:00
"net/http"
2020-08-14 14:06:53 +00:00
"net/url"
"path"
2020-08-14 21:24:41 +00:00
"time"
2019-07-23 18:49:09 +00:00
2020-05-20 17:43:22 +00:00
"github.com/filecoin-project/go-jsonrpc"
"github.com/filecoin-project/lotus/api"
2020-05-20 17:43:22 +00:00
"github.com/filecoin-project/lotus/api/apistruct"
2020-08-14 14:06:53 +00:00
"github.com/filecoin-project/lotus/lib/rpcenc"
)
2019-08-02 16:25:10 +00:00
// NewCommonRPC creates a new http jsonrpc client.
func NewCommonRPC(ctx context.Context, addr string, requestHeader http.Header) (api.Common, jsonrpc.ClientCloser, error) {
2019-12-09 17:08:32 +00:00
var res apistruct.CommonStruct
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
2019-08-02 16:25:10 +00:00
[]interface{}{
&res.Internal,
2020-03-24 18:00:08 +00:00
},
requestHeader,
)
2019-08-02 16:25:10 +00:00
2019-10-03 18:12:30 +00:00
return &res, closer, err
2019-08-02 16:25:10 +00:00
}
// NewFullNodeRPC creates a new http jsonrpc client.
func NewFullNodeRPC(ctx context.Context, addr string, requestHeader http.Header) (api.FullNode, jsonrpc.ClientCloser, error) {
2019-12-09 17:08:32 +00:00
var res apistruct.FullNodeStruct
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
[]interface{}{
&res.CommonStruct.Internal,
&res.Internal,
}, requestHeader)
2019-10-03 18:12:30 +00:00
return &res, closer, err
}
// NewStorageMinerRPC creates a new http jsonrpc client for miner
func NewStorageMinerRPC(ctx context.Context, addr string, requestHeader http.Header, opts ...jsonrpc.Option) (api.StorageMiner, jsonrpc.ClientCloser, error) {
2019-12-09 17:08:32 +00:00
var res apistruct.StorageMinerStruct
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
[]interface{}{
&res.CommonStruct.Internal,
&res.Internal,
2020-03-24 18:00:08 +00:00
},
requestHeader,
opts...,
2020-03-24 18:00:08 +00:00
)
2019-10-03 18:12:30 +00:00
return &res, closer, err
2019-07-24 01:10:26 +00:00
}
2020-03-11 01:57:52 +00:00
func NewWorkerRPC(ctx context.Context, addr string, requestHeader http.Header) (api.Worker, jsonrpc.ClientCloser, error) {
2020-08-14 14:06:53 +00:00
u, err := url.Parse(addr)
if err != nil {
return nil, nil, err
}
switch u.Scheme {
case "ws":
u.Scheme = "http"
case "wss":
u.Scheme = "https"
}
///rpc/v0 -> /rpc/streams/v0/push
u.Path = path.Join(u.Path, "../streams/v0/push")
2020-03-11 01:57:52 +00:00
var res apistruct.WorkerStruct
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
2020-03-11 01:57:52 +00:00
[]interface{}{
&res.Internal,
2020-03-24 18:00:08 +00:00
},
2020-08-14 21:24:41 +00:00
requestHeader,
rpcenc.ReaderParamEncoder(u.String()),
jsonrpc.WithNoReconnect(),
jsonrpc.WithTimeout(30*time.Second),
2020-03-24 18:00:08 +00:00
)
2020-03-11 01:57:52 +00:00
return &res, closer, err
}
2020-09-05 19:36:32 +00:00
// NewGatewayRPC creates a new http jsonrpc client for a gateway node.
func NewGatewayRPC(ctx context.Context, addr string, requestHeader http.Header, opts ...jsonrpc.Option) (api.Gateway, jsonrpc.ClientCloser, error) {
var res apistruct.GatewayStruct
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
[]interface{}{
&res.Internal,
},
requestHeader,
opts...,
)
return &res, closer, err
}
func NewWalletRPC(ctx context.Context, addr string, requestHeader http.Header) (api.Wallet, jsonrpc.ClientCloser, error) {
2020-09-05 19:36:32 +00:00
var res apistruct.WalletStruct
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
[]interface{}{
&res.Internal,
},
requestHeader,
)
return &res, closer, err
}