Skip to content

Commit

Permalink
Merge pull request #230 from ipfs-force-community/opt/dial-rpc
Browse files Browse the repository at this point in the history
opt: add DialMinerRPC
  • Loading branch information
LinZexiao committed Sep 22, 2023
2 parents befe933 + 2f99ce5 commit a99ec7d
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"fmt"
"net/http"

"github.com/filecoin-project/go-jsonrpc"
Expand Down Expand Up @@ -42,3 +43,23 @@ func NewMinerRPC(ctx context.Context, addr string, requestHeader http.Header, op

return &res, closer, err
}

// DialMinerRPC is a more convinient way of building client, as it resolves any format (url, multiaddr) of addr string.
func DialMinerRPC(ctx context.Context, addr string, token string, requestHeader http.Header, opts ...jsonrpc.Option) (api.MinerAPI, jsonrpc.ClientCloser, error) {
ainfo := venus_api.NewAPIInfo(addr, token)
endpoint, err := ainfo.DialArgs(venus_api.VerString(MajorVersion))
if err != nil {
return nil, nil, fmt.Errorf("get dial args: %w", err)
}

if requestHeader == nil {
requestHeader = http.Header{}
}
requestHeader.Set(venus_api.VenusAPINamespaceHeader, APINamespace)
ainfo.SetAuthHeader(requestHeader)

var res api.MinerAPIStruct
closer, err := jsonrpc.NewMergeClient(ctx, endpoint, MethodNamespace, venus_api.GetInternalStructs(&res), requestHeader, opts...)

return &res, closer, err
}

0 comments on commit a99ec7d

Please sign in to comment.