Skip to content

Commit

Permalink
Allow providing your own underlyling http client
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio committed Jan 22, 2025
1 parent 5f6f878 commit 235a422
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
16 changes: 12 additions & 4 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"net/http"

"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/jhttp"
Expand All @@ -10,11 +11,12 @@ import (
)

type Client struct {
url string
cli *jrpc2.Client
url string
cli *jrpc2.Client
httpClient *http.Client
}

func NewClient(url string) *Client {
func NewClient(url string, httpClient *http.Client) *Client {
c := &Client{url: url}
c.refreshClient()
return c
Expand All @@ -28,7 +30,13 @@ func (c *Client) refreshClient() {
if c.cli != nil {
c.cli.Close()
}
ch := jhttp.NewChannel(c.url, nil)
var opts *jhttp.ChannelOptions
if c.httpClient != nil {
opts = &jhttp.ChannelOptions{
Client: c.httpClient,
}
}
ch := jhttp.NewChannel(c.url, opts)
c.cli = jrpc2.NewClient(ch, nil)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func NewTest(t *testing.T, cfg *TestConfig) *Test {
i.spawnRPCDaemon()
}

i.rpcClient = client.NewClient(i.GetSorobanRPCURL())
i.rpcClient = client.NewClient(i.GetSorobanRPCURL(), nil)
if shouldWaitForRPC {
i.waitForRPC()
}
Expand Down

0 comments on commit 235a422

Please sign in to comment.