Skip to content

Commit

Permalink
Feat/headers adds header functionality to http and websockets (umbrac…
Browse files Browse the repository at this point in the history
…le#140)

* added in header functionality

* draft implementation for websockets

* minor formatting changes

Co-authored-by: Ben Wilson <[email protected]>
  • Loading branch information
aBitUnique and Ben Wilson authored Jan 12, 2022
1 parent 013846a commit a8fbcb8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
25 changes: 21 additions & 4 deletions jsonrpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,41 @@ type endpoints struct {
n *Net
d *Debug
}
type Config struct {
headers map[string]string
}

type ConfigOption func(*Config)

func WithHeaders(headers map[string]string) ConfigOption {
return func(c *Config) {
for k, v := range headers {
c.headers[k] = v
}
}
}

func NewClient(addr string, opts ...ConfigOption) (*Client, error) {
config := &Config{headers: map[string]string{}}
for _, opt := range opts {
opt(config)
}

// NewClient creates a new client
func NewClient(addr string) (*Client, error) {
c := &Client{}
c.endpoints.w = &Web3{c}
c.endpoints.e = &Eth{c}
c.endpoints.n = &Net{c}
c.endpoints.d = &Debug{c}

t, err := transport.NewTransport(addr)
t, err := transport.NewTransport(addr, config.headers)
if err != nil {
return nil, err
}
c.transport = t
return c, nil
}

// Close closes the tranport
// Close closes the transport
func (c *Client) Close() error {
return c.transport.Close()
}
Expand Down
7 changes: 6 additions & 1 deletion jsonrpc/transport/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import (
type HTTP struct {
addr string
client *fasthttp.Client
headers map[string]string
}

func newHTTP(addr string) *HTTP {
func newHTTP(addr string, headers map[string]string) *HTTP {
return &HTTP{
addr: addr,
client: &fasthttp.Client{},
headers: headers,
}
}

Expand Down Expand Up @@ -53,6 +55,9 @@ func (h *HTTP) Call(method string, out interface{}, params ...interface{}) error
req.SetRequestURI(h.addr)
req.Header.SetMethod("POST")
req.Header.SetContentType("application/json")
for k, v := range h.headers {
req.Header.Add(k, v)
}
req.SetBody(raw)

if err := h.client.Do(req, res); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions jsonrpc/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ const (
)

// NewTransport creates a new transport object
func NewTransport(url string) (Transport, error) {
func NewTransport(url string, headers map[string]string) (Transport, error) {
if strings.HasPrefix(url, wsPrefix) || strings.HasPrefix(url, wssPrefix) {
t, err := newWebsocket(url)
t, err := newWebsocket(url, headers)
if err != nil {
return nil, err
}
Expand All @@ -45,5 +45,5 @@ func NewTransport(url string) (Transport, error) {
}
return t, nil
}
return newHTTP(url), nil
return newHTTP(url, headers), nil
}
6 changes: 5 additions & 1 deletion jsonrpc/transport/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import (
"github.com/umbracle/go-web3/jsonrpc/codec"
)

func newWebsocket(url string) (Transport, error) {
func newWebsocket(url string, headers map[string]string) (Transport, error) {
wsHeaders := http.Header{}
for k, v := range headers {
wsHeaders.Add(k, v)
}
wsConn, _, err := websocket.DefaultDialer.Dial(url, http.Header{})
if err != nil {
return nil, err
Expand Down

0 comments on commit a8fbcb8

Please sign in to comment.