rpc: allow using a custom http client in rpc client (#3779)

fixes #2010

* allow using a custom http client in rpc client

* add tests and fix bug

* fix confusion between remote and address

* parse remote inside NewJSONRPCClientWithHTTPClient

* cleanups

* add warnings

* add changelog entry

* Update CHANGELOG_PENDING.md

Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com>
This commit is contained in:
gracenoah
2019-09-05 16:13:22 +03:00
committed by Anton Kaliaev
parent 72285115ec
commit aa721b972f
5 changed files with 118 additions and 28 deletions

View File

@ -2,6 +2,7 @@ package client
import (
"context"
"net/http"
"strings"
"sync"
"time"
@ -84,8 +85,19 @@ var _ rpcClient = (*baseRPCClient)(nil)
// NewHTTP takes a remote endpoint in the form <protocol>://<host>:<port> and
// the websocket path (which always seems to be "/websocket")
// The function panics if the provided remote is invalid.<Paste>
func NewHTTP(remote, wsEndpoint string) *HTTP {
rc := rpcclient.NewJSONRPCClient(remote)
httpClient := rpcclient.DefaultHTTPClient(remote)
return NewHTTPWithClient(remote, wsEndpoint, httpClient)
}
// NewHTTPWithClient allows for setting a custom http client. See NewHTTP
// The function panics if the provided client is nil or remote is invalid.
func NewHTTPWithClient(remote, wsEndpoint string, client *http.Client) *HTTP {
if client == nil {
panic("nil http.Client provided")
}
rc := rpcclient.NewJSONRPCClientWithHTTPClient(remote, client)
cdc := rc.Codec()
ctypes.RegisterAmino(cdc)
rc.SetCodec(cdc)