diff --git a/thorclient/httpclient/client.go b/thorclient/httpclient/client.go index 8f88783f5..ce05bf17f 100644 --- a/thorclient/httpclient/client.go +++ b/thorclient/httpclient/client.go @@ -33,9 +33,13 @@ type Client struct { // New creates a new Client with the provided URL. func New(url string) *Client { + return NewWithHTTP(url, http.DefaultClient) +} + +func NewWithHTTP(url string, c *http.Client) *Client { return &Client{ url: url, - c: &http.Client{}, + c: c, } } diff --git a/thorclient/thorclient.go b/thorclient/thorclient.go index 8458a0ae4..0b7939f51 100644 --- a/thorclient/thorclient.go +++ b/thorclient/thorclient.go @@ -11,6 +11,7 @@ package thorclient import ( "fmt" + "net/http" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/math" @@ -44,6 +45,13 @@ func New(url string) *Client { } } +// NewWithHTTP creates a new Client using the provided HTTP URL and HTTP client. +func NewWithHTTP(url string, c *http.Client) *Client { + return &Client{ + httpConn: httpclient.NewWithHTTP(url, c), + } +} + // NewWithWS creates a new Client using the provided HTTP and WebSocket URLs. // Returns an error if the WebSocket connection fails. func NewWithWS(url string) (*Client, error) { @@ -202,7 +210,7 @@ func (c *Client) ChainTag() (byte, error) { } // SubscribeBlocks subscribes to block updates over WebSocket. -func (c *Client) SubscribeBlocks(pos string) (*common.Subscription[*blocks.JSONCollapsedBlock], error) { +func (c *Client) SubscribeBlocks(pos string) (*common.Subscription[*subscriptions.BlockMessage], error) { if c.wsConn == nil { return nil, fmt.Errorf("not a websocket typed client") } diff --git a/thorclient/wsclient/client.go b/thorclient/wsclient/client.go index 057d5aa48..9eb1519ab 100644 --- a/thorclient/wsclient/client.go +++ b/thorclient/wsclient/client.go @@ -16,7 +16,6 @@ import ( "github.com/vechain/thor/v2/thor" "github.com/gorilla/websocket" - "github.com/vechain/thor/v2/api/blocks" "github.com/vechain/thor/v2/api/subscriptions" "github.com/vechain/thor/v2/thorclient/common" ) @@ -89,7 +88,7 @@ func (c *Client) SubscribeEvents(pos string, filter *subscriptions.EventFilter) // SubscribeBlocks subscribes to block updates based on the provided query. // It returns a Subscription that streams block messages or an error if the connection fails. -func (c *Client) SubscribeBlocks(pos string) (*common.Subscription[*blocks.JSONCollapsedBlock], error) { +func (c *Client) SubscribeBlocks(pos string) (*common.Subscription[*subscriptions.BlockMessage], error) { queryValues := &url.Values{} queryValues.Add("pos", pos) conn, err := c.connect("/subscriptions/block", queryValues) @@ -97,7 +96,7 @@ func (c *Client) SubscribeBlocks(pos string) (*common.Subscription[*blocks.JSONC return nil, fmt.Errorf("unable to connect - %w", err) } - return subscribe[blocks.JSONCollapsedBlock](conn), nil + return subscribe[subscriptions.BlockMessage](conn), nil } // SubscribeTransfers subscribes to transfer events based on the provided query. diff --git a/thorclient/wsclient/client_test.go b/thorclient/wsclient/client_test.go index 483ae7233..19dd1b395 100644 --- a/thorclient/wsclient/client_test.go +++ b/thorclient/wsclient/client_test.go @@ -13,13 +13,11 @@ import ( "testing" "time" - "github.com/vechain/thor/v2/test/datagen" - "github.com/vechain/thor/v2/thor" - "github.com/gorilla/websocket" "github.com/stretchr/testify/assert" - "github.com/vechain/thor/v2/api/blocks" "github.com/vechain/thor/v2/api/subscriptions" + "github.com/vechain/thor/v2/test/datagen" + "github.com/vechain/thor/v2/thor" "github.com/vechain/thor/v2/thorclient/common" ) @@ -50,7 +48,7 @@ func TestClient_SubscribeEvents(t *testing.T) { func TestClient_SubscribeBlocks(t *testing.T) { pos := "best" - expectedBlock := &blocks.JSONCollapsedBlock{} + expectedBlock := &subscriptions.BlockMessage{} ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, "/subscriptions/block", r.URL.Path) @@ -288,7 +286,7 @@ func TestClient_SubscribeBlocks_ServerError(t *testing.T) { func TestClient_SubscribeBlocks_ServerShutdown(t *testing.T) { pos := "best" - expectedBlock := &blocks.JSONCollapsedBlock{} + expectedBlock := &subscriptions.BlockMessage{} ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, "/subscriptions/block", r.URL.Path) @@ -325,7 +323,7 @@ func TestClient_SubscribeBlocks_ServerShutdown(t *testing.T) { func TestClient_SubscribeBlocks_ClientShutdown(t *testing.T) { pos := "best" - expectedBlock := &blocks.JSONCollapsedBlock{} + expectedBlock := &subscriptions.BlockMessage{} ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, "/subscriptions/block", r.URL.Path) @@ -377,7 +375,7 @@ func TestClient_SubscribeBlocks_ClientShutdown(t *testing.T) { func TestClient_SubscribeBlocks_ClientShutdown_LongBlocks(t *testing.T) { pos := "best" - expectedBlock := &blocks.JSONCollapsedBlock{} + expectedBlock := &subscriptions.BlockMessage{} ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, "/subscriptions/block", r.URL.Path)